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

Literal objects misinterpreted within collections / mappings #7547

Closed
rsokl opened this issue Mar 23, 2024 · 3 comments
Closed

Literal objects misinterpreted within collections / mappings #7547

rsokl opened this issue Mar 23, 2024 · 3 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@rsokl
Copy link

rsokl commented Mar 23, 2024

I am writing a library that does some runtime type checking, so I am working with type-objects a lot. I encountered the following:

>>> from typing import Literal
>>> {Literal[1]: 88}[Literal[1]]  # Argument of type "type[Literal[1]]" cannot be assigned to parameter "key" of type "type[int]" in function "__getitem__"
88

I realize that this is a very unusual use of Literal, and it isn't a big deal for me to just ignore this.

@rsokl rsokl added the bug Something isn't working label Mar 23, 2024
@erictraut
Copy link
Collaborator

What you're doing here is very atypical. In general, pyright assumes that the Literal special form is used only in type expressions. You're using it here in a value expression. It's a heavy lift to fix this everywhere. I'm going to leave this open because I agree it's a bug, but it may not be fixed any time soon.

erictraut added a commit that referenced this issue May 21, 2024
…o textual format for diagnostic messages) so it better handles special forms like `Literal` or unions when used in a value expression. This partly addresses #7547.
erictraut added a commit that referenced this issue May 21, 2024
…o textual format for diagnostic messages) so it better handles special forms like `Literal` or unions when used in a value expression. This partly addresses #7547.
erictraut added a commit that referenced this issue May 21, 2024
erictraut added a commit that referenced this issue May 21, 2024
@erictraut
Copy link
Collaborator

I added enough support to make your case work along with some related use cases. Making Literal and other special forms work in all value expressions is beyond the scope of the current change. This change will be included in the next release of pyright.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label May 21, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.364.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants