None and Optional handling
The following flags adjust how mypy handles values of type None
.For more details, see Disabling strict optional checking.
—no-implicit-optional
- This flag causes mypy to stop treating arguments with a
None
default value as having an implicitOptional
type.
For example, by default mypy will assume that the x
parameteris of type Optional[int]
in the code snippet below sincethe default parameter is None
:
- def foo(x: int = None) -> None:
- print(x)
If this flag is set, the above snippet will no longer type check:we must now explicitly indicate that the type is Optional[int]
:
- def foo(x: Optional[int] = None) -> None:
- print(x)
—no-strict-optional
- This flag disables strict checking of
Optional
types andNone
values. With this option, mypy doesn’tgenerally check the use ofNone
values – they are valideverywhere. See Disabling strict optional checking for more about this feature.
Note: Strict optional checking was enabled by default starting inmypy 0.600, and in previous versions it had to be explicitly enabledusing —strict-optional
(which is still accepted).