-
Notifications
You must be signed in to change notification settings - Fork 229
Open
Labels
narrowingrelated to flow-sensitive type narrowingrelated to flow-sensitive type narrowing
Description
Summary
In the following code, g in the class scope D should be inferred as str | None, but is instead inferred as str.
g: str | None = None
def f(flag: bool):
class C:
if flag:
g = 1
if g is not None: # this `g` may refer to a class variable, or a global variable
class D:
# refers to the global `g`
reveal_type(g) # should be: str | NoneHere, it appears that we have mistakenly applied the narrowing constraint g is not None to the global variable g.
However, simply checking the symbol table and not recording an eager snapshot if the class variable C.g is bound is not sufficient. In the following example, we can safely narrow the global g because we can assume that C.g is undefined in class D.
g: str | None = None
def f(flag: bool):
class C:
if flag:
g = None
if g is not None: # if this evaluates to `True`, `g` must refer to a global variable
class D:
reveal_type(g) # revealed: strThis is already checked in mdtest/narrow/conditionals/nested.md.
Version
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
narrowingrelated to flow-sensitive type narrowingrelated to flow-sensitive type narrowing