Skip to content

Preserve type hints in decorator (#512) #984

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

elecnix
Copy link

@elecnix elecnix commented Apr 26, 2025

The @cached decorator did not preserve type hints, breaking static analysis.

What do these changes do?

This PR fixes type hint preservation in the @cached decorator by using ParamSpec and TypeVar in its implementation.

It also adds a test to verify that type hints are correctly preserved on decorated functions.

Are there changes in behavior for the user?

No user-facing behavior changes, but static analysis and type checking for decorated functions now work as expected in pyright.

On the other hand, mypy does not correctly infer the return type of the decorated function. This appears to be a limitation with mypy.

Related issue number

Fixes #512

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

The @cached decorator did not preserve type hints, breaking static analysis.
@Dreamsorcerer
Copy link
Member

The tasks under the milestone need to be implemented in roughly the order listed:
https://github.com/aio-libs/aiocache/milestone/8

Type annotations will not work correctly before then (we already enabled them in 0.12 and had to disable them again).

@Dreamsorcerer
Copy link
Member

Wait, Github has removed the ordering of milestone issues... that's rather annoying...

@Dreamsorcerer
Copy link
Member

Order may have been:

#684
#625
#671
#609
#573
#608
#610
#512

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.

Typing
2 participants