Skip to content

Baseline

KotlinIsland edited this page Mar 30, 2022 · 7 revisions

Basedmypy support a feature know as 'baselining' where a snapshot of a code base is taken and compared against on subsequent runs. This can be used to ease the gradual adoption of new checks and strictness options. It is not intended as a utility to replace type: ignore comments or to hide errors.

The baseline file should be committed with your project so that other people can utilize and update it.

Workflow

Consider the following:
test.py:

def foo(a: list) -> None:
    if "bar" in a:
        a.append("baz") 

All three lines here contain errors:

> mypy test.py
test.py:1: error: Missing type parameters for generic type "list"  [type-arg]     
test.py:2: error: Expression type contains "Any" (has type "List[Any]")  [no-any-expr]
test.py:3: error: Expression type contains "Any" (has type "List[Any]")  [no-any-expr]
Found 3 errors in 1 file (checked 1 source file)

We can write these errors to a baseline file:

> mypy --write-baseline test.py
test.py:1: error: Missing type parameters for generic type "list"  [type-arg]     
test.py:2: error: Expression type contains "Any" (has type "List[Any]")  [no-any-expr]
test.py:3: error: Expression type contains "Any" (has type "List[Any]")  [no-any-expr]
Found 3 errors (3 new errors) in 1 file (checked 1 source file)
Baseline successfully written to .mypy/baseline.json

Now when we run mypy again we will not receive any errors:

> mypy test.py
Success: no issues found in 1 source file

If we modify the source code to have the correct full-form type annotation:

def foo(a: list[str]) -> None:
    if "bar" in a:
        a.append("baz")

And run mypy again:

> mypy test.py
No errors, baseline file removed
Success: no issues found in 1 source file

It has detected that all the errors have now been resoved and the baseline file is no longer required.

If your project uses multiple mypy runs or configurations you can specify alternative baseline file locations for each:

> mypy --baseline-file .mypy/strict.json

Command Line Options

--write-baseline

Run mypy and create/update the baseline file with the errors found.

--baseline-file

The path of the baseline file to use. Default is ./.mypy/baseline.json.

You can specify --baseline-file= to indicate that no baseline should be used:

> mypy --baseline-file= test.py

--auto-baseline

Inverse: --no-auto-baseline

Enabled by default, this feature will update the baseline file automatically if there are no new errors encountered.

--baseline-format

Defaults to the format of the current version, this will set mypy to expect and be compatible with older versions of baseline files.

Note on --legacy

Baseline functionality is not affected by the --legacy flag.