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 is None, even though Optional[str] was intended.
- Because
foo has type None, the if 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) shows str. Perhaps the assignment should be an error?
Pitch
A friend submitted a PR like this and thought it was fine.
Feature
This program type-checks, even though it's obviously wrong. It's not a bug, it's a feature:
fooisNone, even thoughOptional[str]was intended.foohas typeNone, theif foo is not None:part is eliminated as dead code, and therefore the missing return isn't detected.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.