Skip to content

[None] * int not widened to match expected list type #3002

@Jeremiah-England

Description

@Jeremiah-England

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

Metadata

Metadata

Assignees

Labels

bidirectional inferenceInference of types that takes into account the context of a declared type or expected type

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions