mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
[ty] fix lazy snapshot sweeping in nested scopes (#19908)
## Summary This PR closes astral-sh/ty#955. ## Test Plan New test cases in `narrowing/conditionals/nested.md`.
This commit is contained in:
parent
957320c0f1
commit
0e5577ab56
3 changed files with 56 additions and 23 deletions
|
@ -240,6 +240,21 @@ def f(x: str | None):
|
|||
|
||||
# When there is a reassignment, any narrowing constraints on the place are invalidated in lazy scopes.
|
||||
x = None
|
||||
|
||||
def f(x: str | None):
|
||||
def _():
|
||||
if x is not None:
|
||||
def closure():
|
||||
reveal_type(x) # revealed: str | None
|
||||
x = None
|
||||
|
||||
def f(x: str | None):
|
||||
class C:
|
||||
def _():
|
||||
if x is not None:
|
||||
def closure():
|
||||
reveal_type(x) # revealed: str
|
||||
x = None # This assignment is not visible in the inner lazy scope, so narrowing is still valid.
|
||||
```
|
||||
|
||||
If a variable defined in a private scope is never reassigned, narrowing remains in effect in the
|
||||
|
@ -256,6 +271,12 @@ def f(const: str | None):
|
|||
reveal_type(const) # revealed: str
|
||||
|
||||
[reveal_type(const) for _ in range(1)] # revealed: str
|
||||
|
||||
def f(const: str | None):
|
||||
def _():
|
||||
if const is not None:
|
||||
def closure():
|
||||
reveal_type(const) # revealed: str
|
||||
```
|
||||
|
||||
And even if there is an attribute or subscript assignment to the variable, narrowing of the variable
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue