-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
bugmypy got something wrongmypy got something wrongpriority-0-hightopic-reachabilityDetecting unreachable codeDetecting unreachable codetopic-runtime-semanticsmypy doesn't model runtime semantics correctlymypy doesn't model runtime semantics correctlytopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder
Description
Here is a minimal program demonstrating the problem:
foo = None
for _ in range(2):
if foo is not None:
print(1[2])
foo = 1
Expected behavior:
error: Value of type "int" is not indexable
Actual behavior:
mypy accepts the program as valid, when run with the --strict flag
This issue seems to be a problem with how mypy handles reassignment to None type variables, and static evaluation of conditionals. After line 1, foo
has the type None according to mypy. Thus, mypy determines that the condition on line 3 will never hold, and thus doesn't do type checking for the body of the if statement. On line 5, foo's type changes to Optional[int]
. This, however, will only be reflected in code that follows line 5.
JosiahKane, unfazed-unbothered-ataraxed, r-owen and hauntsaninja
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongpriority-0-hightopic-reachabilityDetecting unreachable codeDetecting unreachable codetopic-runtime-semanticsmypy doesn't model runtime semantics correctlymypy doesn't model runtime semantics correctlytopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder