Sequence of types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
The following works in pyright, but not in pyrefly, ty, or (which is why I'm here!) mypy:
from __future__ import annotations
from dataclasses import dataclass
from typing_extensions import (
Generic,
Sequence,
TypeVar,
assert_type,
)
T = TypeVar("T")
@dataclass
class Agent(Generic[T]):
output_type: Sequence[type[T]]
class Foo:
pass
class Bar:
pass
# pyright - works
# mypy - error: Expression is of type "Agent[object]", not "Agent[Foo | Bar]" [assert-type]
# pyrefly - assert_type(Agent[Foo], Agent[Bar | Foo]) failed + Argument `list[type[Bar] | type[Foo]]` is not assignable to parameter `output_type` with type `Sequence[type[Foo]]` in function `Agent.__init__`
# ty - `Agent[Foo | Bar]` and `Agent[Unknown]` are not equivalent types
assert_type(Agent([Foo, Bar]), Agent[Foo | Bar])
# pyright - works
# mypy - error: Expression is of type "Agent[Never]", not "Agent[int | str]" [assert-type]
# pyrefly - assert_type(Agent[int], Agent[str | int]) failed + Argument `list[type[str] | type[int]]` is not assignable to parameter `output_type` with type `Sequence[type[int]]` in function `Agent.__init__`
# ty - `Agent[int | str]` and `Agent[Unknown]` are not equivalent types
assert_type(Agent([int, str]), Agent[int | str])
# works
assert_type(Agent[Foo | Bar]([Foo, Bar]), Agent[Foo | Bar])
# works
assert_type(Agent[int | str]([int, str]), Agent[int | str])
It would be great to see this work in mypy, but if there's a good reason the other 2 out of 3 typecheckers also don't support this I'd love to understand why!
- This is related to a new PydanticAI feature, if you're curious check out https://github.com/pydantic/pydantic-ai/pull/1785#issuecomment-2905774110
- Issues for other type checkers:
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
Führe zunächst die minimale Reproduktion im Issue aus und vergleiche die von mypy inferierten Typen mit den explizit parametrisierten Fällen. Untersuche, wie die Inferenz generischer Konstruktoren mit Sequence[type[T]] umgeht, wenn das Argument mehrere Typen enthält. Die Aufgabe ist abgeschlossen, wenn die Assertions für den nicht parametrisierten Agent([Foo, Bar]) und Agent([int, str]) bestehen, ohne die bestehenden Fälle zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100