[spec] Can staticmethod calls infer type parameters for the containing class?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
In this code:
from typing import TypeVar, Generic
T = TypeVar('T')
class Foo(Generic[T]):
@staticmethod
def create(arg: T) -> 'Foo[T]':
...
reveal_type(Foo.create(1234))
What should the type be? Should T be inferred based on the argument to the static method, resulting in Foo[int]? Or should it be Foo[Unknown] or similar?
mypy (python/mypy@fe15ee69b) picks Foo[int].
pyright used to choose Foo[int], but microsoft/pyright#7454 changed it to pick Foo[Unknown], on the grounds that this is required to comply with PEP 696's statement:
Type parameter defaults should be bound by attribute access (including call and subscript).
My example does not include any type parameter defaults, but I can see the logic of applying the same rule without one: you wouldn't want it to be a breaking change to add a default.
Still, why can't these kinds of static method calls be treated like constructor calls, where class type parameters can be inferred from arguments?
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
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 mit der Durchsicht des Beispiels, der Bindungsregeln von PEP 696, des mypy-Commits fe15ee69b und des referenzierten pyright-Issues. Ermittle das vereinbarte Verhalten für staticmethod-Aufrufe bei generischen Klassen und halte die daraus resultierende Spezifikationsentscheidung fest, einschließlich der Frage, ob Typechecker Foo[int] ableiten oder einen unbekannten Typ verwenden sollen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100