Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MyPy does not infer dictionary type correctly #18754

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

Closed
antspy opened this issue Mar 3, 2025 · 1 comment
Closed

MyPy does not infer dictionary type correctly #18754

antspy opened this issue Mar 3, 2025 · 1 comment
Labels
bug mypy got something wrong

Comments

@antspy
Copy link

antspy commented Mar 3, 2025

I have the following code:

  def g(input: Mapping[str, str | Sequence[tuple[str, str]] | Sequence[str]) -> bool: 
     ...

  def main():
     ...
     mydict: dict[str, str] = ...
     mylist: list[str] = ...
     mystring: str = ...
     context = {
      'a': [x for x in mylist if f(x)],
      'b': list(mydict.items())
      'c': mystring,
      }
      g(context)

mypy complains about this code, saying that Argument 2 to "g" has incompatible type "dict[str, object]"; expected "Mapping[str, Sequence[tuple[str, str]] | Sequence[str] | str]"

If I remove the b key, then it works fine - apparently mypy can tell that context is a dict[str, str | list[str]] and thus compatible with the signature for g. But if I add the b key, it gives up and infers a generic dict[str, object] type, which is then incompatible with g.

Worth noting that the type is correctly inferred by Pylance integrated with vscode.

@antspy antspy added the bug mypy got something wrong label Mar 3, 2025
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Mar 3, 2025

Duplicate of #15217 or #13374

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Mar 3, 2025
@hauntsaninja hauntsaninja marked this as a duplicate of #15217 Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants