GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — improve lru_cache by using ParamSpec by asottile · Pull Request #7771 · python/typeshed

Conversation

@asottile

@asottile asottile commented May 2, 2022 •
edited by AlexWaygood
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

python/mypy#5107

Fixes #6987.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
1 similar comment
@github-actions

This comment has been minimized.

Sign in to view
@asottile

asottile commented May 3, 2022

Copy link
Copy Markdown
Contributor Author

hmmm, this is close but not quite there. seems my little descriptor trick fixes a lot of situations but doesn't quite work for classmethods. also seems to trigger an internal error in mypy (though pyright is ok with it) -- mypy's matching of Callable with ParamSpec seems a little incomplete

I had originally written this idea as a WIP mypy plugin

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

This comment has been minimized.

Sign in to view
@asottile
improve lru_cache by using ParamSpec
34c2521
@github-actions

This comment has been minimized.

Sign in to view
@AlexWaygood

AlexWaygood commented May 3, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Linking to previous typeshed discussion on this topic:

  • functools._lru_cache_wrapper should be a descriptor class, not a callable #6347
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
Merge branch 'master' into functools-lru-cache-paramspec
e7c20d8
@github-actions

This comment has been minimized.

Sign in to view
@AlexWaygood
Merge branch 'master' into functools-lru-cache-paramspec
faaeb5c
@github-actions

This comment has been minimized.

Sign in to view
AlexWaygood
AlexWaygood reviewed Jun 1, 2022
View reviewed changes
Comment thread stdlib/functools.pyi Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood AlexWaygood mentioned this pull request Jun 1, 2022
Crash involving ParamSpec, Concatenate and overloads python/mypy#12922
Closed
@AlexWaygood

AlexWaygood commented Jun 1, 2022

Copy link
Copy Markdown
Member

I've reported the mypy crash at python/mypy#12922.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Jun 7, 2022

Copy link
Copy Markdown
Member

I've reported the mypy crash at python/mypy#12922.

The crash should be fixed in 0.970 now that python/mypy#12953 has been merged :)

asottile reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Jun 12, 2022

Copy link
Copy Markdown
Member

Marking this as deferred until mypy 0.970 has been released.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood AlexWaygood added the status: deferred Issue or PR deferred until some precondition is fixed label Jun 12, 2022
@AlexWaygood AlexWaygood mentioned this pull request Jun 14, 2022
Need to update cachetools decorator type stub with paramspec #4849
Closed
@AlexWaygood
Merge branch 'master' into functools-lru-cache-paramspec
44393b6
AlexWaygood
AlexWaygood reviewed Jul 19, 2022
View reviewed changes
Comment thread stdlib/functools.pyi Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread stdlib/functools.pyi Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
type: ignore
172540d
AlexWaygood
AlexWaygood reviewed Jul 19, 2022
View reviewed changes
Comment thread stdlib/functools.pyi Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood
More ignore
0a1e546
@AlexWaygood AlexWaygood removed the status: deferred Issue or PR deferred until some precondition is fixed label Jul 19, 2022
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@AlexWaygood

AlexWaygood commented Jul 19, 2022

Copy link
Copy Markdown
Member

The patch no longer causes mypy to crash on 0.971, but there's now a stubtest failure. Also, the mypy_primer report is a bit of a mixed bag.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood added 2 commits July 20, 2022 18:05
@AlexWaygood
Fix stubtest issues
c224f26
@AlexWaygood
More stubtest fixes
dd99e4b
@github-actions

This comment has been minimized.

Sign in to view
@github-actions

This comment has been minimized.

Sign in to view
@kkirsche

This comment was marked as off-topic.

Sign in to view
@AlexWaygood

This comment was marked as resolved.

Sign in to view
@AlexWaygood
Merge branch 'master' into functools-lru-cache-paramspec
43e9013
@github-actions

This comment has been minimized.

Sign in to view
@AlexWaygood AlexWaygood added the status: needs decision Needs a final decision by the typeshed maintainers label Aug 20, 2022
@AlexWaygood
Merge branch 'master' into functools-lru-cache-paramspec
1198195
@github-actions

github-actions Bot commented Sep 13, 2022

Copy link
Copy Markdown
Contributor

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

