Open
Description
Describe the Bug
From https://microsoft.github.io/pyright/#/type-concepts-advanced?id=type-guards
Type guards of the form x.y
or x[y]
are all partially-supported: we currently apply the narrowing to the type of x[y]
, but do not narrow the type of x
(if it's something that can be discriminated by that field/index/key)
-
x is None
/x is not None
-
x == None
/x != None
-
x is ...
/x is not ...
/x == ...
/x != ...
(where ... is an ellipsis token) Note: as far as I can tell, this is just for narrowingobject
orAny
toEllipsis
-
x is S
/x is not S
(where S is a Sentinel) Note: Sentinel here seems to refer to someFinal
variable - even ifS
is not a literal, if it is final we can use it for type narrowing. -
type(x) is T
/type(x) is not T
Type narrowing viaassert type(x) is X
andassert type(x) == X
#335 -
type(x) == T
/type(x) != T
Type narrowing viaassert type(x) is X
andassert type(x) == X
#335 -
x is L
/x is not L
(where L is an expression that evaluates to a literal type) -
x is C
/x is not C
(where C is a class) -
x == L
/x != L
(where L is an expression that evaluates to a literal type) -
x.y is None
/x.y is not None
(where x is a type that is distinguished by a field with a None) -
x.y is E
/x.y is not E
(where E is a literal enum or bool and x is a type that is distinguished by a field with a literal type) [feature] Discriminated Union (narrow typed dicts based on field type) #418 -
x.y == LN
/x.y != LN
(where LN is a literal expression or None and x is a type that is distinguished by a field or property with a literal type) [feature] Discriminated Union (narrow typed dicts based on field type) #418 -
x[K] == V
/x[K] != V
/x[K] is V
/x[K] is not V
(where K and V are literal expressions and x is a type that is distinguished by a TypedDict field with a literal type) [feature] Discriminated Union (narrow typed dicts based on field type) #418 -
x[I] == V
/x[I] != V
(where I and V are literal expressions and x is a known-length tuple that is distinguished by the index indicated by I) [feature] Discriminated Union (narrow typed dicts based on field type) #418 -
x[I] is B
/x[I] is not B
(where I is a literal expression, B is a bool or enum literal, and x is a known-length tuple that is distinguished by the index indicated by I) [feature] Discriminated Union (narrow typed dicts based on field type) #418 -
x[I] is None
/x[I] is not None
(where I is a literal expression and x is a known-length tuple that is distinguished by the index indicated by I) [feature] Discriminated Union (narrow typed dicts based on field type) #418 -
len(x) == L
,len(x) != L
,len(x) < L
, etc. (where x is tuple and L is an expression that evaluates to an int literal type) -
x in y
/x not in y
(where y is instance of list, set, frozenset, deque, tuple, dict, defaultdict, or OrderedDict) -
S in D
/S not in D
(where S is a string literal and D is a TypedDict) TypedDict NotRequired key narrowing #84 -
isinstance(x, T)
(where T is a type or a tuple of types) -
issubclass(x, T)
(where T is a type or a tuple of types) -
f(x)
(where f is a user-defined type guard as defined in PEP 647 or PEP 742) -
bool(x)
(where x is any expression that is statically verifiable to be truthy or falsey in all cases) -
x
(where x is any expression that is statically known to be truthy or falsy)
Sandbox Link
No response
(Only applicable for extension issues) IDE Information
No response