Open
Description
Feature
foo = None
def bar() -> str:
global foo
if foo is not None:
print("Hello")
else:
foo = "bar"
return "lol"
This program type-checks, even though it's obviously wrong. It's not a bug, it's a feature:
- Inferred type of
foo
isNone
, even thoughOptional[str]
was intended. - Because
foo
has typeNone
, theif foo is not None:
part is eliminated as dead code, and therefore the missing return isn't detected. - After setting
foo = "bar"
,reveal_type(foo)
showsstr
. Perhaps the assignment should be an error?
Pitch
A friend submitted a PR like this and thought it was fine.