python / python/mypy

Unable to type check only specific submodule/subpackage.

Aperta
#15,936 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo i comandi documentati -m, -p, path e --follow-imports sull’albero dei pacchetti di esempio nell’issue. Traccia come mypy seleziona i file dei pacchetti e segue gli import; il lavoro è completato quando vengono controllati il sottomodulo selezionato e i relativi import transitivi, mentre i moduli non correlati vengono esclusi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.