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 Nonedefault value as having an implicit Optional 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:

  1. def foo(x: int = None) -> None:
  2. 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]:

  1. def foo(x: Optional[int] = None) -> None:
  2. print(x)
  • —no-strict-optional
  • This flag disables strict checking of Optionaltypes and None values. With this option, mypy doesn’tgenerally check the use of None 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).