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

Use ParamSpec for lru_cache/cache #11767

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

Viicos
Copy link
Contributor

@Viicos Viicos commented Apr 16, 2024

No description provided.

This comment has been minimized.

@Viicos
Copy link
Contributor Author

Viicos commented Apr 16, 2024

This unfortunately doesn't work with default values:

@overload
def cache(user_function: Callable[Concatenate[_S, _P], _R], /) -> _lru_cache_wrapper[_S, _P, _R]: ...
@overload
def cache(user_function: Callable[_P, _R], /) -> _lru_cache_wrapper_0[_P, _R]: ...


@cache
def get_int(arg: str | None = None) -> int: ...


# The first `cache` overload was used, meaning `arg` matches the TypeVar `_S` which doesn't account for default values
# Inverting the definition order of overloads fixes the issue, but then it brakes for methods :/
reveal_type(get_logger)  # _lru_cache_wrapper[str | None, (), int]
get_logger()  # Expected 1 more positional argument

Co-authored-by: Anthony Sottile <asottile@umich.edu>
Copy link
Contributor

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

paroxython (https://github.com/laowantong/paroxython)
+ paroxython/assess_costs.py:85: error: No overload variant of "__get__" of "_lru_cache_wrapper" matches argument types "LearningCostAssessor", "type[LearningCostAssessor]"  [call-overload]
+ paroxython/assess_costs.py:85: note: Possible overload variants:
+ paroxython/assess_costs.py:85: note:     def __get__(self, int, type[Any] | None = ..., /) -> _lru_cache_wrapper_0[[int], float]
+ paroxython/assess_costs.py:85: note:     def __get__(self, None, type[Any] | None = ..., /) -> _lru_cache_wrapper[int, [int], float]

pip (https://github.com/pypa/pip)
+ src/pip/_internal/models/link.py:245: error: Missing positional argument "url" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ src/pip/_internal/models/link.py:245: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[LinkHash]"  [arg-type]
+ src/pip/_internal/index/package_finder.py:900: error: Argument 1 to "__call__" of "_lru_cache_wrapper_0" has incompatible type "str | None"; expected "str"  [arg-type]

pydantic (https://github.com/samuelcolvin/pydantic)
+ pydantic/v1/tools.py:37: error: Unused "type: ignore" comment  [unused-ignore]

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/utilities/asyncutils.py:45: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ src/prefect/deployments/base.py:389: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ src/prefect/deployments/base.py:395: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ src/prefect/workers/utilities.py:52: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ src/prefect/flow_runs.py:78: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]

paasta (https://github.com/yelp/paasta)
+ paasta_tools/delete_kubernetes_deployments.py:71: error: Unexpected keyword argument "kube_client" for "__call__" of "_lru_cache_wrapper"  [call-arg]
+ note: "__call__" of "_lru_cache_wrapper" defined here
+ paasta_tools/setup_kubernetes_cr.py:194: error: Unexpected keyword argument "kube_client" for "__call__" of "_lru_cache_wrapper"  [call-arg]
+ note: "__call__" of "_lru_cache_wrapper" defined here
+ paasta_tools/cli/cmds/validate.py:796: error: Unexpected keyword argument "service" for "__call__" of "_lru_cache_wrapper"  [call-arg]
+ note: "__call__" of "_lru_cache_wrapper" defined here

typeshed-stats (https://github.com/AlexWaygood/typeshed-stats)
+ src/typeshed_stats/gather.py:407: error: Returning Any from function declared to return "str | None"  [no-any-return]
+ src/typeshed_stats/gather.py:860: error: Returning Any from function declared to return "str | None"  [no-any-return]

manticore (https://github.com/trailofbits/manticore)
+ manticore/core/smtlib/solver.py:505: error: Signature of "can_be_true" incompatible with supertype "Solver"  [override]
+ manticore/core/smtlib/solver.py:505: note:      Superclass:
+ manticore/core/smtlib/solver.py:505: note:          def can_be_true(self, constraints: Any, expression: Any = ...) -> bool
+ manticore/core/smtlib/solver.py:505: note:      Subclass:
+ manticore/core/smtlib/solver.py:505: note:          _lru_cache_wrapper[SMTLIBSolver, [ConstraintSet, bool | Bool], bool]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/dtypes/cast.py:592: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/core/apply.py:981: error: Unused "type: ignore" comment  [unused-ignore]

mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ mitmproxy/proxy/mode_specs.py:165: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/connection.py:181: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/connection.py:181: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Never]"  [arg-type]
+ mitmproxy/tools/console/common.py:811: error: Argument "error_message" to "__call__" of "_lru_cache_wrapper_0" has incompatible type "str | None"; expected "str"  [arg-type]
+ mitmproxy/tools/console/common.py:848: error: Incompatible types in assignment (expression has type "_lru_cache_wrapper_0[[NamedArg(RenderMode, 'render_mode'), NamedArg(bool, 'focused'), NamedArg(str, 'marked'), NamedArg(str | None, 'is_replay'), NamedArg(str, 'request_method'), NamedArg(str, 'request_scheme'), NamedArg(str, 'request_host'), NamedArg(str, 'request_path'), NamedArg(str, 'request_url'), NamedArg(str, 'request_http_version'), NamedArg(float, 'request_timestamp'), NamedArg(bool, 'request_is_push_promise'), NamedArg(bool, 'intercepted'), NamedArg(int | None, 'response_code'), NamedArg(str | None, 'response_reason'), NamedArg(int | None, 'response_content_length'), NamedArg(str | None, 'response_content_type'), NamedArg(float | None, 'duration'), NamedArg(str | None, 'error_message')], Any]", variable has type "_lru_cache_wrapper_0[[NamedArg(RenderMode, 'render_mode'), NamedArg(bool, 'focused'), NamedArg(str, 'marked'), NamedArg(bool, 'is_replay'), NamedArg(str, 'request_method'), NamedArg(str, 'request_scheme'), NamedArg(str, 'request_host'), NamedArg(str, 'request_path'), NamedArg(str, 'request_url'), NamedArg(str, 'request_http_version'), NamedArg(float, 'request_timestamp'), NamedArg(bool, 'request_is_push_promise'), NamedArg(bool, 'intercepted'), NamedArg(int | None, 'response_code'), NamedArg(str | None, 'response_reason'), NamedArg(int | None, 'response_content_length'), NamedArg(str | None, 'response_content_type'), NamedArg(float | None, 'duration'), NamedArg(str | None, 'error_message')], Any]")  [assignment]
+ mitmproxy/tools/console/common.py:853: error: Argument "is_replay" to "__call__" of "_lru_cache_wrapper_0" has incompatible type "str | None"; expected "bool"  [arg-type]
+ mitmproxy/test/tflow.py:134: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/test/tflow.py:134: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Never]"  [arg-type]
+ mitmproxy/test/tflow.py:235: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/test/tflow.py:235: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Never]"  [arg-type]
+ mitmproxy/proxy/server.py:569: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/proxy/server.py:569: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Never]"  [arg-type]
+ mitmproxy/proxy/mode_servers.py:116: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/proxy/mode_servers.py:116: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Never]"  [arg-type]
+ mitmproxy/addons/clientplayback.py:98: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/addons/clientplayback.py:98: error: Need type annotation for "mode"  [var-annotated]
+ mitmproxy/addons/proxyserver.py:104: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/addons/proxyserver.py:104: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Never]"  [arg-type]
+ mitmproxy/addons/proxyserver.py:248: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/addons/proxyserver.py:288: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/master.py:135: error: Missing positional argument "spec" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mitmproxy/master.py:135: error: Need type annotation for "mode"  [var-annotated]

