Skip to content

Literal not infered as list[Literal] after casting it inside a list #1648

@mflova

Description

@mflova

Summary

Errors not raised by mypy nor pyright

It seems that for this specific case, doing [tags] infers list[str] instead of list[Tags], which makes the assignment invalid.

from collections.abc import Sequence
from typing import Literal, TypeAlias

Tags: TypeAlias = Literal["a", "b"]

def func(tags: Tags | Sequence[Tags]) -> None:
    tags = [tags] if isinstance(tags, str) else tags  # error[invalid-assignment]: Object of type `list[Unknown | str] | (Sequence[Literal["a", "b"]] & ~str)` is not assignable to `Literal["a", "b"] | Sequence[Literal["a", "b"]]`

Version

ty 0.0.1-alpha.28 (8c34249 2025-11-25)

Metadata

Metadata

Assignees

No one assigned

    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