Improve the signature of type.__subclasses__ - #16299
Merged
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
AlexWaygood
marked this pull request as ready for review
August 25, 2026 10:43
Collaborator
|
Could we add a brief test here that this infers correctly for the type checkers supported by our tests? |
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
JelleZijlstra
approved these changes
Aug 25, 2026
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.
ty currently infers the types of
object.__subclasses__()aslist[<class 'object'>], AKA, "a list in which all contents of the list are the classobject". This is obviously suboptimal:objectclearly has more subclasses than just itself.All other type checkers infer the type of
object.__subclasses__()as eitherlist[type[object]](pyright, pyrefly) orlist[Callable[[], object]](mypy), because no other type checker has the concept of "class-literal types" the same way that ty does. (A "class-literal" type like<class 'object'>means "exactly the classobject, not including subclasses", unliketype[object]which means "the classobject, or any subclass".) But ty's behaviour arguably isn't incorrect here: it's a valid solution to the type variable in typeshed's signature as it is right now.With the changes made in this PR, mypy, ty, pyright and pyrefly all infer the type of
int.__subclasses__()aslist[type[int]].Fixes astral-sh/ty#4386. Credit to @sharkdp for the idea of how to fix this.