-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Reachability and narrowing for enum methods #21130
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
Conversation
4d39b75 to
2946e79
Compare
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
2946e79 to
6a9bc2e
Compare
|
| 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
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.
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
* 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) ...
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