fix(typing): preserve decorated function type signatures with ParamSpec#376
fix(typing): preserve decorated function type signatures with ParamSpec#376gencurrent wants to merge 4 commits intopython-cachier:masterfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #376 +/- ##
==========================================
- Coverage 99.95% 99.90% -0.05%
==========================================
Files 16 16
Lines 2098 2110 +12
Branches 251 252 +1
==========================================
+ Hits 2097 2108 +11
- Partials 1 2 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
shaypal5
left a comment
There was a problem hiding this comment.
Looks good. Let's wait and see what CoPilot says. :)
There was a problem hiding this comment.
Pull request overview
This PR aims to improve Cachier’s static typing experience by preserving decorated function signatures/return types (especially for tools like mypy/Pyrefly) using ParamSpec + TypeVar, and adds a mypy-driven test suite to validate the behavior.
Changes:
- Add
ParamSpec/TypeVargenerics tocachier()and its internal decorator/wrapper to preserve function signatures through decoration. - Add
tests/test_typing.pywhich runs mypy programmatically to assert signature/return-type preservation for sync/async functions and some decorator argument variants. - Add
mypytotests/requirements.txtso the typing tests can run in CI.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
src/cachier/core.py |
Introduces generic typing for the decorator/wrapper to preserve callable signatures and return types. |
tests/test_typing.py |
Adds mypy-based tests that validate the preserved typing behavior via reveal_type and error assertions. |
tests/requirements.txt |
Adds mypy as a test dependency to enable running the typing tests. |
|
Now there's a test fail with |
I don't know. I'm looking at it now, and the only red flow is codecov's. Please take a look at CoPilot's comments, treat anything valid, resolve as rejected anything that's not, raise patch test coverage to 100% (it's currently at 96.42%), and i'll merge this sucker. |
Perhaps it failed just once yesterday between some of my commits. |
I encounter this issue with Pyrefly constantly: the declared return type of the decorated function / method does not match the expected return type, so I have to add
# ... ignore ...comments.The
@cachierdecorator erases all type information from decorated functions. Becausecachier()has no return type annotation and no generic type variables, type checkers see every decorated function -- both its signature and return value -- asAny: