mypy confuses dataclass field name with imported function name
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
This standalone file produces the mistaken error models/mypy_bug.py:8: error: "str" not callable:
from dataclasses import dataclass, field
from typing import List
@dataclass
class FilterExpression:
match_type: str
field: str
value: List[str] = field(default_factory=list)
This is because mypy confuses the field "field" and the imported function "field()". Renaming the field removes the error message.
Expected Behavior
This should not be an error.
It's not obvious to me what's correct here, but this code executes as intended in Python 3.8.
So either mypy or Python 3.8 is wrong about this. I choose to file the bug with mypy.
Actual Behavior
mypy gives this error:
models/mypy_bug.py:8: error: "str" not callable
Your Environment
- Mypy version used: 0.800
- Mypy command-line flags: Only
--ignore-missing-imports [paths] - Mypy configuration options from
mypy.ini:strict_optional = False - Python version used: 3.8
- Operating system and version: MacOS 11.2
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
Führe zunächst mypy 0.800 für das eigenständige Beispiel models/mypy_bug.py mit den gemeldeten Flags und der Python-3.8-Umgebung aus und untersuche anschließend den Namensauflösungspfad für dataclass-Felder und importierte Funktionen. Fertig ist die Arbeit, wenn das Beispiel nicht mehr "str" not callable meldet und dabei die korrekte Behandlung der field()-default factory erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100