Troubleshooting MyPy Exclude Option with pytest --mypy
Troubleshooting MyPy Exclude Option with pytest --mypy

mypy exclude option not working with pytest –mypy: Bug or Misconfiguration?

Using MyPy with pytest helps catch type errors early, making Python projects more robust. Investigate why mypy exclude option not working with pytest --mypy.4 min


Using MyPy with pytest helps catch type errors early, making Python projects more robust. But sometimes, things don’t work as expected. One issue that often confuses developers is the mypy exclude option not working when running pytest –mypy. Is this a bug in pytest-mypy or a simple misconfiguration? Let’s investigate.

Setting Up the Project Folder

To reproduce the problem, let’s set up a small project with specific files and folders.

First, create a main project folder:


MyProjectFolder/

Inside this folder, add a basic Python script:


helloroot.py

Next, create a subfolder that we want to ignore:


MyProjectFolder/FolderNotIgnored/

Inside this subfolder, create another Python file:


MyProjectFolder/FolderNotIgnored/hello.py

Finally, add a mypy.ini file in MyProjectFolder/ with the following configuration:


[mypy]
exclude = FolderNotIgnored/

This tells MyPy to ignore FolderNotIgnored/ and its contents when checking types. But will pytest –mypy honor this setting? Let’s find out.

Running pytest –mypy

Open a terminal in MyProjectFolder/ and run:


pytest --mypy

This command should run MyPy on all Python files in the project except those in FolderNotIgnored/.

Expected behavior: MyPy should analyze helloroot.py but skip FolderNotIgnored/hello.py because of the exclude configuration.

Unexpected output: Strangely, pytest still applies MyPy checks to FolderNotIgnored/hello.py, despite the exclusion rule. The error messages from MyPy indicate that the file is still being analyzed.

What’s causing this behavior?

Analyzing the Problem

Is this issue a bug in pytest-mypy, or is it a misconfiguration we can correct? Let’s break it down.

First, carefully check the pytest output. If pytest-mypy still scans the excluded folder, it might mean:

  • pytest is not correctly respecting MyPy’s configuration.
  • The exclude rule in mypy.ini isn’t being applied correctly.
  • pytest-mypy processes files differently from running MyPy directly.

To confirm whether MyPy itself honors the exclusion, run MyPy independently:


mypy .

If MyPy ignores FolderNotIgnored/ here but pytest-mypy does not, the issue likely lies in pytest-mypy’s integration rather than MyPy itself.

Troubleshooting Steps

Start with basic verification:

Check mypy.ini

Ensure the exclude rule is correctly formatted:


[mypy]
exclude = FolderNotIgnored/

Try modifying it using a regex for clarity:


exclude = ^FolderNotIgnored/

Verify the Folder Structure

Run in the correct directory and double-check your project structure:


ls -R

Paths specified in mypy.ini must match the actual directory structure exactly.

Test with Different Settings

Try explicitly specifying files to include in mypy.ini:


[mypy]
files = helloroot.py

This should prevent FolderNotIgnored/ from being analyzed. If pytest still picks it up, pytest-mypy might not be fully respecting MyPy’s settings.

Solutions and Workarounds

Update MyPy & pytest-mypy

An outdated version of pytest-mypy can contain bugs. Update everything to the latest versions:


pip install --upgrade pytest pytest-mypy mypy

If the issue persists, check pytest-mypy’s issue tracker on GitHub to see if this is a known bug.

Manually Exclude Files

If exclude doesn’t work, use per-file ignores in mypy.ini:


[mypy-FolderNotIgnored.*]
ignore_errors = True

This tells MyPy to silently ignore errors in files within FolderNotIgnored/.

Use Alternative Exclusion Methods

Another approach is limiting your pytest scope explicitly:


pytest --mypy helloroot.py

Or create a custom pytest.ini that excludes undesired directories:


[pytest]
addopts = --mypy --ignore=FolderNotIgnored/

This ensures pytest doesn’t check that directory at all.

Summary

When pytest –mypy doesn’t exclude files as expected, it usually indicates an issue within pytest-mypy’s integration, not MyPy itself.

Ensuring correct mypy.ini formatting, verifying your directory structure, and updating related packages can resolve most common scenarios. For challenging cases, per-file ignores or pytest-specific exclusions can effectively handle the issue.

If the problem remains unresolved, consider reporting it to pytest-mypy’s GitHub repository, as others might also encounter this.

Have you faced this issue? Let us know what worked for you!


Like it? Share with your friends!

Shivateja Keerthi
Hey there! I'm Shivateja Keerthi, a full-stack developer who loves diving deep into code, fixing tricky bugs, and figuring out why things break. I mainly work with JavaScript and Python, and I enjoy sharing everything I learn - especially about debugging, troubleshooting errors, and making development smoother. If you've ever struggled with weird bugs or just want to get better at coding, you're in the right place. Through my blog, I share tips, solutions, and insights to help you code smarter and debug faster. Let’s make coding less frustrating and more fun! My LinkedIn Follow Me on X

0 Comments

Your email address will not be published. Required fields are marked *