using __new__ with type(x: T) resolvestype(x: T) as being of type type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
The following code passes a MyPy check with flying colors:
from typing import TypeVar, Any
T = TypeVar("T", bound=Any)
def this_one_works(x: T) -> T:
res = x.__new__(type(x))
return res
But the following does not:
from typing import TypeVar, Any
T = TypeVar("T", bound=Any)
def this_one_not_so_much(x: T) -> T:
res = type(x).__new__(type(x))
return res
Instead it seems to resolve type(x) as <class 'type'> when used with __new__() and complains about missing arguments:
test.py:5: error: No overload variant of "new" of "type" matches argument type "Type[T]"
test.py:5: note: Possible overload variants:
test.py:5: note: def new(cls, cls: Type[type], o: object) -> type
test.py:5: note: def new(cls, cls: Type[type], name: str, bases: Tuple[type, ...], namespace: Dict[str, Any]) -> type
Found 1 error in 1 file (checked 1 source file)
Is this expected behavior, or is it resolving incorrectly in the second case?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die beiden Codeausschnitte aus dem Issue mit mypy auszuführen und zu vergleichen, wie type(x) und x.new inferiert werden. Verfolge den Type-Checking-Pfad für type(x).new und ermittle, ob die gemeldete Auflösung erwartungsgemäß ist; abgeschlossen ist die Aufgabe, wenn das beabsichtigte Verhalten festgestellt und das Ergebnis dokumentiert oder getestet wurde.
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
- 42/100