Annotation parsing incorrect when class scope defines same name as import
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Given:
import uuid
class Bar:
uuid: uuid.UUID = uuid.uuid4()
reveal_type(Bar().uuid)
mypy reports no errors and will inform the user that the type of the attribute is uuid.UUID.
However this isn't strictly correct and without lazy annotations enabled this code will actually error at runtime:
File ".../demo.py", line 3, in Bar
uuid: uuid.UUID = uuid.uuid4()
AttributeError: 'UUID' object has no attribute 'UUID'
Ideally mypy would report the missing attribute (UUID) along with a mention of the name overlap between the type name and the attribute name.
Cross-linking: a related issue was raised against pyflakes (that the import appeared unused; https://github.com/PyCQA/pyflakes/issues/682; https://github.com/PyCQA/pyflakes/issues/648), though rejected on the grounds that this code is indeed invalid. Unfortunately when this code is alongside other code it is easy for there not to be any linter errors.
Your Environment
- Mypy version used: 0.910, also tested 0.941
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none that matter - Python version used: 3.9
- Operating system and version: Ubuntu 20.04
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
Reproduziere das Problem mit dem bereitgestellten Python-Snippet unter Verwendung von mypy ohne Konfigurationsflags und verfolge anschließend die Auflösung von Annotierungsnamen für das Klassenattribut und das importierte uuid. Füge Abdeckung für die Diagnosen missing-attribute und name-overlap hinzu und überprüfe das Verhalten anhand des zur Laufzeit ungültigen Beispiels.
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
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100