Skip to content

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

Merged
merged 3 commits into from
Dec 16, 2024
Merged

Fix todo in inspect #13129

merged 3 commits into from
Dec 16, 2024

Conversation

kbaikov
Copy link
Contributor

@kbaikov kbaikov commented Nov 26, 2024

latest mypy seems to accept it.

This comment has been minimized.

# 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, ...]]
Copy link
Member

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:

But, the list contains tuple items, see https://github.com/python/cpython/blob/71ede1142ddad2d31cc966b8fe4a5aff664f4d53/Lib/inspect.py#L1161

This comment has been minimized.

# _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, ...]]
Copy link
Member

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:

Suggested change
_ClassTreeItem: TypeAlias = list[_ClassTreeItem] | list[tuple[type, ...]]
_ClassTreeItem: TypeAlias = list[tuple[type, ...]] | list[_ClassTreeItem]

Since this type is recursive, it better reads this way.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Member

@sobolevn sobolevn left a 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'>,))]]

@sobolevn sobolevn merged commit 54e1c6a into python:main Dec 16, 2024
63 checks passed
@kbaikov kbaikov deleted the fix-inspect branch December 16, 2024 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants