basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

`Literal` accepted as `type[T]`, and is inconsistent

Open superlopuh opened this issue 2 months ago • 5 comments

from typing import Literal, TypeGuard, TypeVar

T = TypeVar("T")


def isa_literal(obj: object, t: type[T]) -> TypeGuard[T]:
    assert False


a = isa_literal("string", Literal["string"]) # Argument of type "type[Literal['string']]" cannot be assigned to parameter "t" of type "type[T@isa_literal]" in function "isa_literal"basedpyright[reportArgumentType](https://github.com/detachhead/basedpyright/blob/main/docs/configuration.md#reportArgumentType)
a = isa_literal("string", Literal["string", "other_string"])

The first one fails, although, to my knowledge, it's a valid construct.

Edit

When the value goes through a generic function, it has a different type than just the direct assignment:

from typing import Literal

def f[T](t: T) -> T: ...
c: type[int]
c = Literal[1, 2]  # Expression of type "type[Literal[1, 2]]" cannot be assigned to declared type "type[int]"
c = f(Literal[1, 2])

superlopuh avatar Apr 22 '24 15:04 superlopuh