-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Improve isinstance() truthiness analysis for generic types
#19668
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
Conversation
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
Contributor
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
Contributor
|
isinstance() reachability analysis for generic typesisinstance() truthiness analysis for generic types
20cd78f to
83ea2df
Compare
sharkdp
approved these changes
Aug 1, 2025
Contributor
sharkdp
left a comment
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.
Looks good — Thank you!
dcreager
added a commit
that referenced
this pull request
Aug 1, 2025
* main: (39 commits) [ty] Initial test suite for `TypedDict` (#19686) [ty] Improve debuggability of protocol types (#19662) [ty] Simplify lifetime requirements for `PySlice` trait (#19687) [ty] Improve `isinstance()` truthiness analysis for generic types (#19668) [`refurb`] Make example error out-of-the-box (`FURB164`) (#19673) Fix link: unused_import.rs (#19648) [ty] Remove `Specialization::display` (full) (#19682) [ty] Remove `KnownModule::is_enum` (#19681) [ty] Support `__setitem__` and improve `__getitem__` related diagnostics (#19578) [ty] Sync vendored typeshed stubs (#19676) [`flake8-use-pathlib`] Expand `PTH201` to check all `PurePath` subclasses (#19440) [`refurb`] Make example error out-of-the-box (`FURB180`) (#19672) [`pyupgrade`] Prevent infinite loop with `I002` (`UP010`, `UP035`) (#19413) [ty] Improve the `Display` for generic `type[]` types (#19667) [ty] Refactor `TypeInferenceBuilder::infer_subscript_expression_types` (#19658) Fix tests on 32-bit architectures (#19652) [ty] Move `pandas-stubs` to bad.txt (#19659) [ty] Remove special casing for string-literal-in-tuple `__contains__` (#19642) Update pre-commit's `ruff` id (#19654) Update salsa (#19449) ...
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
This PR extends the
isinstance()truthiness inference (added in #19503) so that it works better for instances of generic types. Specifically, we now inferLiteral[True]here, whereas onmainwe inferbool:The motivation for improving our logic here is that the logic on
mainbreaks for tuples if we remove theType::Tuplevariant and treat them simply as instances of a (very special) generic class. I.e., we have tests asserting that we inferisinstance((1, 2), tuple)asLiteral[True]; those break on the branch for #19669 without this change.I've split this out into a separate PR from #19669 as it seems like a useful change on its own merits, however. We can see from the ecosystem report that it gets rid of two false positives from
bokeh: we now correctly infer two branches of code as unreachable, and therefore do not emit anyinvalid-return-typediagnostics on those branches anymore. (See https://github.com/bokeh/bokeh/blob/adef0157284696ce86961b2089c75fddda53c15c/src/bokeh/core/property/container.py#L130-L140.)I can't quite figure out what's going on in the sympy ecosystem hit (https://github.com/sympy/sympy/blob/3c817ed8ab551d71e36907ce869afe5e4ca789ff/sympy/polys/polyclasses.py#L308-L317) -- it seems like we used to infer the type of
gasDMP[Es] & DMP[Unknown]at the point of thereturnstatement, whereas we now infer the type ofgasDMP[Es]. I don't quite understand why we now get to that answer; but still, our new inference seems better here -- intersecting withDMP[Unknown]as a result of theisinstance()check in that function is incorrect (astral-sh/ty#456).Test Plan
I added mdtests for the direct functionality being added, and for the false positives that this PR removes from
bokeh. I also extended the reachability tests added in #19503 to include some examples of generic types. Most of these tests currently fail, so there are many TODO comments added -- they require astral-sh/ty#456 for them to be fixed. If this is merged, I'll add a comment to that issue saying that we now have some failing tests onmainthat should naturally be fixed if and when that issue is fixed.