type hint FunctionWrapper#288
type hint FunctionWrapper#288grayjk wants to merge 4 commits intoGrahamDumpleton:developfrom grayjk:type-hint-func-wrapper
Conversation
|
I thought adding type hints incrementally might be more amenable This diff follows the guidance from https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators The printing_decorator example using legacy syntax (Python 3.11 and earlier) |
|
Part of the reason for trying to catch up with backlog of stuff in wrapt such as removing any Python 2.X code and increment to new major version has been to prepare for finally adding the type hint stuff. Because I don't know enough about subtleties of type hints I still have couple of points of concern before charging into it. The first is whether now that Python 2.X and older Python 3.X version support has been removed it is better to include the type hints directly in the Python code. I am still not sure whether this is possible though since most of the time people would be relying on the C extension. Can tools defer to the hints in the Python code even though it may be implemented in the C code. The second is how to test things. I have been uncomfortable with adding it all without a way to confirm that it is done properly and that I don't break it over time. The comment above with the Can you see a strategy for how we could add mypy driven tests as part of repository that can trigger locally and from GitHub actions? Is using separate Also, to keep this going, it may be better that I create a feature branch in GitHub and we create PRs and merge against that. That way the whole branch can be checked out more easily to work on it incrementally and test it and know not upsetting |
|
I used a .pyi file based on https://peps.python.org/pep-0484/#stub-files. Since wrapt includes a C extension, it would fall into the first use case for a stub file. A way forward for testing might be https://github.com/TypedDjango/pytest-mypy-plugins. The attrs project uses it: https://github.com/python-attrs/attrs/blob/main/tests/test_mypy.yml. If this approach looks good to you, I'll take a stab at adding it to this PR. |
|
in commit 0126ea1, I added a test that uses pytest-mypy-plugins. Let me know if you like that format |
|
see https://typing.python.org/en/latest/reference/quality.html for some other approaches to testing |
|
Looks like I got a bunch of stuff Anyway, am spending what time I can on |
|
Looks like |
|
I have made a lot of changes to wrapt now including:
So I think were are in a good spot to finally start making some progress. No I still need to look at how this |
|
FWIW. Am not seeing that |
|
Ahh, it was running. Stupid browser search. |
|
|
||
| F = TypeVar("F", bound=Callable) | ||
|
|
||
| def FunctionWrapper(wrapped: F, wrapper: Callable) -> F: ... |
There was a problem hiding this comment.
Would this all be more precisely defined as:
from typing import Callable, TypeVar, Any, Tuple, Dict, Union
WrappedFunction = TypeVar("WrappedFunction", bound=Callable)
# Standard signature for wrappers
StandardWrapper = Callable[[WrappedFunction, Any, Tuple[Any, ...], Dict[str, Any]], Any]
# Catch-all wrapper signature
CatchAllWrapper = Callable[..., Any]
# Union type to accept either wrapper style
WrapperFunction = Union[StandardWrapper, CatchAllWrapper]
def FunctionWrapper(
wrapped: WrappedFunction, wrapper: WrapperFunction
) -> WrappedFunction: ...
The standard wrapper would be:
def standard_wrapper(wrapped, instance, *args, **kwargs):
pass
It would be rare, but someone may want to instead define this as:
def catch_all_wrapper(*args, **kwargs):
pass
Anyway, starting to understand what I need to do. Just working out easy way of maintaining the tests. It is a pain to have to add in the out section with exact line numbers.
|
The way that Next you just run: and then the This combining stuff into a single YAML file is an extra step which just makes it annoying. Don't understand why the overall |
|
Well that was easy to write my own simpler custom test handler. Well at least, easy for the AI Copilot. 🤣 |
|
At this point I have basically invalidated this PR and we can close it. Have a close look at I have pushed up the modified Although have added initial test code for After that, next step would be to work out properly what type hints are needed and so work out which to tackle next. Maybe we create a new issue at a time to propose what type hints should be for certain things, iterate over it in discussion as need be and then create a PR. But then maybe I will get enthusiastic over the weekend and just work out a whole. 😁 |
Create a test case (testcase.py):
With a pyproject.toml:
Run mypy on the test case with wrapt from the develop branch:
Now run mypy with wrapt from the type-hint-func-wrapper branch:
Relates to #258