Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type[Literal['']] does not accept str as the type #18196

Closed
sobolevn opened this issue Nov 26, 2024 · 11 comments · Fixed by #18276
Closed

type[Literal['']] does not accept str as the type #18196

sobolevn opened this issue Nov 26, 2024 · 11 comments · Fixed by #18276
Assignees
Labels
bug mypy got something wrong topic-literal-types

Comments

@sobolevn
Copy link
Member

I am not even sure that type[Literal] is correct :)

from typing import Literal

def a(x: type[Literal['a']]):
    reveal_type(x)
    
a(str)  # Argument 1 to "a" has incompatible type "type[str]"; expected "type[Literal['a']]"  [arg-type]

https://mypy-play.net/?mypy=latest&python=3.12&gist=6b8a1d7e6f98c64e7f751c58d356bf16

@sobolevn sobolevn added bug mypy got something wrong topic-literal-types labels Nov 26, 2024
@JelleZijlstra
Copy link
Member

type[Literal[...]] is invalid and should be rejected.

@jorenham
Copy link

jorenham commented Dec 6, 2024

type[Literal[...]] is invalid and should be rejected.

>>> from typing import Literal
... class A(str):
...     def __new__(cls, /) -> Literal["a"]:
...         return super().__new__(cls, "a")
...      
>>> A()
'a'

@JelleZijlstra
Copy link
Member

@jorenham I'm not sure what point you are making. type[Literal[...]] isn't meaningful under the typing specification. The upcoming TypeForm PEP will provide a way to express this type.

@jorenham
Copy link

jorenham commented Dec 6, 2024

Wow that was a quick response 😋.

A is a type that only produces Literal["a"], so it appears to be compatible with a type[Literal["a"]]. And as far as I can tell, this isn't (potentially) type-unsafe. So I guess that I'm trying to say that I don't see why it's necessarily "invalid".

@KotlinIsland
Copy link
Contributor

of course type[Literal[...]] is valid? why wouldn't it be?

@sobolevn
Copy link
Member Author

sobolevn commented Dec 6, 2024

Isn't Literal[1] is just 1 and type(1) is int?

@JelleZijlstra
Copy link
Member

type[] represents an instance of type. It doesn't work with arbitrary type annotation objects.

@jorenham
Copy link

jorenham commented Dec 6, 2024

Isn't Literal[1] is just 1 and type(1) is int?

I think it's more accurate to describe it as a refinement type, i.e. a T: int s.t. T in {1}

@jorenham
Copy link

jorenham commented Dec 6, 2024

type[] represents an instance of type

type[S | T] doesn't represent an instance of type, but it's a valid statement.

@JelleZijlstra
Copy link
Member

It is syntactic sugar for type[S] | type[T].

@jorenham
Copy link

jorenham commented Dec 6, 2024

It is syntactic sugar for type[S] | type[T].

yes, which doesn't describe an instance of type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-literal-types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants