Hi, this series adds static type hints to the QAPI module. This is part two, and covers introspect.py.
Part 2: https://gitlab.com/jsnow/qemu/-/tree/python-qapi-cleanup-pt2 Everything: https://gitlab.com/jsnow/qemu/-/tree/python-qapi-cleanup-pt6 - Requires Python 3.6+ - Requires mypy 0.770 or newer (for type analysis only) - Requires pylint 2.6.0 or newer (for lint checking only) (Note: pylint does not like Python 3.9 very much yet. Known problem.) Type hints are added in patches that add *only* type hints and change no other behavior. Any necessary changes to behavior to accommodate typing are split out into their own tiny patches. Every commit should pass with (from ./scripts): - flake8 qapi/ - pylint --rcfile=qapi/pylintrc qapi/ - mypy --config-file=qapi/mypy.ini qapi/ - isort -c qapi/ V6: 001/19:[down] 'qapi: Replace List[str] with Sequence[str] for ifcond' 009/19:[0021] [FC] 'qapi/introspect.py: Introduce preliminary tree typing' 010/19:[0013] [FC] 'qapi/introspect.py: create a typed 'Annotated' data strutcure' 013/19:[down] 'qapi/introspect.py: remove _gen_variants helper' 014/19:[0053] [FC] 'qapi/introspect.py: add type hint annotations' 015/19:[down] 'qapi/introspect.py: Add docstrings to _gen_tree and _tree_to_qlit' 017/19:[down] 'qapi/introspect.py: Type _gen_tree variants as Sequence[str]' 018/19:[down] 'qapi/introspect.py: set _gen_tree's default ifcond argument to ()' 019/19:[down] 'qapi/introspect.py: add SchemaMetaType enum' 01: New; consistently type ifcond as Seq[str] in already-typed files. 09: Adjust comment concerning _stub to be more clear (?) Rename _stub to _Stub, etc. TreeValue becomes JSONValue. 10: _NodeT becomes _ValueT to match the _Value name. Change visit_alternate_type whitespace around some more. 13: New, pre-requisite for using SchemaInfo aliases. (Was not appropriate to go into #14.) 14: Use Sequence[str] instead of List[str] for ifcond Use SchemaInfo "dummy types" instead of _DObject 15: Adjust comment to mention dict_value limitation. Add docstring for _gen_tree (from former "dummy types" patch). Change name of commit to reflect now-multiple docstring additions. OPTIONAL PATCHES: 17: Use Sequence[QAPISchemaFeature] instead of Optional[List[QAPISchemaFeature]] 18: Set a default argument for ifcond to the empty tuple (). Stylistically matches the above patch. 19: Create a SchemaMetaType enum and use it instead of the string type. (Contains an optional blurb that can be removed if desired.) V5: 04: Rename 'suppress_first_indent' to 'dict_value' (Docstring added in 014.) 06: Avoid changing the output structure of _make_tree 07: Chance the structure of _make_tree 8-) 08: Change commented TreeValue to include a TODO instead. 09: Change NodeT bound to _value instead of TreeValue Change "Remove in 3.7" text to include "TODO: " Remove forwarding suppress_first_indent/dict_value in recursive cases Change spacing in visit_alternate_type() 11: Consequence of suppress_first_value/dict_value change 12: Commit message note added Changed _DObject comment 13: Commit notes adjusted _DObject stuff: Comment near SchemaInfo et al adjusted 14: Changed docstring to reflect dict_value change 15: Updated copyright year for 2021 :~) V4: - Rebased on "pt1.5" v4 - signatures updated to use Optional[QAPISourceInfo] John Snow (19): qapi: Replace List[str] with Sequence[str] for ifcond qapi/introspect.py: assert schema is not None qapi/introspect.py: use _make_tree for features nodes qapi/introspect.py: add _gen_features helper qapi/introspect.py: guard against ifcond/comment misuse qapi/introspect.py: Unify return type of _make_tree() qapi/introspect.py: replace 'extra' dict with 'comment' argument qapi/introspect.py: Always define all 'extra' dict keys qapi/introspect.py: Introduce preliminary tree typing qapi/introspect.py: create a typed 'Annotated' data strutcure qapi/introspect.py: improve _tree_to_qlit error message qapi/introspect.py: improve readability of _tree_to_qlit qapi/introspect.py: remove _gen_variants helper qapi/introspect.py: add type hint annotations qapi/introspect.py: Add docstrings to _gen_tree and _tree_to_qlit qapi/introspect.py: Update copyright and authors list qapi/introspect.py: Type _gen_tree variants as Sequence[str] qapi/introspect.py: set _gen_tree's default ifcond argument to () qapi/introspect.py: add SchemaMetaType enum scripts/qapi/commands.py | 3 +- scripts/qapi/events.py | 4 +- scripts/qapi/gen.py | 12 +- scripts/qapi/introspect.py | 350 +++++++++++++++++++++++++++---------- scripts/qapi/mypy.ini | 5 - scripts/qapi/schema.py | 2 +- scripts/qapi/types.py | 12 +- scripts/qapi/visit.py | 10 +- 8 files changed, 275 insertions(+), 123 deletions(-) -- 2.29.2