python / python/mypy

Support using Literal on __getattr__ and __getattribute__

Offen
#8,203 4 Kommentare 24 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature needs discussion priority-1-normal topic-literal-types
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

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

Führe zunächst die bereitgestellte main.py-Reproduktion mit mypy aus und vergleiche die gemeldeten Diagnosen für getattr und getattribute. Verfolge die Einstiegspunkte der Typprüfung für diese speziellen Methoden; abgeschlossen ist die Aufgabe, wenn die Reproduktion „Success: no issues found in 1 source file“ ausgibt, ohne die Prüfungen für gewöhnliches object.getattribute abzuschwächen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

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