[ty] Fix false positives when subscripting an object inferred as having an Intersection type#18920
Merged
AlexWaygood merged 3 commits intomainfrom Jun 24, 2025
Merged
[ty] Fix false positives when subscripting an object inferred as having an Intersection type#18920AlexWaygood merged 3 commits intomainfrom
Intersection type#18920AlexWaygood merged 3 commits intomainfrom
Conversation
a479576 to
a1f0673
Compare
carljm
approved these changes
Jun 24, 2025
| ) | ||
| .build(), | ||
| (Type::Intersection(_), _, _) => { | ||
| todo_type!("Subscript expressions on intersections") |
Contributor
There was a problem hiding this comment.
Suggested change
| todo_type!("Subscript expressions on intersections") | |
| // TODO: we can map over the intersection and fold the results back into an intersection, | |
| // but we need to ignore any errors, which means `infer_subscript_expression_types` | |
| // needs to return a `Result` rather than eagerly emitting diagnostics. | |
| todo_type!("Subscript expressions on intersections") |
Member
Author
There was a problem hiding this comment.
"Ignore any errors" is something I might have to think about a bit... what about something like Foo & Bar here?
class Foo:
__getitem__ = None
class Bar:
def __getitem__(self, key) -> int:
return 42(Foo & Bar).__getitem__ simplifies to Never, I suppose, so if you attempt a subscript operation on an object of type Foo & Bar... I suppose ideally we should emit a warning about unreachable code but avoid any errors about the implicit call to __getitem__?
We definitely need to avoid any errors to do with missing methods if any element has the method, though
Member
Author
There was a problem hiding this comment.
I added a slightly more qualified comment in 2af7148
Contributor
|
dcreager
added a commit
that referenced
this pull request
Jun 24, 2025
* main: [ty] Fix false positives when subscripting an object inferred as having an `Intersection` type (#18920) [`flake8-use-pathlib`] Add autofix for `PTH202` (#18763) [ty] Add relative import completion tests [ty] Clarify what "cursor" means [ty] Add a cursor test builder [ty] Enforce sort order of completions (#18917) [formatter] Fix missing blank lines before decorated classes in .pyi files (#18888) Apply fix availability and applicability when adding to `DiagnosticGuard` and remove `NoqaCode::rule` (#18834) py-fuzzer: allow relative executable paths (#18915) [ty] Change `environment.root` to accept multiple paths (#18913) [ty] Rename `src.root` setting to `environment.root` (#18760) Use file path for detecting package root (#18914) Consider virtual path for various server actions (#18910) [ty] Introduce `UnionType::try_from_elements` and `UnionType::try_map` (#18911) [ty] Support narrowing on `isinstance()`/`issubclass()` if the second argument is a dynamic, intersection, union or typevar type (#18900) [ty] Add decorator check for implicit attribute assignments (#18587) [`ruff`] Trigger `RUF037` for empty string and byte strings (#18862) [ty] Avoid duplicate diagnostic in unpacking (#18897) [`pyupgrade`] Extend version detection to include `sys.version_info.major` (`UP036`) (#18633) [`ruff`] Frozen Dataclass default should be valid (`RUF009`) (#18735)
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.
Summary
This fixes a regression introduced in #18846. Following that PR, ty will now emit a false-positive diagnostic on code like this:
But we should avoid emitting a diagnostic if the subscript operation would succeed for any element of the intersection, even if it would fail for some other elements in the intersection.
Fixing this properly will probably involve some tricky refactoring, so for now this is just a hotfix that gets rid of the false-positive diagnostic. (I'm adding the "internal" label because the bug being fixed here hasn't appeared in any release of ty.)
Test Plan
Added mdtests