[ty] more cases for the class body global fallback
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

This commit is contained in:
Jack O'Connor 2025-08-06 11:14:20 -07:00
parent 462adfd0e6
commit 827456f977
5 changed files with 69 additions and 34 deletions

View file

@ -75,7 +75,7 @@ class _:
if cond():
a = A()
reveal_type(a.x) # revealed: int | None
reveal_type(a.x) # revealed: int | None | Unknown
reveal_type(a.y) # revealed: Unknown | None
reveal_type(a.z) # revealed: Unknown | None

View file

@ -269,6 +269,8 @@ If we try to access a variable in a class before it has been defined, the lookup
global.
```py
import secrets
x: str = "a"
def f(x: int, y: int):
@ -286,4 +288,23 @@ def f(x: int, y: int):
# error: [unresolved-reference]
reveal_type(y) # revealed: Unknown
y = None
# Declarations count as definitions, even if there's no binding.
class F:
reveal_type(x) # revealed: str
x: int
reveal_type(x) # revealed: str
# Explicitly `nonlocal` variables don't count, even if they're bound.
class G:
nonlocal x
reveal_type(x) # revealed: int
x = 42
reveal_type(x) # revealed: Literal[42]
# Possibly-unbound variables get unioned with the fallback lookup.
class H:
if secrets.randbelow(2):
x = None
reveal_type(x) # revealed: None | str
```

View file

@ -34,7 +34,7 @@ class C:
if coinflip():
x = 1
# error: [possibly-unresolved-reference]
# Possibly unbound variables in enclosing scopes are considered bound.
y = x
reveal_type(C.y) # revealed: Unknown | Literal[1, "abc"]