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:
  1. def process(items: List[str]) -> None:
  2. # 'items' has type List[str]
  3. items = [item.split() for item in items]
  4. # 'items' now has type List[List[str]]
  5. ...
  • —local-partial-types
  • In mypy, the most common cases for partial types are variables initialized using None,but without explicit Optional 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 for Nonefrom two assignments in different scopes. For example:
  1. from typing import Optional
  2.  
  3. a = None # Need type annotation here if using --local-partial-types
  4. b = None # type: Optional[int]
  5.  
  6. class Foo:
  7. bar = None # Need type annotation here if using --local-partial-types
  8. baz = None # type: Optional[int]
  9.  
  10. def __init__(self) -> None:
  11. self.bar = 1
  12.  
  13. 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:
  1. # This won't re-export the value
  2. from foo import bar
  3. # This will re-export it as bar and allow other modules to import it
  4. from foo import bar as bar
  5. # This will also re-export bar
  6. from foo import bar
  7. __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:
  1. from typing import List, Text
  2.  
  3. items: List[int]
  4. if 'some string' in items: # Error: non-overlapping container check!
  5. ...
  6.  
  7. text: Text
  8. if text != b'other bytes': # Error: non-overlapping equality check!
  9. ...
  10.  
  11. 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.