python / python/mypy

Support using Literal on __getattr__ and __getattribute__

Aperta
#8,203 4 commenti 24 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature needs discussion priority-1-normal topic-literal-types
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Hi! Feature request for using Literal on __getattr__ and __getattribute__.

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Literal

LiteralStrHello = Literal["hello"]
LiteralStrWorld = Literal["world"]


class A:
    def __getattr__(self, name: LiteralStrHello) -> LiteralStrWorld:
        assert name == "hello"
        return "world"

    def __getattribute__(self, name: LiteralStrHello) -> LiteralStrWorld:
        assert name == "hello"
        return "world"
  • What is the actual behavior/output?
main.py:8: error: Invalid signature "def (main.A, Literal['hello']) -> Literal['world']" for "__getattr__"
main.py:12: error: Invalid signature "def (main.A, Literal['hello']) -> Literal['world']" for "__getattribute__"
main.py:12: error: Argument 1 of "__getattribute__" is incompatible with supertype "object"; supertype defines the argument type as "str"
Found 3 errors in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
Success: no issues found in 1 source file
  • What are the versions of mypy and Python you are using? Mypy:mypy 0.770+dev.5336f272c2ac625bd40a79ab3dbbe3d548f147b8, Python:3.8.0
    Do you see the same issue after installing mypy from Git master? yes

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 eseguendo la riproduzione fornita in main.py con mypy e confronta le diagnostico riportato per getattr e getattribute. Traccia i punti di ingresso del controllo dei tipi per questi metodi speciali; il lavoro è completato quando la riproduzione produce “Success: no issues found in 1 source file” senza indebolire i normali controlli di object.getattribute.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.