fix(typing): preserve decorated function type signatures with ParamSpec#376
fix(typing): preserve decorated function type signatures with ParamSpec#376gencurrent wants to merge 6 commits intopython-cachier:masterfrom
Conversation
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. |
|
The pre-commit.ci - pr failure is unrelated to this PR - it's a build error in untokenize (a transitive dep of docformatter v1.7.7 pinned in .pre-commit-config.yaml) on Python 3.12+, where ast.Constant.s was removed. Note that the GitHub Actions pre-commit job passes (different Python version). Could you either:
|
|
I think it's docformatter: cachier uses it, but the project (https://github.com/PyCQA/docformatter) hasn't been released for almost a year, since May of 2025. |
docformatter v1.7.7 transitively pulls untokenize, whose setup.py uses ast.Constant.s (removed in Python 3.12+) and fails to install on pre-commit.ci's runners. Skip the hook on pre-commit.ci until docformatter v1.7.8 ships (which drops the untokenize dep). The pre-commit GitHub Actions job still runs docformatter, so coverage of the hook isn't lost. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
@shaypal5 Ready! |
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:This breaks IDE autocomplete, disables type error detection on wrong arguments, and forces downstream typed codebases to use stubs or type: ignore.
Changes
After:
docstringstep is skipped in the CI pipeline.