-
Notifications
You must be signed in to change notification settings - Fork 219
Closed
astral-sh/ruff
#22426Labels
bidirectional inferenceInference of types that takes into account the context of a declared type or expected typeInference of types that takes into account the context of a declared type or expected typegenericsBugs or features relating to ty's generics implementationBugs or features relating to ty's generics implementation
Milestone
Description
Summary
def upcast(arg: int) -> int:
return arg
def test[X: int](items: list[X]) -> list[X]:
_a: list[int] = list(items) # ✅️
_b: list[int] = [*items] # ✅️
_c: list[int] = [x for x in items] # ✅️
_1: list[str] | list[int] = list(items) # ❌️
_2: list[int] | list[str] = list(items) # ❌️
_3: list[str] | list[int] = [*items] # ✅️
_4: list[int] | list[str] = [*items] # ✅️
_5: list[str] | list[int] = [x for x in items] # ✅️
_6: list[int] | list[str] = [x for x in items] # ✅️
_7: list[str] | list[int] = [upcast(x) for x in items] # ✅️
_8: list[int] | list[str] = [upcast(x) for x in items] # ✅️
return itemsThis reports
Object of type
list[X@test]is not assignable to `list[str] | list[int]
But at the same time, it reports list is [T] (Iterable[T]) -> list[T].
So in _1 and _2 we have joint constraints:
X <: T(list[X]must be assignable toIterable[T]),X <: intupper boundT = intorT=str(list[T] <: list[int] | list[str])
So, this has the unique solution T=int, but ty seems to incorrectly select T=X.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bidirectional inferenceInference of types that takes into account the context of a declared type or expected typeInference of types that takes into account the context of a declared type or expected typegenericsBugs or features relating to ty's generics implementationBugs or features relating to ty's generics implementation