Methods with the same name as a type sometimes cause error "not valid as a type"
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
When using a decorator on a class method with the same name as a type used in its return type, and the method is decorated with a decorator that spans multiple lines, an error is thrown.
What puzzles me is that this only causes an error if the decorator spans multiple lines, not if it is just on a single line.
Neither pylint nur flake8 do complain about having a method that shadows another name in general. So I am assuming that this should be generally ok.
To Reproduce
class Foo:
@property
# the existence of this comment triggers the error
def int(self) -> int:
return 0
Some more examples show that this
- The error occurs in any situation where the decorator starts more than one line before the function definition.
- with a blank line in between
- with a comment line in between
- with a multi-line argument decorator (this is the case where I initially encountered it when using @deprecated.
- The error occurs with both built-in decorators (
@property) and custom defined decorators. - The error occurs both with built-in types and imported types.
Expected Behavior
Use the types from the outer scope to construct the return type and validate correctly.
Actual Behavior
error: Function "__main__.Foo.int" is not valid as a type [valid-type]
note: Perhaps you need "Callable[...]" or a callback protocol?
Your Environment
- Mypy version used: 1.2.0, master branch via mypy-play.net
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10 and 3.11 tested
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’esempio Python minimo nel report ed esegui mypy 1.2.0 o il master corrente, quindi confronta un decoratore su una singola riga con versioni separate da un commento, una riga vuota o argomenti su più righe. Traccia la risoluzione del nome del metodo mentre viene verificata la sua annotazione del tipo restituito. Il lavoro è completato quando tutte le disposizioni dei decoratori mostrate accettano il tipo dell’ambito esterno senza l’errore valid-type.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100