Miscellaneous strictness flags
This section documents any other flags that do not neatly fall under anyof the above sections.
—allow-untyped-globals
- This flag causes mypy to suppress errors caused by not being able to fullyinfer the types of global and class variables.
—allow-redefinition
- By default, mypy won’t allow a variable to be redefined with anunrelated type. This flag enables redefinion of a variable with anarbitrary type in some contexts: only redefinitions within thesame block and nesting depth as the original definition are allowed.Example where this can be useful:
- def process(items: List[str]) -> None:
- # 'items' has type List[str]
- items = [item.split() for item in items]
- # 'items' now has type List[List[str]]
- ...
—local-partial-types
- In mypy, the most common cases for partial types are variables initialized using
None
,but without explicitOptional
annotations. By default, mypy won’t check partial typesspanning module top level or class top level. This flag changes the behavior to only allowpartial types at local level, therefore it disallows inferring variable type forNone
from two assignments in different scopes. For example:
- from typing import Optional
- a = None # Need type annotation here if using --local-partial-types
- b = None # type: Optional[int]
- class Foo:
- bar = None # Need type annotation here if using --local-partial-types
- baz = None # type: Optional[int]
- def __init__(self) -> None:
- self.bar = 1
- reveal_type(Foo().bar) # Union[int, None] without --local-partial-types
Note: this option is always implicitly enabled in mypy daemon and will becomeenabled by default for mypy in a future release.
—no-implicit-reexport
- By default, imported values to a module are treated as exported and mypy allowsother modules to import them. This flag changes the behavior to not re-export unlessthe item is imported using from-as or is included in
all
. Note this isalways treated as enabled for stub files. For example:
- # This won't re-export the value
- from foo import bar
- # This will re-export it as bar and allow other modules to import it
- from foo import bar as bar
- # This will also re-export bar
- from foo import bar
- __all__ = ['bar']
—strict-equality
- By default, mypy allows always-false comparisons like
42 == 'no'
.Use this flag to prohibit such comparisons of non-overlapping types, andsimilar identity and container checks:
- from typing import List, Text
- items: List[int]
- if 'some string' in items: # Error: non-overlapping container check!
- ...
- text: Text
- if text != b'other bytes': # Error: non-overlapping equality check!
- ...
- assert text is not None # OK, check against None is allowed as a special case.
—strict
- This flag mode enables all optional error checking flags. You can see thelist of flags enabled by strict mode in the full
mypy —help
output.
Note: the exact list of flags enabled by running —strict
may changeover time.