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

Wrong warning for multiple type parameters if argument passed is a variable #18305

Closed
pksadiq opened this issue Dec 18, 2024 · 2 comments
Closed
Labels
bug mypy got something wrong

Comments

@pksadiq
Copy link

pksadiq commented Dec 18, 2024

Bug Report
When the function parameter is annotated to have some combination of multiple types with |, mypy validation warns
if a variable is passed to the called function.

To Reproduce

from typing import Any

def show(data: dict[int | str, dict[str, str]]):
    print(data)

data = {0: {"message": "Hello world"}}

# Warning
show(data)

# No warning
show({0: {"message": "Hello world"}})

Gist url: https://mypy-play.net/?mypy=latest&python=3.12&gist=2e3c8b121347ac9547e8e84eba7f1017

Expected Behavior

There should be no warning as it's a valid code

Actual Behavior

mypy ./test.py 
test.py:10: error: Argument 1 to "show" has incompatible type "dict[int, dict[str, str]]"; expected "dict[int | str, dict[str, str]]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.13.0 (compiled: yes) as packaged in Debian sid
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12.8
@pksadiq pksadiq added the bug mypy got something wrong label Dec 18, 2024
@brianschubert
Copy link
Collaborator

brianschubert commented Dec 18, 2024

Hi! This is https://mypy.readthedocs.io/en/stable/common_issues.html#invariance-vs-covariance

Like many mutable generic containers, dict is invariant with respect to its type parameters. That means that even though int is a subtype of int | str, dict[int, V] is not a subtype of dict[int | str, V]. To see why that would be unsafe, consider this example:

def foo(x: dict[int | str, str]):
    x["a"] = "bad"

d: dict[int, str] = {1: "good"}
foo(d)

for key in d:
    assert isinstance(key, int)  # boom!

The reason the second example works is because bidirectional inference allows mypy to infer a better type for the dict literal by peeking at the type of the parameter that its being passed to. To make the first example type check, you can try adding an explicit type annotation for data or modifying the signature of show to accept a type that's compatible with dict[int, dict[str, str]].

@brianschubert brianschubert closed this as not planned Won't fix, can't repro, duplicate, stale Dec 18, 2024
@pksadiq
Copy link
Author

pksadiq commented Dec 18, 2024

I see. I can't modify the function signature. I shall annotate my variable instead.

Thanks for the explanation.

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