valid-type not reported for variables imported in other modules
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Mypy reports [valid-type] for a variable defined in the same file but not when the variable is imported in other files.
To Reproduce
Create 3 files
# app/example_a.py
def dynamic_type(x):
return type("some_type", () , x)
# app/example_b.py
from app.example_a import dynamic_type
SOME_FAKE_TYPE = dynamic_type({"a": "a"})
x: SOME_FAKE_TYPE
# app/example_c.py
from app.example_b import SOME_FAKE_TYPE
x: SOME_FAKE_TYPE
Expected Behavior
Mypy should raise [valid-type] errors in both app/example_b.py and app/example_c.py
Actual Behavior
It only reports [valid-type] errors in app/example_b.py
app/example_b.py:6: error: Variable "app.example_b.SOME_FAKE_TYPE" is not valid as a type [valid-type]
app/example_b.py:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Your Environment
$ uv run mypy --version
mypy 1.16.0 (compiled: yes
- Mypy version used: 1.16.0
- Mypy command-line flags:
mypy - Mypy configuration options from
mypy.ini(and other config files):
[mypy]
check_untyped_defs = True
follow_imports = normal
disallow_untyped_defs = False
files = app/
- Python version used: 3.11.8
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, den Bericht mit app/example_a.py, app/example_b.py und app/example_c.py unter Verwendung von mypy 1.16.0 zu reproduzieren, und vergleiche anschließend die [valid-type]-Diagnosen für die lokalen und importierten Variablen. Die Korrektur ist vollständig, wenn mypy den erwarteten Fehler sowohl in example_b.py als auch in example_c.py meldet, ohne die Absicht der Reproduktion zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100