-
Notifications
You must be signed in to change notification settings - Fork 276
[None] * int not widened to match expected list type #3002
Copy link
Copy link
Closed
astral-sh/ruff
#24197Labels
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 type
Milestone
Description
Summary
See https://play.ty.dev/092bbf5e-a753-4e30-bd29-1a80fb86304d and #2972 (comment).
from typing import reveal_type
x: list[int] = list()
y: list[int | None] = [None] * len(x) # Object of type `list[None]` is not assignable to `list[int | None]` (invalid-assignment)
y: list[int | None] = [None] # OK
def func() -> list[int | None]:
if ...:
return [None] * len(x) # Return type does not match returned value: expected `list[int | None]`, found `list[None]` (invalid-return-type)
else:
return [None] # OK
def func(x: list[int | None]):
...
func([None] * len(x)) # Argument to function `func` is incorrect: Expected `list[int | None]`, found `list[None]` (invalid-argument-type)
func(reveal_type([None])) # OK (Revealed type: `list[int | None]`)Version
ty 0.0.21
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 type