python / python/mypy

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

Aperta
#17,227 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

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.

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 con il riproduttore in test.py ed esegui il comando mypy mostrato insieme a Python per confermare la discrepanza. Traccia il modo in cui mypy risolve l’annotazione e il lato destro all’interno della classe A, quindi aggiungi un test di regressione che mostri che l’esempio viene rifiutato con un errore esplicativo.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.