jax (https://github.com/google/jax)
+ jax/experimental/sharding.py:94: error: Signature of "shard_shape" incompatible with supertype "Sharding"  [override]
+ jax/experimental/sharding.py:304: error: Signature of "devices_indices_map" incompatible with supertype "Sharding"  [override]
+ jax/experimental/sharding.py:317: error: Signature of "shard_shape" incompatible with supertype "XLACompatibleSharding"  [override]
+ jax/experimental/sharding.py:317: error: Signature of "shard_shape" incompatible with supertype "Sharding"  [override]
+ jax/experimental/sharding.py:378: error: Signature of "devices_indices_map" incompatible with supertype "Sharding"  [override]

manticore (https://github.com/trailofbits/manticore)
+ manticore/core/smtlib/solver.py:505: error: Signature of "can_be_true" incompatible with supertype "Solver"

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

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

jinja (https://github.com/pallets/jinja)
+ src/jinja2/environment.py:1184: error: Unused "type: ignore" comment
+ src/jinja2/environment.py:1199: error: Unused "type: ignore" comment

ibis (https://github.com/ibis-project/ibis)
+ ibis/backends/duckdb/tests/conftest.py:19: error: Too many arguments for "__call__" of "_lru_cache_wrapper_0"
+ ibis/backends/duckdb/tests/conftest.py:53: error: Signature of "connect" incompatible with supertype "BackendTest"

paroxython (https://github.com/laowantong/paroxython)
+ paroxython/assess_costs.py:85: error: Incompatible types in assignment (expression has type "_lru_cache_wrapper[int, [int], float]", variable has type "_lru_cache_wrapper_0[[int], float]")

pylint (https://github.com/pycqa/pylint)
+ pylint/checkers/classes/class_checker.py:2002: error: Returning Any from function declared to return "Union[SupportsDunderLT[Any], SupportsDunderGT[Any]]"  [no-any-return]

rich (https://github.com/Textualize/rich)
+ rich/progress_bar.py:145: error: Argument 3 to "__call__" of "_lru_cache_wrapper_0" has incompatible type "Optional[str]"; expected "str"  [arg-type]

zulip (https://github.com/zulip/zulip)
+ zerver/lib/cache.py:744: error: "_lru_cache_wrapper" expects 3 type arguments, but 1 given  [type-arg]
+ zerver/lib/cache.py:761: error: Unused "type: ignore" comment

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/connector.py:935: error: Too many arguments for "__call__" of "_lru_cache_wrapper_0"  [call-arg]
+ aiohttp/connector.py:941: error: Too many arguments for "__call__" of "_lru_cache_wrapper_0"  [call-arg]
+ aiohttp/connector.py:942: error: Too many arguments for "__call__" of "_lru_cache_wrapper_0"  [call-arg]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asottile asottile closed this Sep 13, 2022
@asottile
asottile deleted the functools-lru-cache-paramspec branch September 13, 2022 16:01
@asottile

asottile commented Sep 13, 2022

Copy link
Copy Markdown
Contributor Author

this is going to go the same way as #7430 so I'm just going to retract it

Hnasar reacted with thumbs down emoji
All reactions
  • 👎 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Greedquest

Greedquest commented Nov 26, 2022

Copy link
Copy Markdown

Hello, has this been implemented somewhere. lru_cache and cache would really benefit from ParamSpec they seem like the textbook usecase. Right now mypy is seeing the function args as Any which means my callsites are not enforced.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kkirsche

kkirsche commented Nov 26, 2022

Copy link
Copy Markdown
Contributor

I don't believe so. I know when I tried it originally, I had run into some problems getting everything to work with the various dunder (double underscore) properties. Some of my personal obligations are clearing up so I may be able to take another stab at this. I may have still misunderstood ParamSpec at that time (I wanted it to be more like typescript function parameter utility type). At the very least, giving it another shot should allow me to articulate more clearly what specific issues I ran into in case others may be able to help solve those.

Greedquest and jrdnh reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Nov 26, 2022

Copy link
Copy Markdown
Member

I believe the difficulty is in dealing with __get__ to make it work correctly as both a function and a method decorator.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood AlexWaygood mentioned this pull request Feb 20, 2023
functools: Add typing for decorated function arguments in cache API #9768
Closed
@AlexWaygood AlexWaygood mentioned this pull request Jan 16, 2024
@functools.cache destroys the function signature #11280
Closed
@srittau srittau removed the status: needs decision Needs a final decision by the typeshed maintainers label Mar 15, 2024
@AlexWaygood AlexWaygood mentioned this pull request Mar 26, 2024
Use ParamSpec for lru_cache #11662
Open
@srittau srittau mentioned this pull request Sep 23, 2025
Add tests for @functools.cache #14768
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AlexWaygood AlexWaygood AlexWaygood left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

functools.lru_cache wrapped function with TypeVar has wrong return type.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@asottile @AlexWaygood @kkirsche @Greedquest @JelleZijlstra @srittau
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.