python - How to type hint yielded variable in with...as... style context manager? -


consider sample json file:

[     {"name": "alex"},     {"name": "roger"},     {"name": "lily"},     {"name": "billy"} ] 

it easy interact disk follows:

import os import json contextlib import contextmanager  @contextmanager def documentdb(file_name):     open(file_name, mode='rt') fp:         cur = json.load(fp)     yield cur     open(file_name, mode='wt') fp:         json.dump(cur, fp)     return  # following code works nicely documentdb("sample.json") dbcur:     print(dbcur) 

but how assert / hint dbcur in above code should list of dict objects? tried this:

from typing import list  # not work documentdb("sample.json") dbcur: list[dict]:     print(dbcur) 

but syntax error.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -