python / python/mypy

`__new__()` on a `Generic` class doesn't work with `Self` as return type if the typevar is not specified, picking the first element of the typevar

Offen
#15,322 1 Kommentar 0 Reaktionen 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

Bug Report

In pandas-stubs, we want to be able to return Series[int], Series[str], etc., as well as just Series from a __new__() constructor. In the current released version, we had return types of Series[S1] and Series and everything worked fine. But pyright made a change and developers there (@erictraut) say that Self needs to be the return type. See discussion starting here: https://github.com/microsoft/pyright/issues/5178#issuecomment-1563478640

With that change, mypy has a strange behavior, where returning Self in an overload that doesn't specify the value of the Generic parameter picks the first value. in the list provided in the TypeVar declaration.

To Reproduce

from typing import Generic, Self, TypeVar, overload, cast

S = TypeVar("S", int, str, bytes)


class Foo(Generic[S]):
    @overload
    def __new__(cls, dtype: type[S]) -> Self:
        ...

    @overload
    def __new__(cls, dtype=None) -> Self:
        ...

    def __new__(cls, dtype: type[S] | None = None) -> Self:
        return cast(Self, object.__new__(cls))


f = Foo(None)
reveal_type(f)  # Mypy reveals: Foo[int]; should be Foo[Any]

f2 = Foo(str)
reveal_type(f2)

Expected Behavior

Output from pyright:

gennewself.py:20:13 - information: Type of "f" is "Foo[Unknown]"
gennewself.py:23:13 - information: Type of "f2" is "Foo[str]"

Actual Behavior

gennewself.py:20: note: Revealed type is "gennewself.Foo[builtins.int]"
gennewself.py:23: note: Revealed type is "gennewself.Foo[builtins.str]"

The issue here is that the type of f should be Foo[Any], not Foo[int] .

Your Environment

  • Mypy version used: 1.3.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.31

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 damit, den bereitgestellten Generic-, overload- und Self-Reproducer mit mypy 1.3.0 auszuführen und seine aufgedeckten Typen mit der Ausgabe von pyright zu vergleichen. Verfolge die Typinferenz für einen nicht angegebenen Generic-Parameter in new-Overloads; erledigt ist die Aufgabe, wenn Foo(None) Foo[Any] aufdeckt, während Foo(str) weiterhin Foo[str] aufdeckt, mit Regressionstests für beide Fälle.

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.