Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add functools.lru_cache plugin support Fixes #16261#19432

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
machin0r wants to merge1 commit intopython:master
base:master
Choose a base branch
Loading
frommachin0r:implement-lru-cache-support

Conversation

machin0r
Copy link

Add support forfunctools.lru_cache decorator,Fixes#16261

Description

Problem: Functions decorated with@lru_cache lose their original type signatures, preventing mypy from validating function calls properly.

Solution:

  • Addedfunctools_lru_cache_callback to preserve function signatures for@lru_cache
  • Addedlru_cache_wrapper_call_callback to validate calls to cached functions
  • Registered callbacks inDefaultPlugin for both decorator creation and wrapper calls
  • Added tests for differentlru_cache patterns

Supported patterns:

  • @lru_cache - bare decorator
  • @lru_cache() - empty parentheses
  • @lru_cache(maxsize=128) - parameters

Example

Before:

fromfunctoolsimportlru_cache@lru_cache()deff(x:int)->str:returnstr(x)f()# No error, mypy can't validate argumentsf("str")# No error, mypy can't check typesAfter:fromfunctoolsimportlru_cache@lru_cache()deff(x:int)->str:returnstr(x)f()# error: Missing positional argument "x" in call to "f"  [call-arg]f("str")# error: Argument 1 to "f" has incompatible type "str"; expected "int"  [arg-type]

Files changed:

  • mypy/plugins/functools.py - Implementation
  • mypy/plugins/default.py - Plugin registration
  • test-data/unit/check-functools.test - Test cases
  • test-data/unit/lib-stub/functools.pyi - Type stub updates

@machin0rmachin0r marked this pull request as draftJuly 13, 2025 17:14
@machin0rmachin0r marked this pull request as ready for reviewJuly 13, 2025 17:47
@github-actionsGitHub Actions

This comment has been minimized.

@machin0rmachin0rforce-pushed theimplement-lru-cache-support branch from9aa3d9f toa4372e8CompareJuly 13, 2025 18:35
@machin0rmachin0r marked this pull request as draftJuly 13, 2025 18:46
 - Add lru_cache callback to functools plugin for type validation - Register callbacks in default plugin for decorator and wrapper calls - Support different lru_cache patterns: @lru_cache, @lru_cache(), @lru_cache(maxsize=N)Fixes issuepython#16261
@machin0rmachin0rforce-pushed theimplement-lru-cache-support branch froma4372e8 tod9d5d00CompareJuly 13, 2025 18:49
@github-actionsGitHub Actions
Copy link
Contributor

Diff frommypy_primer, showing the effect of this PR on open source code:

psycopg (https://github.com/psycopg/psycopg)+ psycopg/psycopg/rows.py:136: error: Argument 2 to "_make_nt" has incompatible type "*Generator[bytes | None, None, None]"; expected "bytes"  [arg-type]archinstall (https://github.com/archlinux/archinstall)+ archinstall/lib/interactions/general_conf.py:160: error: Argument 1 to "list_available_packages" has incompatible type "tuple[Repository, ...]"; expected "tuple[Repository]"  [arg-type]mitmproxy (https://github.com/mitmproxy/mitmproxy)+ mitmproxy/tools/console/common.py:808: error: Argument "request_timestamp" to "format_dns_flow" has incompatible type "float | None"; expected "float"  [arg-type]+ mitmproxy/tools/console/common.py:816: error: Argument "error_message" to "format_dns_flow" has incompatible type "str | None"; expected "str"  [arg-type]core (https://github.com/home-assistant/core)+ homeassistant/components/xiaomi_aqara/config_flow.py:216: error: Argument 1 to "format_mac" has incompatible type "str | None"; expected "str"  [arg-type]+ homeassistant/components/mqtt_room/sensor.py:199: error: Argument 1 to "_slugify_upper" has incompatible type "Any | None"; expected "str"  [arg-type]+ homeassistant/components/esphome/manager.py:468: error: Argument 1 to "format_mac" has incompatible type "str | None"; expected "str"  [arg-type]+ homeassistant/components/esphome/light.py:182: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:191: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:202: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:211: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:212: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:217: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:229: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:246: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:251: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:265: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/light.py:346: error: Argument 1 to "_filter_color_modes" has incompatible type "tuple[Any, ...]"; expected "list[Any]"  [arg-type]+ homeassistant/components/esphome/config_flow.py:541: error: Argument 1 to "format_mac" has incompatible type "str | None"; expected "str"  [arg-type]+ homeassistant/components/esphome/config_flow.py:591: error: Argument 1 to "format_mac" has incompatible type "str | None"; expected "str"  [arg-type]schemathesis (https://github.com/schemathesis/schemathesis)+ src/schemathesis/specs/openapi/stateful/__init__.py:127: error: Argument 2 to "make_response_filter" has incompatible type "tuple[Any, ...]"; expected "Iterator[str]"  [arg-type]+ src/schemathesis/specs/openapi/stateful/__init__.py:127: note: "tuple" is missing following "Iterator" protocol member:+ src/schemathesis/specs/openapi/stateful/__init__.py:127: note:     __next__ibis (https://github.com/ibis-project/ibis)+ ibis/backends/tests/test_temporal.py:2227: error: Argument "exclude" to "_get_backend_names" has incompatible type "tuple[str, str]"; expected "tuple[str]"  [arg-type]+ ibis/backends/tests/test_temporal.py:2245: error: Argument "exclude" to "_get_backend_names" has incompatible type "tuple[str, str, str, str, str, str]"; expected "tuple[str]"  [arg-type]

@machin0rmachin0r marked this pull request as ready for reviewJuly 13, 2025 19:46
@cdce8pcdce8p added the topic-pluginsThe plugin API and ideas for new plugins labelJul 15, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
topic-pluginsThe plugin API and ideas for new plugins
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Parameters for a call to a function wrapped in a lru_cache are not checked
2 participants
@machin0r@cdce8p

[8]ページ先頭

©2009-2025 Movatter.jp