mypy
56b68032 - Support making a variable Optional in an else branch (#11002)

Commit
4 years ago
Support making a variable Optional in an else branch (#11002) That is, support patterns such as: ``` if condition: foo = Foo() else: foo = None ``` Currently this does not work, but the the *reverse* does (because foo will be inferred as a PartialType). I think it might be worth tackling this in a more general way, for other types, though I think that is a little fiddlier and likely to be more controversial, so I'm starting with something special-cased for the "assigning literal None" case first. The rule we implement is that we allow updating the type of a variable when assigning `None` to it if the variable's type was inferred and it was defined in an earlier branch of the same `if/then/else` statement. Some infrastructure is added to make determinations about that.
Author
Parents
Loading