python / python/mypy

mypy does not show error in excluded folder when PYTHONPATH set

Open
#16,973 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

As far as I understand, when I use the exclude setting for some folder mypy should still show errors in these packages when imported, because of the follow imports logic. However, if I set the PYTHONPATH to the current working directory, it does not show these errors anymore.

To Reproduce
Here is the folder structure:

src/
|--excluded_folder/
|           |-- excluded.py
|           |-- __init__.py
|--included_folder/
            |-- included.py
            |-- __init__.py

The contents of the file excluded.py are as follows:

def function() -> str:  # obviously wrong return type
        return 5

File included.py looks like so:

from excluded_folder.excluded import function

print(function)

Now, if i run mypy like so, with the setting to exclude the excluded folder, it reports this:

$ mypy --strict --exclude excluded_folder src                          
src/excluded_folder/excluded.py:3: error: Incompatible return value type (got "int", expected "str")  [return-value]
Found 1 error in 1 file (checked 2 source files)

This is what i would expect. It still shows the errors in the file, although excluded.

But if i run mypy with PYTHONPATH set to the current working directory, it does not show any error anymore:

% PYTHONPATH=. mypy --strict --exclude excluded_folder src
Success: no issues found in 2 source files

Expected Behavior
Mypy should also show the errors when the PYTHONPATH is set.

Actual Behavior
Mypy does not show the errors when the PYTHONPATH is set.

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: see commands above
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.11.3

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.

Research direction

Start with the reproduced package layout containing excluded_folder/excluded.py and included_folder/included.py, then compare the two mypy commands with and without PYTHONPATH=. Trace how --exclude and imported modules are discovered. Done means the PYTHONPATH=. invocation reports the incompatible return value in excluded.py, with a regression test covering both commands.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.