Skip to content

unpacking dict in function call with optional arguments fails #10008

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

Open
terencehonles opened this issue Feb 2, 2021 · 0 comments
Open

unpacking dict in function call with optional arguments fails #10008

terencehonles opened this issue Feb 2, 2021 · 0 comments
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@terencehonles
Copy link
Contributor

terencehonles commented Feb 2, 2021

Bug Report

Unpacking a dict when calling a function with **kwargs should not fail. This seems to be related to #4771 and #9676, but slightly different since the function definition is wider than those reports.

To Reproduce

from __future__ import annotations

from collections.abc import Mapping
from typing import Any, cast, TYPE_CHECKING


def test(one: int | None = None, **kwargs: Any) -> int | None:
    print(repr(kwargs))
    return one


if TYPE_CHECKING:
    reveal_type(test)
    reveal_type(test())
    reveal_type(test(test='one'))
    reveal_type(test(**{'test': 'one'}))

which outputs:

test.py:13: note: Revealed type is 'def (one: Union[builtins.int, None] =, **kwargs: Any) -> Union[builtins.int, None]'
test.py:14: note: Revealed type is 'Union[builtins.int, None]'
test.py:15: note: Revealed type is 'Union[builtins.int, None]'
test.py:16: error: Argument 1 to "test" has incompatible type "**Dict[str, str]"; expected "Optional[int]"  [arg-type]

Expected Behavior

There is no issue passing kwargs via a dict and you shouldn't need to cast to a Mapping type.

Actual Behavior

There is an error message, but reveal_type(test(**cast(Mapping[str, str], {'test': 'one'}))) (or saving as a temporary variable with Mapping as its type) will allow the code to succeed.

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags: --show-error-codes
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8
@terencehonles terencehonles added the bug mypy got something wrong label Feb 2, 2021
@AlexWaygood AlexWaygood added the topic-runtime-semantics mypy doesn't model runtime semantics correctly label Apr 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

2 participants