Repository navigation
Fix Python type errors - #32
Merged
Merged
Conversation
With a complete _rustlib stub, mypy and pyright reported these errors: - _ConfigNode's invalid_ordering is a tuple of any length. - BaseNode._SUBCLASS_WRAP needs a ClassVar annotation. - TreeDom.__eq__ must accept any object, as object.__eq__ does. - create_element accepts a QualName name, as the Rust constructor does. - AttrsList's (key, value) pairs are typed as such, rather than as any tuple, which could have supplied the start argument when unpacked. - AttrsList.get's default of None conflicted with its type; give it overloads. _find_by_key's default argument was never passed. - AttrsList.extend and __setitem__ reassigned variables with values of different types. - The iterators' _BASECLASS is called with a raw node, not a BaseNode. - Traverse yields EdgeTraverse rather than nodes, so it shouldn't inherit the other iterators' __next__. mypy and pyright now report no errors in the package. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Anders Kaseorg <andersk@mit.edu>
BaseNode._raw was untyped, so everything read from the raw node was Any, and type checkers couldn't catch mistakes in using it. Make BaseNode generic over the type of its raw node, with each node type a subclass of BaseNode[the corresponding _rustlib type]. Make it covariant, so that attach() and the iterators can accept any node as a BaseNode[_Node], and make _raw a read-only property so that's sound. Add a Node type alias for any node, which is what the navigation properties, the iterators, and TreeDom.__iter__ return, so that callers can narrow it with isinstance() to use a node type's attributes. Replace the registry of node classes that BaseNode._wrap() looked up, whose types couldn't be checked, with isinstance() checks. The raw nodes don't support ordering comparisons, so remove BaseNode's, which compared the raw nodes. Without them, comparing nodes raises TypeError as before, and Comment and Text, which compare their content with strings, return NotImplemented rather than deferring to them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Annotate the remaining functions, and QualName's constructor in the _rustlib stub, so that the package passes mypy --strict. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Anders Kaseorg <andersk@mit.edu>
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.
With a complete
_rustlibstub,mypyandpyrightreported these errors:_ConfigNode's invalid_ordering is a tuple of any length.BaseNode._SUBCLASS_WRAPneeds aClassVarannotation.TreeDom.__eq__must accept any object, asobject.__eq__does.create_elementaccepts aQualNamename, as the Rust constructor does.AttrsList's(key, value)pairs are typed as such, rather than as anytuple, which could have supplied thestartargument when unpacked.AttrsList.get's default ofNoneconflicted with its type; give it overloads._find_by_key's default argument was never passed.AttrsList.extendand__setitem__reassigned variables with values of different types._BASECLASSis called with a raw node, not aBaseNode.TraverseyieldsEdgeTraverserather than nodes, so it shouldn't inherit the other iterators'__next__.BaseNode._rawwas untyped, so everything read from the raw node wasAny, and type checkers couldn't catch mistakes in using it.mypy --strictandpyrightnow report no errors in the package.