Unable to type check only specific submodule/subpackage.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Perhaps, this is a "skill issue" on my side, but I was unable to figure out how to make mypy type check a specific submodule/subpackage only. Documentation and previous issues on this topic recommend using the --follow-imports flag, but this flag doesn't quite solve the problem, and it seems to be more of a workaround than a true solution.
To Reproduce
Consider the following source tree:
# foo/__init__.py
from . import _internal_junk, strictly_typed_public_module
# foo/_internal_junk/__init__.py
from . import junk
# foo/_internal_junk/junk.py
def incorrectly_typed_function(val: int) -> str: return val / 2.0
# foo/strictly_typed_public_module/__init__.py
from . import api_component1, api_component2
# foo/strictly_typed_public_module/api_component1.py
from some_different_package import this_package_has_a_typing_error
def this_is_ok() -> str: return "okay"
# foo/strictly_typed_public_module/api_component2.py
def this_mistake_should_be_caught_by_mypy(v: str) -> int: return 2.0
# some_different_package.py
def this_package_has_a_typing_error(a: str) -> int: return a
Then, here are some of my attempts to check only the strictly_typed_module submodule:
mypy --strict -m foo.strictly_typed_public_module
mypy --strict -p foo.strictly_typed_public_module
mypy --strict foo/strictly_typed_public_module
mypy --strict -m foo.strictly_typed_public_module --follow-imports=silent
mypy --strict foo/strictly_typed_public_module --follow-imports=silent
mypy --strict -p foo.strictly_typed_public_module --follow-imports=silent
None of these produce the desired results.
Expected Behavior
mypyshould find all the typing problems in thestrictly_typed_public_modulesubmodule:- type check all the files that are part of this submodule
- transitively follow all imports in those files
mypyshould NOT check any unrelated files that are not submodules of thestrictly_typed_public_moduleOR imported by it.
Actual Behavior
The first 3 commands (without --follow-imports) all produce the same output:
foo/_internal_junk/junk.py:1: error: Incompatible return value type (got "float", expected "str") [return-value]
some_different_package.py:1: error: Incompatible return value type (got "str", expected "int") [return-value]
foo/strictly_typed_public_module/api_component2.py:1: error: Incompatible return value type (got "float", expected "int") [return-value]
Found 3 errors in 3 files (checked 1 source file)
Here, mypy checks foo/_internal_junk/junk.py even though it was not requested on the CLI, and it's not imported by the strictly_typed_public_module.
The next command (-m with --follow-imports=silent) just ignores all imports and seemingly only checks the single __init__.py file:
Success: no issues found in 1 source file
The final 2 commands ALMOST do the right thing:
foo/strictly_typed_public_module/api_component2.py:1: error: Incompatible return value type (got "float", expected "int") [return-value]
Found 1 error in 1 file (checked 3 source files)
Except that they also ignore the external import from some_different_package.
Your Environment
- Mypy version used:
1.5.1 (compiled: yes) - Mypy configuration options from
mypy.ini(and other config files): None - Python version used:
3.11.3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the documented -m, -p, path, and --follow-imports commands against the example package tree in the issue. Trace how mypy selects package files and follows imports; done means the selected submodule and its transitive imports are checked while unrelated modules are excluded.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100