python / python/mypy

Parameters for a call to a function wrapped in a lru_cache are not checked

Open
#16,261 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

The parameters used in a call to a function which is wrapped in a functools.lru_cache are not checked.

To Reproduce

from datetime import date
from functools import lru_cache

@lru_cache
def f(v: str, at: date) -> str:
    return v

f()
f("abc")
f("abc", date.today())
f("abc", now=date.today())
f("abc", at=date.today())
f("abc", at="wrong_type")

Expected Behavior

This should raise several errors about the arguments being wrong. Commenting out the line @lru_cache correctly generates the following errors.

lru_cache_type_test.py:8: error: Missing positional arguments "v", "at" in call to "f"  [call-arg]
lru_cache_type_test.py:9: error: Missing positional argument "at" in call to "f"  [call-arg]
lru_cache_type_test.py:11: error: Unexpected keyword argument "now" for "f"  [call-arg]
lru_cache_type_test.py:13: error: Argument "at" to "f" has incompatible type "str"; expected "date"  [arg-type]

Actual Behavior

Success: no issues found in 1 source file

Your Environment

mypy 1.6.0 running on Python 3.11.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer described in lru_cache_type_test.py and run mypy with and without @lru_cache to confirm the difference. Trace how functools.lru_cache-wrapped functions are analyzed, then add coverage for missing arguments, unexpected keywords, and incompatible types; done means these calls report the expected errors while valid calls remain accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.