bug: strict = true is incompatible with per-module configuration

Open
#11,401 11 comments 30 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Reproduce the issue with the shown mypy.ini and pyproject.toml configurations, then trace how strict and per-module options are processed when running mypy . . Done means strict applies to my_module.* while tests.* accepts untyped definitions in both supported configuration formats.

Written by the indexing model from the issue text.

Description

bug topic-configuration

To Reproduce

Create the following file structure:

my_module/__init__.py
tests/__init__.py
mypy.ini

Both my_module/__init__.py and tests/__init__.py contain:

def untyped():
    ...

Which should trigger an error when run with strict but otherwise pass.

In mypy.ini:

[mypy]
strict = true
[mypy-tests.*]
strict = false

That is, I want strict mode in my source code but not my tests.

Then I run this using mypy .

Expected Behavior

I expect to get an error for my source code but not my tests:

my_module/__init__.py:1: error: Function is missing a return type annotation
my_module/__init__.py:1: note: Use "-> None" if function does not return a value

Actual Behavior

I get an error for both my source code and tests.
It seems like strict = False in the per module section is ignored.

tests/__init__.py:1: error: Function is missing a return type annotation
tests/__init__.py:1: note: Use "-> None" if function does not return a value
my_module/__init__.py:1: error: Function is missing a return type annotation
my_module/__init__.py:1: note: Use "-> None" if function does not return a value

If I add an explicit allow_untyped_defs = True to the mypy-tests.* section:

[mypy]
strict = true
[mypy-tests.*]
strict = false
allow_untyped_defs = True

That seems to be processed fine (I now get no errors for my tests but still get errors for my source).
So I believe the config file is valid, mypy is picking up that the options only apply to tests.*.

I also tried flipping the enable/disable:

[mypy]
strict = false
[mypy-my_module.*]
strict = true

But I get the same result.

This also seems to be the case with pyproject.toml configs.

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.