python / python/mypy

Unable to type check only specific submodule/subpackage.

Offen
#15,936 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

  1. mypy should find all the typing problems in the strictly_typed_public_module submodule:
    1. type check all the files that are part of this submodule
    2. transitively follow all imports in those files
  2. mypy should NOT check any unrelated files that are not submodules of the strictly_typed_public_module OR 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die dokumentierten -m-, -p-, path- und --follow-imports-Befehle anhand des Beispiel-Paketbaums im Issue nachzustellen. Verfolge, wie mypy Paketdateien auswählt und Imports verfolgt; abgeschlossen ist die Aufgabe, wenn das ausgewählte Submodul und seine transitiven Imports geprüft werden, während nicht verwandte Module ausgeschlossen bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.