Skip to content

Function with decorator + required kwargs is not type checked properly #16685

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
dekim24 opened this issue Dec 20, 2023 · 2 comments
Closed

Function with decorator + required kwargs is not type checked properly #16685

dekim24 opened this issue Dec 20, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@dekim24
Copy link

dekim24 commented Dec 20, 2023

Bug Report

If you wrap a function containing required keyword arguments with a decorator, type checking doesn't behave as one might expect - if you call the function and it's missing those kwargs (and/or contains unexpected kwargs), the mypy type check succeeds.

To Reproduce

from functools import lru_cache

@lru_cache(maxsize=128)
def my_function(required_param: str) -> None:
    pass

my_function(color="blue", speed="fast")

Run mypy <filename> - observe that it succeeds
Run python <filename> - observe error

Note that the actual code I was testing in our codebase uses a different decorator. I just picked a random one that is publicly available

Expected Behavior

mypy returns a typecheck error because required_param is not set

Actual Behavior

Success: no issues found in 1 source file

if I try to execute this code, python raises the expected TypeError: my_function() got an unexpected keyword argument 'color'

if I remove the decorator mypy correctly raises a typecheck error.

Your Environment

  • Mypy version used: 1.7.1 (latest)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None that would impact this case
  • Python version used: 3.11.6
@dekim24 dekim24 added the bug mypy got something wrong label Dec 20, 2023
@erictraut
Copy link

This is a duplicate of #16261. For additional details, see #5107 (comment).

This isn't a problem with decorators in general. It's specific to lru_cache, and it's because of the way lru_cache is defined in the typeshed stubs.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Dec 20, 2023
@dekim24
Copy link
Author

dekim24 commented Dec 20, 2023

This is a duplicate of #16261. For additional details, see #5107 (comment).

This isn't a problem with decorators in general. It's specific to lru_cache, and it's because of the way lru_cache is defined in the typeshed stubs.

I agree it's not a mypy bug, so closing the issue is fine - but as I mentioned in the issue description, the actual decorator we implemented in our code is not lru_cache. I did some more research and the underlying issue is that while our function is type hinted, the decorator we wrote was not type hinted.

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