pylint (https://github.com/pycqa/pylint)
+ pylint/checkers/utils.py:2262: error: "_lru_cache_wrapper" expects 3 type arguments, but 1 given  [type-arg]
+ pylint/checkers/utils.py:2262: error: Missing type parameters for generic type "_lru_cache_wrapper"  [type-arg]

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/connector.py:980:17: error: Returning Any from function declared to return "SSLContext | None"  [no-any-return]
+ aiohttp/connector.py:980:17: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-no-any-return for more info
+ aiohttp/connector.py:980:24: error: No overload variant of "__get__" of "_lru_cache_wrapper" matches argument types "TCPConnector", "type[TCPConnector]"  [call-overload]
+ aiohttp/connector.py:980:24: note: Possible overload variants:
+ aiohttp/connector.py:980:24: note:     def __get__(self, bool, type[Any] | None = ..., /) -> _lru_cache_wrapper_0[[], SSLContext]
+ aiohttp/connector.py:980:24: note:     def __get__(self, None, type[Any] | None = ..., /) -> _lru_cache_wrapper[bool, [], SSLContext]
+ aiohttp/connector.py:980:24: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-call-overload for more info
+ aiohttp/connector.py:986:17: error: Returning Any from function declared to return "SSLContext | None"  [no-any-return]
+ aiohttp/connector.py:986:24: error: No overload variant of "__get__" of "_lru_cache_wrapper" matches argument types "TCPConnector", "type[TCPConnector]"  [call-overload]
+ aiohttp/connector.py:986:24: note: Possible overload variants:
+ aiohttp/connector.py:986:24: note:     def __get__(self, bool, type[Any] | None = ..., /) -> _lru_cache_wrapper_0[[], SSLContext]
+ aiohttp/connector.py:986:24: note:     def __get__(self, None, type[Any] | None = ..., /) -> _lru_cache_wrapper[bool, [], SSLContext]
+ aiohttp/connector.py:987:13: error: Returning Any from function declared to return "SSLContext | None"  [no-any-return]
+ aiohttp/connector.py:987:20: error: No overload variant of "__get__" of "_lru_cache_wrapper" matches argument types "TCPConnector", "type[TCPConnector]"  [call-overload]
+ aiohttp/connector.py:987:20: note: Possible overload variants:
+ aiohttp/connector.py:987:20: note:     def __get__(self, bool, type[Any] | None = ..., /) -> _lru_cache_wrapper_0[[], SSLContext]
+ aiohttp/connector.py:987:20: note:     def __get__(self, None, type[Any] | None = ..., /) -> _lru_cache_wrapper[bool, [], SSLContext]

rich (https://github.com/Textualize/rich)
+ rich/theme.py:25: error: Missing positional argument "style_definition" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/theme.py:25: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Style]"  [arg-type]
+ rich/theme.py:54: error: Missing positional argument "style_definition" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/theme.py:54: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Style]"  [arg-type]
+ rich/style.py:147: error: Missing positional argument "color" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/style.py:147: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Color]"  [arg-type]
+ rich/style.py:391: error: Missing positional argument "style_definition" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/style.py:391: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Style]"  [arg-type]
+ rich/style.py:527: error: Missing positional argument "color" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/style.py:527: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Color]"  [arg-type]
+ rich/style.py:554: error: Missing positional argument "color" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/style.py:554: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Color]"  [arg-type]
+ rich/segment.py:168: error: Returning Any from function declared to return "tuple[Segment, Segment]"  [no-any-return]
+ rich/segment.py:168: error: No overload variant of "__get__" of "_lru_cache_wrapper" matches argument types "Segment", "type[Segment]"  [call-overload]
+ rich/segment.py:168: note: Possible overload variants:
+ rich/segment.py:168: note:     def __get__(self, type[Segment], type[Any] | None = ..., /) -> _lru_cache_wrapper_0[[Segment, int], tuple[Segment, Segment]]
+ rich/segment.py:168: note:     def __get__(self, None, type[Any] | None = ..., /) -> _lru_cache_wrapper[type[Segment], [Segment, int], tuple[Segment, Segment]]
+ rich/markup.py:163: error: Missing positional argument "style" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/markup.py:163: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Style]"  [arg-type]
+ rich/markup.py:220: error: Missing positional argument "style" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/markup.py:220: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Style]"  [arg-type]
+ rich/console.py:1477: error: Missing positional argument "style_definition" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/console.py:1477: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Style]"  [arg-type]
+ rich/ansi.py:175: error: Missing positional argument "style_definition" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ rich/ansi.py:175: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "str"; expected "type[Style]"  [arg-type]
+ rich/progress_bar.py:145: error: Argument 3 to "__call__" of "_lru_cache_wrapper_0" has incompatible type "str | None"; expected "str"  [arg-type]

