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

Incorrect behavior for functions with keyword-only arguments and **kwargs #15317

Open
jrast opened this issue May 28, 2023 · 2 comments
Open
Labels
bug mypy got something wrong

Comments

@jrast
Copy link

jrast commented May 28, 2023

Bug Report
mypy reports a arg-type error Argument 2 to "send_command" has incompatible type "**Dict[str, object]"; expected "Optional[float]" [arg-type] for a valid function invocation.

To Reproduce

def send_command(name: str, *, timeout: Optional[float] = None, **params: Any):
    print(name, params, timeout)

send_command("Test", **{"p1": "abc"})

mypy Playground

Expected Behavior
The code above should typecheck without any error.

Actual Behavior
I get the following mypy error:

main.py:7: error: Argument 2 to "send_command" has incompatible type "**Dict[str, str]"; expected "Optional[float]"  [arg-type]

Your Environment

  • Mypy version used: 1.3.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11

See also

@jrast jrast added the bug mypy got something wrong label May 28, 2023
@aripollak
Copy link

I think #11583 might also be somewhat related.

@delicb
Copy link

delicb commented Jan 2, 2024

I just bumped into the same problem. After some digging, I found a workaround - create TypedDict with kwargs and explicitly annotate kwargs dict with it. In case of original problem it would look like this: https://mypy-play.net/?mypy=latest&python=3.11&gist=d0a3e2279a7a6147541570967634f0da

This adds a bit of verbosity and for frequently used functions it might be problematic, but maybe it can help someone (I solved my problem, since I have this situation only in one place).

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

3 participants