"Cannot determine type" if first assignment to attribute is unreachable #9709
Labels
bug
mypy got something wrong
topic-inference
When to infer types or require explicit annotations
topic-reachability
Detecting unreachable code
To Reproduce
Run mypy on the following code:
It will output:
Line 6 contains the second assignment to
self.x
.Expected Behavior
mypy should be able to determine that the type of
C.x
isint
.Actual Behavior
The fact that
a
is annotated asstr
means that mypy determines thata is None
is never true and therefore the first assignment toself.x
is unreachable. The problem disappears ifa
is annotated asOptional[str]
.So it looks like mypy half-ignores the first assignment to
self.x
: it doesn't use the type information available there since the code is unreachable, but it still considers it the assignment that determines the type ofself.x
.Your Environment
mypy 0.790 with default configuration, running on Python 3.8.5 on openSUSE Linux.
The text was updated successfully, but these errors were encountered: