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

lru_cache preserves signature of wrapped function #6221

Merged
merged 4 commits into from
Nov 3, 2021

Conversation

Tomaz-Vieira
Copy link
Contributor

This PR uses ParamSpec to make lru_cache preserve the signature of the wrapped function.

Unfortunately this removes the constraint that *args and **kwargs must be Hashable at least until the meaning of the bound parameter in ParamSpec is decided, but I feel that preserving the function signature is a worthy trade-off.

@Tomaz-Vieira
Copy link
Contributor Author

So it seems mypy doesn't support ParamSpec yet. Could anyone point me to the correct course of action here? Should I just wait until it does?

@srittau
Copy link
Collaborator

srittau commented Nov 2, 2021

You can # type: ignore the affected lines for now.

@Tomaz-Vieira Tomaz-Vieira force-pushed the param_spec_for_lru_cache branch from 5d5cce1 to 850b2b7 Compare November 2, 2021 15:48
@Tomaz-Vieira Tomaz-Vieira force-pushed the param_spec_for_lru_cache branch from 850b2b7 to 130c84b Compare November 2, 2021 15:50
@github-actions

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

For future reference: Please don't force push, we squash on merge, but force pushing makes reviewing harder.

@srittau
Copy link
Collaborator

srittau commented Nov 2, 2021

I'm not sure why pytype complains. Maybe @rchen152 has some insight.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@srittau
Copy link
Collaborator

srittau commented Nov 2, 2021

The new primer warnings are probably due to the missing ParamSpec support in mypy and can be ignored.

@srittau
Copy link
Collaborator

srittau commented Nov 2, 2021

@Tomaz-Vieira You could try merging master, where we just updated the pytype version used. Maybe that will help.

@rchen152
Copy link
Collaborator

rchen152 commented Nov 2, 2021

Oh, I think pytype only recognizes paramspecs in Callables right now. So the Generic[_P] is probably what's tripping it up. Let me see if I can fix that.

@github-actions

This comment has been minimized.

rchen152 added a commit to google/pytype that referenced this pull request Nov 2, 2021
This is to unblock python/typeshed#6221.
Also see #786.

PiperOrigin-RevId: 407168815
@rchen152
Copy link
Collaborator

rchen152 commented Nov 2, 2021

I just released a pytype version (2021.11.2) that should fix the pytype_test failure.

@srittau
Copy link
Collaborator

srittau commented Nov 2, 2021

@Tomaz-Vieira could you update the pytype version in the requirements file?

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2021

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

isort (https://github.com/pycqa/isort.git)
+ isort/place.py:17: error: Returning Any from function declared to return "str"

pydantic (https://github.com/samuelcolvin/pydantic.git)
+ pydantic/tools.py:34: error: unused "type: ignore" comment

pandas (https://github.com/pandas-dev/pandas.git)
+ pandas/core/dtypes/cast.py:498: error: unused "type: ignore" comment

aiohttp (https://github.com/aio-libs/aiohttp.git)
+ aiohttp/multipart.py:516: error: Returning Any from function declared to return "str"  [no-any-return]
+ aiohttp/multipart.py:690: error: Returning Any from function declared to return "str"  [no-any-return]
+ aiohttp/connector.py:918: error: Returning Any from function declared to return "Optional[SSLContext]"  [no-any-return]
+ aiohttp/connector.py:924: error: Returning Any from function declared to return "Optional[SSLContext]"  [no-any-return]
+ aiohttp/connector.py:925: error: Returning Any from function declared to return "Optional[SSLContext]"  [no-any-return]
+ aiohttp/client_reqrep.py:1010: error: Returning Any from function declared to return "str"  [no-any-return]

pytest (https://github.com/pytest-dev/pytest.git)
- src/_pytest/config/__init__.py:1683: error: Argument 1 to "filterwarnings" has incompatible type "*Tuple[str, str, Type[Warning], str, int]"; expected "Union[Literal['default'], Literal['error'], Literal['ignore'], Literal['always'], Literal['module'], Literal['once']]"  [arg-type]
- src/_pytest/config/__init__.py:1686: error: Argument 1 to "filterwarnings" has incompatible type "*Tuple[str, str, Type[Warning], str, int]"; expected "Union[Literal['default'], Literal['error'], Literal['ignore'], Literal['always'], Literal['module'], Literal['once']]"  [arg-type]
+ src/_pytest/python.py:946: error: Unexpected keyword argument "indices" for "CallSpec2"  [call-arg]

edgedb (https://github.com/edgedb/edgedb.git)
+ edb/schema/objects.py:997: error: unused "type: ignore" comment
+ edb/schema/objects.py:1992:9: error: Returning Any from function declared to return "QualName"
+ edb/schema/objects.py:2223: error: unused "type: ignore" comment
+ edb/schema/objects.py:2637:9: error: Returning Any from function declared to return "Name"
+ edb/schema/schema.py:1146:9: error: Returning Any from function declared to return "FrozenSet[Cast]"
+ edb/schema/schema.py:1156:9: error: Returning Any from function declared to return "FrozenSet[Cast]"
+ edb/schema/schema.py:1166:9: error: Returning Any from function declared to return "FrozenSet[so.Object_T]"
+ edb/schema/schema.py:1412:9: error: Returning Any from function declared to return "Optional[Migration]"
+ edb/schema/scalars.py:132:9: error: Returning Any from function declared to return "bool"
+ edb/schema/scalars.py:163:9: error: Returning Any from function declared to return "bool"
+ edb/schema/scalars.py:176:9: error: Returning Any from function declared to return "int"
+ edb/schema/casts.py:91:5: error: Returning Any from function declared to return "bool"
+ edb/schema/casts.py:119:21: error: Returning Any from function declared to return "Optional[Type]"

@srittau srittau merged commit 8bda66a into python:master Nov 3, 2021
@Tomaz-Vieira Tomaz-Vieira deleted the param_spec_for_lru_cache branch November 3, 2021 09:24
JukkaL added a commit to JukkaL/typeshed that referenced this pull request Nov 22, 2021
This reverts commit 8bda66a.

The change causes issues with ParamSpec implementations in type
checkers, at least pyright and my work-in-progress support for
ParamSpec in mypy. It's not yet clear how to fix the issues, so I
think that it's best to revert this, at least temporarily until we've
found a good solution. See python#6347 for context.
srittau pushed a commit that referenced this pull request Nov 22, 2021
…6356)

This reverts commit 8bda66a.

The change causes issues with ParamSpec implementations in type
checkers, at least pyright and my work-in-progress support for
ParamSpec in mypy. It's not yet clear how to fix the issues, so I
think that it's best to revert this, at least temporarily until we've
found a good solution. See #6347 for context.
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.

3 participants