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

Dict unpacking and unions lose Literal typing #4707

Closed
arjenzorgdoc opened this issue Feb 28, 2023 · 2 comments
Closed

Dict unpacking and unions lose Literal typing #4707

arjenzorgdoc opened this issue Feb 28, 2023 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@arjenzorgdoc
Copy link

Describe the bug
When concatenating dicts with Literal keys, I get a type error from pyright.

To Reproduce

from typing import Literal

a: dict[Literal["a","b"], str] = {'a': "x"}
b: dict[Literal["a","b"], str] = {'b': "y"}
b = {**a, **b}
b = a | b
$ npx pyright@1.1.296  test2.py 
No configuration file found.
No pyproject.toml file found.
stubPath /home/arjen/tmp/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
pyright 1.1.296
/home/arjen/tmp/test2.py
  /home/arjen/tmp/test2.py:5:5 - error: Expression of type "dict[str, str]" cannot be assigned to declared type "dict[Literal['a', 'b'], str]" (reportGeneralTypeIssues)
  /home/arjen/tmp/test2.py:6:5 - error: Expression of type "dict[str, str]" cannot be assigned to declared type "dict[Literal['a', 'b'], str]"
    "dict[str, str]" is incompatible with "dict[Literal['a', 'b'], str]"
      TypeVar "_KT@dict" is invariant
        Type "str" cannot be assigned to type "Literal['a', 'b']"
          "str" cannot be assigned to type "Literal['a']" (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations 
Completed in 0.584sec

Expected behavior
Dict unpacking or unions of dict should return the original type.

@erictraut erictraut added the bug Something isn't working label Feb 28, 2023
erictraut pushed a commit that referenced this issue Mar 1, 2023
…ions with dictionary expansion entries. This partially addresses #4707.
erictraut pushed a commit that referenced this issue Mar 1, 2023
…rator with two dicts whose keys or values are defined as literals. This addresses part of #4707.
@erictraut
Copy link
Collaborator

Thanks for the bug report. This will be addressed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Mar 1, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.297, which I just published. It will also be included in a future release of pylance.

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