Description
In mypy we are proposing to implement strict Optional checking per PEP 484, with one exception.
The one exception will be in the case of instance variables defined in the class body. These variables may be assigned None while having a non-Optional type, provided that they are initialized to a non-None value by the end of
__init__
. They may be initialized in__init__
itself, or in any method on self that__init__
calls, or in__new__
. Mypy will not check these variables for use before assignment to a non-None value in__init__
or called methods, though it may make sense to add that at some point in the future.The purpose of this exception is to allow variables to be typed in a common place rather than scattered around
__init__
.
I think this is a good idea and we should update PEP 484 to follow this.