Skip to content

Baseline

DetachHead edited this page Mar 3, 2022 · 7 revisions

Basedmypy supports a feature known 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]")  [dynamic]
test.py:3: error: Expression type contains "Any" (has type "List[Any]")  [dynamic]
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]")  [dynamic]
test.py:3: error: Expression type contains "Any" (has type "List[Any]")  [dynamic]
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.

Commands

--write-baseline

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

--baseline-file

The path of the baseline file, default is ./.mypy/baseline.json.

--auto-baseline

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

Clone this wiki locally