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 not inferred as TypedDict in generator expression #3948

Closed
goodmami opened this issue Sep 15, 2022 · 2 comments
Closed

Literal not inferred as TypedDict in generator expression #3948

goodmami opened this issue Sep 15, 2022 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request

Comments

@goodmami
Copy link

Describe the bug

Literal dicts produced in a generator expression are not inferred as compatible with declared TypedDicts while they are inferred properly in list comprehensions.

To Reproduce

from typing import TypedDict

class X(TypedDict):
    x: str

xs: list[X] = []
xs.extend({"x": c} for c in 'abc')  # "dict[str, str]" is incompatible with "X"
xs.extend([{"x": c} for c in 'abc'])  # no problem in list comprehension

Expected behavior

I expect the generator expression to behave the same as the list comprehension. In the above MWE, xs.extend() expects an argument with type Iterable[X], and both the generator expression and the list comprehension provide that.

VS Code extension or command-line

pyright version 1.1.271 on the command line:

$ python -m pyright test.py
No configuration file found.
No pyproject.toml file found.
stubPath /home/goodmami/tst/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
pyright 1.1.271
/home/goodmami/tst/test.py
  /home/goodmami/tst/test.py:7:11 - error: Argument of type "Generator[dict[str, str], None, None]" cannot be assigned to parameter "__iterable" of type "Iterable[X]" in function "extend"
    TypeVar "_T_co@Iterable" is covariant
      "dict[str, str]" is incompatible with "X" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations 
Completed in 0.526sec

Additional context

Seems related to #2967.

@erictraut erictraut added the enhancement request New feature or request label Sep 15, 2022
erictraut pushed a commit that referenced this issue Sep 15, 2022
@erictraut
Copy link
Collaborator

Thanks for the enhancement request. I've added support for bidirectional type inference for comprehensions, so the above code type checks without error.

This will be included in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Sep 15, 2022
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.272. 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 enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants