Skip to content

More Features

KotlinIsland edited this page Mar 30, 2022 · 19 revisions

Legacy Mode (--legacy)

By default basedmypy is as strict as possible with all inspection options enabled. If you desire mypy's selection of strictness you can use the config option --legacy. While this sets the configuration options to match mypys, functionality is not identical, see the changelog for a comprehensive list. Baseline functionality is still active in legacy mode.
To disable the baseline, invoke basedmypy specifying the baseline file as an empty string: mypy --baseline-file= src

Default Return Type of None (default_return)

Causes an unannotated return type to be inferred as None.

def foo(i: int):
  print(i)

reveal_type(foo)  # revealed type is "def(int)"

It is recommended to only apply this option in the specific modules that will use it, as partially typed third party code can cause problems.

[[tool.mypy-overrides]]
module = ["mypackage.*"]
default_return = true

This plays conservatively with allow_untyped_defs and allow_incomplete_defs

# mypy: default-return, allow-incomplete-defs, allow-untyped-defs
def foo(): ... # def() -> Any
def bar(a, b): ... # def(Any, Any) -> Any
def foo(a: int, b): ... # def(int, Any) -> None

Ignore Unused Ignores (type: ignore[X, unused-ignore])

Often when dealing with multi-platform code, type ignores produce false positives:

import sys

if sys.platform != "linux":
  foo()  # type: ignore[misc]
> mypy .
main.py:4: error: Unused "type: ignore" comment
Found 1 error in 1 file (checked 1 source file)

In basedmypy you can add the ignore code unused-ignore to ignore the unused ignore error:

import sys

if sys.platform != "linux":
  foo()  # type: ignore[misc, unused-ignore]
> mypy .
Success: no issues found in 1 source file

Nonlocal Partial Types (--nonlocal-partial-types)

The --local-partial-types flag in mypy is confusing, so in basedmypy it's removed in favor of the inverse --nonlocal-partial-types

Clone this wiki locally