dacite (https://github.com/konradhalas/dacite)
+ dacite/cache.py:11: error: Unused "type: ignore" comment  [unused-ignore]

isort (https://github.com/pycqa/isort)
+ isort/deprecated/finders.py:296: error: No overload variant of "__get__" of "_lru_cache_wrapper" matches argument types "RequirementsFinder", "Type[RequirementsFinder]"  [call-overload]
+ isort/deprecated/finders.py:296: note: Possible overload variants:
+ isort/deprecated/finders.py:296: note:     def __get__(self, Type[RequirementsFinder], Optional[Type[Any]] = ..., /) -> _lru_cache_wrapper_0[[str], List[str]]
+ isort/deprecated/finders.py:296: note:     def __get__(self, None, Optional[Type[Any]] = ..., /) -> _lru_cache_wrapper[Type[RequirementsFinder], [str], List[str]]
+ isort/deprecated/finders.py:327: error: No overload variant of "__get__" of "_lru_cache_wrapper" matches argument types "RequirementsFinder", "Type[RequirementsFinder]"  [call-overload]
+ isort/deprecated/finders.py:327: note: Possible overload variants:
+ isort/deprecated/finders.py:327: note:     def __get__(self, Type[RequirementsFinder], Optional[Type[Any]] = ..., /) -> _lru_cache_wrapper_0[[str], List[str]]
+ isort/deprecated/finders.py:327: note:     def __get__(self, None, Optional[Type[Any]] = ..., /) -> _lru_cache_wrapper[Type[RequirementsFinder], [str], List[str]]

psycopg (https://github.com/psycopg/psycopg)
+ psycopg/psycopg/rows.py:141: error: Argument 2 to "__call__" of "_lru_cache_wrapper" has incompatible type "*Generator[bytes | None, None, None]"; expected "bytes"  [arg-type]

poetry (https://github.com/python-poetry/poetry)
+ src/poetry/mixology/version_solver.py:125: error: Returning Any from function declared to return "list[DependencyPackage]"  [no-any-return]

mkosi (https://github.com/systemd/mkosi)
+ mkosi/user.py:27:16: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/user.py:28:16: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/user.py:29:24: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/user.py:50:21: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/user.py:55:32: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/user.py:65:67: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/user.py:66:21: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/sandbox.py:170:77: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/sandbox.py:224:20: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/sandbox.py:227:35: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/config.py:520:20: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/__init__.py:511:22: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/__init__.py:1578:20: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/__init__.py:1579:20: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/__init__.py:1586:25: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/__init__.py:2532:61: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/__init__.py:4117:28: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]
+ mkosi/__init__.py:4352:22: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]

ibis (https://github.com/ibis-project/ibis)
+ ibis/backends/tests/test_temporal.py:2367: error: Argument "exclude" to "__call__" of "_lru_cache_wrapper_0" has incompatible type "tuple[str, str, str, str]"; expected "tuple[str]"  [arg-type]
+ ibis/backends/tests/test_temporal.py:2385: error: Argument "exclude" to "__call__" of "_lru_cache_wrapper_0" has incompatible type "tuple[str, str, str, str, str, str, str, str]"; expected "tuple[str]"  [arg-type]
+ ibis/backends/tests/test_dot_sql.py:235: error: Argument "exclude" to "__call__" of "_lru_cache_wrapper_0" has incompatible type "tuple[str, str]"; expected "tuple[str]"  [arg-type]

jinja (https://github.com/pallets/jinja)
+ src/jinja2/environment.py:1186: error: Unused "type: ignore" comment  [unused-ignore]
+ src/jinja2/environment.py:1201: error: Unused "type: ignore" comment  [unused-ignore]

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ bson/datetime_ms.py:141: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "tzinfo"; expected "timezone"  [arg-type]
+ bson/datetime_ms.py:143: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "tzinfo"; expected "timezone"  [arg-type]
+ bson/json_util.py:847: error: Missing positional argument "_self" in call to "__call__" of "_lru_cache_wrapper"  [call-arg]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant