-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[ty] basic narrowing on attribute and subscript expressions #17643
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
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9212e03
[red-knot] basic narrowing on attribute and subscript expressions
mtshiba 9db9728
fix: use `try_scoped_use_id`
mtshiba e9d2776
Merge remote-tracking branch 'upstream/main' into attr-subscript-narr…
mtshiba 8a70f2a
add `PlaceExprWithFlags::{as_name, expect_name}`
mtshiba 06feb5c
use `constraint_keys` in attribute/subscript
mtshiba 0c8996c
use `ScopedPlaceId` as `NarrowingConstraints` key
mtshiba f29893a
Don't convert `ast::ExprNamed` with `PlaceExpr::try_from(ast::Expr)`
mtshiba f746730
add `PlaceExprRef`
mtshiba 0cabb63
remove `try_scoped_use_id`
mtshiba 72a97f6
refactor
mtshiba 20b76d0
Move the tornado package to bad.txt
mtshiba b6a7647
Update crates/ty_python_semantic/resources/mdtest/narrow/complex_targ…
mtshiba 1a69abc
refactor: `expr` -> `place`
mtshiba a58c9c0
Merge remote-tracking branch 'upstream/main' into attr-subscript-narr…
mtshiba 325230c
Add tests that combine narrowing and assignment to attributes
sharkdp d8ca65e
Update complex_target.md
mtshiba 991f437
Check attribute/subscript values at store time, not at load time
mtshiba 1292a5a
Fix the combination of conditional & assignment based narrowing to wo…
mtshiba 993a6eb
Merge remote-tracking branch 'upstream/main' into attr-subscript-narr…
mtshiba 96ed63b
Perform default type inference even if we can obtain the type based o…
mtshiba c5e9e15
Merge remote-tracking branch 'origin/main' into attr-subscript-narrowing
sharkdp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
209 changes: 209 additions & 0 deletions
209
crates/ty_python_semantic/resources/mdtest/narrow/complex_target.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
# Narrowing for complex targets (attribute expressions, subscripts) | ||
|
||
We support type narrowing for attributes and subscripts. | ||
|
||
## Attribute narrowing | ||
|
||
### Basic | ||
|
||
```py | ||
class C: | ||
x: int | None = None | ||
|
||
c = C() | ||
|
||
reveal_type(c.x) # revealed: int | None | ||
|
||
if c.x is not None: | ||
reveal_type(c.x) # revealed: int | ||
else: | ||
reveal_type(c.x) # revealed: None | ||
|
||
if c.x is not None: | ||
c.x = None | ||
|
||
reveal_type(c.x) # revealed: None | ||
|
||
c = C() | ||
|
||
if c.x is None: | ||
c.x = 1 | ||
|
||
reveal_type(c.x) # revealed: int | ||
|
||
class _: | ||
reveal_type(c.x) # revealed: int | ||
|
||
c = C() | ||
|
||
class _: | ||
if c.x is None: | ||
c.x = 1 | ||
reveal_type(c.x) # revealed: int | ||
|
||
# TODO: should be `int` | ||
reveal_type(c.x) # revealed: int | None | ||
``` | ||
|
||
Narrowing can be "reset" by assigning to the attribute: | ||
|
||
```py | ||
c = C() | ||
|
||
if c.x is None: | ||
reveal_type(c.x) # revealed: None | ||
c.x = 1 | ||
reveal_type(c.x) # revealed: Literal[1] | ||
c.x = None | ||
reveal_type(c.x) # revealed: None | ||
|
||
reveal_type(c.x) # revealed: int | None | ||
``` | ||
|
||
Narrowing can also be "reset" by assigning to the object: | ||
|
||
```py | ||
c = C() | ||
|
||
if c.x is None: | ||
reveal_type(c.x) # revealed: None | ||
c = C() | ||
reveal_type(c.x) # revealed: int | None | ||
|
||
reveal_type(c.x) # revealed: int | None | ||
``` | ||
|
||
### Multiple predicates | ||
|
||
```py | ||
class C: | ||
value: str | None | ||
|
||
def foo(c: C): | ||
if c.value and len(c.value): | ||
reveal_type(c.value) # revealed: str & ~AlwaysFalsy | ||
|
||
# error: [invalid-argument-type] "Argument to function `len` is incorrect: Expected `Sized`, found `str | None`" | ||
if len(c.value) and c.value: | ||
reveal_type(c.value) # revealed: str & ~AlwaysFalsy | ||
|
||
if c.value is None or not len(c.value): | ||
reveal_type(c.value) # revealed: str | None | ||
else: # c.value is not None and len(c.value) | ||
# TODO: should be # `str & ~AlwaysFalsy` | ||
reveal_type(c.value) # revealed: str | ||
``` | ||
|
||
### Generic class | ||
|
||
```toml | ||
[environment] | ||
python-version = "3.12" | ||
``` | ||
|
||
```py | ||
class C[T]: | ||
x: T | ||
y: T | ||
|
||
def __init__(self, x: T): | ||
self.x = x | ||
self.y = x | ||
|
||
def f(a: int | None): | ||
c = C(a) | ||
reveal_type(c.x) # revealed: int | None | ||
reveal_type(c.y) # revealed: int | None | ||
if c.x is not None: | ||
reveal_type(c.x) # revealed: int | ||
# In this case, it may seem like we can narrow it down to `int`, | ||
# but different values may be reassigned to `x` and `y` in another place. | ||
reveal_type(c.y) # revealed: int | None | ||
|
||
def g[T](c: C[T]): | ||
reveal_type(c.x) # revealed: T | ||
reveal_type(c.y) # revealed: T | ||
reveal_type(c) # revealed: C[T] | ||
|
||
if isinstance(c.x, int): | ||
reveal_type(c.x) # revealed: T & int | ||
reveal_type(c.y) # revealed: T | ||
reveal_type(c) # revealed: C[T] | ||
if isinstance(c.x, int) and isinstance(c.y, int): | ||
reveal_type(c.x) # revealed: T & int | ||
reveal_type(c.y) # revealed: T & int | ||
# TODO: Probably better if inferred as `C[T & int]` (mypy and pyright don't support this) | ||
reveal_type(c) # revealed: C[T] | ||
``` | ||
|
||
### With intermediate scopes | ||
|
||
```py | ||
class C: | ||
def __init__(self): | ||
self.x: int | None = None | ||
self.y: int | None = None | ||
|
||
c = C() | ||
reveal_type(c.x) # revealed: int | None | ||
if c.x is not None: | ||
reveal_type(c.x) # revealed: int | ||
reveal_type(c.y) # revealed: int | None | ||
|
||
if c.x is not None: | ||
def _(): | ||
reveal_type(c.x) # revealed: Unknown | int | None | ||
|
||
def _(): | ||
if c.x is not None: | ||
reveal_type(c.x) # revealed: (Unknown & ~None) | int | ||
``` | ||
|
||
## Subscript narrowing | ||
|
||
### Number subscript | ||
|
||
```py | ||
def _(t1: tuple[int | None, int | None], t2: tuple[int, int] | tuple[None, None]): | ||
if t1[0] is not None: | ||
reveal_type(t1[0]) # revealed: int | ||
reveal_type(t1[1]) # revealed: int | None | ||
|
||
n = 0 | ||
if t1[n] is not None: | ||
# Non-literal subscript narrowing are currently not supported, as well as mypy, pyright | ||
reveal_type(t1[0]) # revealed: int | None | ||
reveal_type(t1[n]) # revealed: int | None | ||
reveal_type(t1[1]) # revealed: int | None | ||
|
||
if t2[0] is not None: | ||
# TODO: should be int | ||
reveal_type(t2[0]) # revealed: Unknown & ~None | ||
# TODO: should be int | ||
reveal_type(t2[1]) # revealed: Unknown | ||
``` | ||
|
||
### String subscript | ||
|
||
```py | ||
def _(d: dict[str, str | None]): | ||
if d["a"] is not None: | ||
reveal_type(d["a"]) # revealed: str | ||
reveal_type(d["b"]) # revealed: str | None | ||
``` | ||
|
||
## Combined attribute and subscript narrowing | ||
|
||
```py | ||
class C: | ||
def __init__(self): | ||
self.x: tuple[int | None, int | None] = (None, None) | ||
|
||
class D: | ||
def __init__(self): | ||
self.c: tuple[C] | None = None | ||
|
||
d = D() | ||
if d.c is not None and d.c[0].x[0] is not None: | ||
reveal_type(d.c[0].x[0]) # revealed: int | ||
``` |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,7 +109,6 @@ stone | |
strawberry | ||
streamlit | ||
sympy | ||
tornado | ||
trio | ||
twine | ||
typeshed-stats | ||
|
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.