[ty] Reachability and narrowing for enum methods#21130
Merged
Conversation
4d39b75 to
2946e79
Compare
Contributor
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
Contributor
|
2946e79 to
6a9bc2e
Compare
AlexWaygood
reviewed
Oct 30, 2025
Contributor
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-return-type |
0 | 1 | 0 |
type-assertion-failure |
0 | 1 | 0 |
| Total | 0 | 2 | 0 |
6a9bc2e to
236427f
Compare
AlexWaygood
reviewed
Oct 30, 2025
AlexWaygood
reviewed
Oct 30, 2025
AlexWaygood
reviewed
Oct 30, 2025
Member
AlexWaygood
left a comment
There was a problem hiding this comment.
This fixes the issue for methods on enums, but there appear to be lots of other cases where you can have a TypeVar bound to a union (or type equivalent to a union) where we also exhibit similar issues with reachability and narrowing. These are all on this branch:
from typing import assert_never, Literal
def f[T: bool](x: T) -> T:
match x:
case True:
return x
case False:
return x
case _:
reveal_type(x) # T@f & ~Literal[True] & ~Literal[False]
assert_never(x) # false-positive error
def g[T: Literal["foo", "bar"]](x: T) -> T:
match x:
case "foo":
return x
case "bar":
return x
case _:
reveal_type(x) # T@g & ~Literal["foo"] & ~Literal["bar"]
assert_never(x) # false-positive error
def h[T: int | str](x: T) -> T:
if isinstance(x, int):
return x
elif isinstance(x, str):
return x
else:
reveal_type(x) # T@h & ~int & ~str
assert_never(x) # false-positive error
crates/ty_python_semantic/src/semantic_index/reachability_constraints.rs
Outdated
Show resolved
Hide resolved
236427f to
53b79d8
Compare
AlexWaygood
approved these changes
Oct 30, 2025
dcreager
added a commit
that referenced
this pull request
Oct 30, 2025
* origin/main: (21 commits) [ty] Update "constraint implication" relation to work on constraints between two typevars (#21068) [`flake8-type-checking`] Fix `TC003` false positive with `future-annotations` (#21125) [ty] Fix lookup of `__new__` on instances (#21147) Fix syntax error false positive on nested alternative patterns (#21104) [`pyupgrade`] Fix false positive for `TypeVar` with default on Python <3.13 (`UP046`,`UP047`) (#21045) [ty] Reachability and narrowing for enum methods (#21130) [ty] Use `range` instead of custom `IntIterable` (#21138) [`ruff`] Add support for additional eager conversion patterns (`RUF065`) (#20657) [`ruff-ecosystem`] Fix CLI crash on Python 3.14 (#21092) [ty] Infer type of `self` for decorated methods and properties (#21123) [`flake8-bandit`] Fix correct example for `S308` (#21128) [ty] Dont provide goto definition for definitions which are not reexported in builtins (#21127) [`airflow`] warning `airflow....DAG.create_dagrun` has been removed (`AIR301`) (#21093) [ty] follow the breaking API changes made in salsa-rs/salsa#1015 (#21117) [ty] Rename `Type::into_nominal_instance` (#21124) [ty] Filter out "unimported" from the current module [ty] Add evaluation test for auto-import including symbols in current module [ty] Refactor `ty_ide` completion tests [ty] Render `import <...>` in completions when "label details" isn't supported [`refurb`] Preserve digit separators in `Decimal` constructor (`FURB157`) (#20588) ...
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
Adds proper type narrowing and reachability analysis for matching on non-inferable type variables bound to enums. For example:
closes astral-sh/ty#1404
Test Plan
Added regression tests