python / python/mypy

mypy should fail if the first element of the type has the same name as the variable

Offen
#17,227 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

This example file.

import pathlib

class A:
    pathlib: pathlib.Path = pathlib.Path('/')
$ mypy test.py 
Success: no issues found in 1 source file

$ python3 test.py 
Traceback (most recent call last):
  File "/tmp/test.py", line 3, in <module>
    class A:
  File "/tmp/test.py", line 4, in A
    pathlib: pathlib.Path = pathlib.Path('/')
             ^^^^^^^^^^^^
AttributeError: 'PosixPath' object has no attribute 'Path'. Did you mean: 'match'?

Expected Behavior

I would expect mypy to fail, possibly explaining why.

Actual Behavior

It succeeds, even though the type annotations cause a failure.

Your Environment

  • Mypy version used: mypy 1.10.0 (compiled: yes)
  • Python version used: Python 3.11.9

I had thought incorrectly that it was an issue with attrs https://github.com/python-attrs/attrs/issues/1282 but @euresti provided the example to re-create the problem.

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 mit dem Reproducer in test.py und führe den gezeigten mypy-Befehl zusammen mit Python aus, um die Abweichung zu bestätigen. Verfolge, wie mypy die Annotation und die rechte Seite innerhalb der Klasse A auflöst, und füge anschließend einen Regressionstest hinzu, der zeigt, dass das Beispiel mit einer erklärenden Fehlermeldung abgelehnt wird.

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
35/100

Neue Issues direkt in Ihr Postfach

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