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

Erroneous arg-type error with Mapping and reversed Mapping #18436

Open
kohlrabi opened this issue Jan 10, 2025 · 1 comment
Open

Erroneous arg-type error with Mapping and reversed Mapping #18436

kohlrabi opened this issue Jan 10, 2025 · 1 comment
Labels
bug mypy got something wrong

Comments

@kohlrabi
Copy link

I get an arg-type error in MyPy 1.14.1 on the following code using a Union of inverted collections.abc.Mapping:

from collections.abc import Mapping


def fun(x: Mapping[str, int | None] | Mapping[int | None, str]):
    pass


fun({"a": 1, "b": 2})

fun({"a": 1, "b": 2, "c": None})

fun({1: "a", 2: "b"})

fun({1: "a", 2: "b", None: "c"})

The error is:

mypy_type_error.py:12: error: Argument 1 to "fun" has incompatible type "dict[int, str]"; expected "Mapping[str, int | None] | Mapping[int | None, str]" [arg-type]

That is, MyPy takes issue with the parameters in the third function call. The last call including the None key does not produce an error, though. I'd expect the third call to not show an error, either.

@kohlrabi kohlrabi added the bug mypy got something wrong label Jan 10, 2025
@A5rocks
Copy link
Contributor

A5rocks commented Jan 10, 2025

This is an inference issue. Note the revealed type:

from collections.abc import Mapping


def fun(x: Mapping[str, int | None] | Mapping[int | None, str]):
    pass

fun(reveal_type({1: "a", 2: "b"}))  # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"

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