python / python/mypy

(🐞) `type.__call__` not checked properly

Offen
#17,316 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

class A:
    def __init__(self, x: int):
        print(x)

A.__call__("a")  # no error
A("a")  # Argument 1 to "A" has incompatible type "str"; expected "int"  [arg-type]

The typing spec says:

At runtime, a call to a class’ constructor typically results in the invocation of three methods in the following order:

  1. The __call__ method of the metaclass (which is typically supplied by the type class but can be overridden by a custom metaclass and which is responsible for calling the next two methods)

So mypy is checking that usages of the call syntax (()) are conforming to the constructor type, but the functionally identical call attribute __call__. This is against the spec, which specifies that type checkers should mirror the behavior that __call__ will invoke __new__ and __init__

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 mypy's Behandlung von Konstruktoraufrufen und Attributaufrufen an type.__call__, wobei das Beispiel im Issue als Reproduktion verwendet wird. Verfolge, wie A("a") im Vergleich zu A.__call__("a") geprüft wird, und füge Tests hinzu, die zeigen, dass beide Formen den Argumenttyp des Konstruktors erzwingen.

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.