-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix todo in inspect #13129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix todo in inspect #13129
Conversation
This comment has been minimized.
This comment has been minimized.
stdlib/inspect.pyi
Outdated
| # seem to be supporting this at the moment: | ||
| # _ClassTreeItem = list[_ClassTreeItem] | Tuple[type, Tuple[type, ...]] | ||
| def getclasstree(classes: list[type], unique: bool = False) -> list[Any]: ... | ||
| _ClassTreeItem: TypeAlias = list[_ClassTreeItem] | tuple[type, tuple[type, ...]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that it ever returns a tuple. See:
walktreewhich is called ingetclasstreeLine 428 in 693add9
def walktree(classes: list[type], children: Mapping[type[Any], list[type]], parent: type[Any] | None) -> list[Any]: ... - source of
walktreehttps://github.com/python/cpython/blob/71ede1142ddad2d31cc966b8fe4a5aff664f4d53/Lib/inspect.py#L1158-L1164
But, the list contains tuple items, see https://github.com/python/cpython/blob/71ede1142ddad2d31cc966b8fe4a5aff664f4d53/Lib/inspect.py#L1161
This comment has been minimized.
This comment has been minimized.
stdlib/inspect.pyi
Outdated
| # _ClassTreeItem = list[_ClassTreeItem] | Tuple[type, Tuple[type, ...]] | ||
| def getclasstree(classes: list[type], unique: bool = False) -> list[Any]: ... | ||
| def walktree(classes: list[type], children: Mapping[type[Any], list[type]], parent: type[Any] | None) -> list[Any]: ... | ||
| _ClassTreeItem: TypeAlias = list[_ClassTreeItem] | list[tuple[type, ...]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to write it other way around:
| _ClassTreeItem: TypeAlias = list[_ClassTreeItem] | list[tuple[type, ...]] | |
| _ClassTreeItem: TypeAlias = list[tuple[type, ...]] | list[_ClassTreeItem] |
Since this type is recursive, it better reads this way.
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After further testing this looks correct to me:
>>> import inspect
>>> inspect.getclasstree([int])
[(<class 'object'>, ()), [(<class 'int'>, (<class 'object'>,))]]
>>> inspect.getclasstree([int, list])
[(<class 'object'>, ()), [(<class 'int'>, (<class 'object'>,)), (<class 'list'>, (<class 'object'>,))]]
>>> class A: ...
...
>>> class B(A): ...
...
>>> class C(B): ...
...
>>> inspect.getclasstree([int, B])
[(<class '__main__.A'>, (<class 'object'>,)), [(<class '__main__.B'>, (<class '__main__.A'>,))], (<class 'object'>, ()), [(<class 'int'>, (<class 'object'>,))]]
>>> inspect.getclasstree([int, str, B])
[(<class '__main__.A'>, (<class 'object'>,)), [(<class '__main__.B'>, (<class '__main__.A'>,))], (<class 'object'>, ()), [(<class 'int'>, (<class 'object'>,)), (<class 'str'>, (<class 'object'>,))]]
latest mypy seems to accept it.