Fix Argument typing, modernize to 3.10+, reserve internal attr names#46
Merged
Conversation
Three related typing/quality improvements: 1. Argument() no longer infers as Any. The fallback overload now returns a bare TypeVar so annotated attributes adopt their declared type (e.g. `tags: list[str] = Argument(nargs="+")` infers `list[str]`), silencing basedpyright's reportAny on the common untyped form. 2. Modernize the library to PEP 604/585 (3.10+): Optional[X] -> X | None, Union -> |, List/Dict/Tuple/Type -> builtins, typing.Callable/Iterable /Mapping -> collections.abc. Enabled ruff's UP rules (tests are exempt since they intentionally exercise legacy typing forms). 3. Reserve internal attribute names. current_subparsers, current_subparser, and __parent__ now raise ArgumentDefinitionError when used as user argument/group/subparser names, instead of silently clobbering parser state. This let the load-bearing type-comment on AbstractParser.current_subparsers become a normal annotation. Adds tests/test_reserved_names.py and documents the reserved names in docs/subparsers.md (+ a pitfalls.md cross-reference).
- store.py: bare type[Action]/type[ConfigAction] annotations collided with the TypedArgument.type field in the class annotation scope under PEP 649 lazy evaluation, breaking every Parser on 3.14. Qualify as builtins.type (Type[...] would just be reverted by ruff UP006). - Reserved-name guard: cls.__dict__['__annotations__'] is absent on 3.14 (lazy), so own_annotation_keys() falls back to annotationlib in FORWARDREF format to read a class's own annotation keys. - ruff format tests/test_edge_cases.py (pre-existing drift CI flags).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three related typing/quality improvements:
Argument()no longer infers asAny. The fallback overload nowreturns a bare
TypeVar, so annotated attributes adopt their declaredtype —
tags: list[str] = Argument(nargs="+")inferslist[str]—silencing basedpyright's
reportAnyon the common untyped form.Modernized the library to PEP 604/585 (3.10+):
Optional[X]→X | None,Union→|,List/Dict/Tuple/Type→ builtins,typing.Callable/
Iterable/Mapping→collections.abc. Enabled ruff'sUPrules.Tests are exempt (they intentionally exercise legacy typing forms to
verify backward compatibility).
Reserved internal attribute names.
current_subparsers,current_subparser, and__parent__now raiseArgumentDefinitionErrorwhen used as user argument/group/subparser names, instead of silently
clobbering parser state. This let the load-bearing type-comment on
AbstractParser.current_subparsersbecome a normal annotation.Tests & docs
tests/test_reserved_names.py(10 tests).docs/subparsers.md+ apitfalls.mdcross-reference.Verification
ruff checkclean,ruff formatclean.mypyclean (only the pre-existingtomllibstdlib-stub baseline).