-
-
Notifications
You must be signed in to change notification settings - Fork 244
Type hint improvements #306
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
base: develop
Are you sure you want to change the base?
Conversation
The `P` and `T` ParamSpec and TypeVar attributes are bound and indicate the parameters of the *wrapper* function, not the *wrapped* function. Signed-off-by: Stephen Finucane <stephen@that.guru>
…er.__get__ Properly handle method binding by providing overrides that will strip the self/cls parameter when used on a method rather than a parameter. Tests are updated to reflect this improvement. Signed-off-by: Stephen Finucane <stephen@that.guru>
|
What is The type hints are the way they are because a more simplified approach doesn't work with various type checkers and results in various cases being flagged as problems when they aren't. Would also have been helpful to see how you are using type hints in your usage of |
|
I'm away without laptop this week so will have to follow up with a longer reply next week, but debtcollector is https://github.com/openstack/debtcollector and we're targeting mypy. I'm typing that as part of a larger effort to type various openstack libraries. If you look at |
|
Also, the fact that mypy at least is now able to correctly identify incorrect parameters and returns types rather than returning |
|
I can't find the TODO you reference. |
|
Apologies, it hasn't merged yet. https://review.opendev.org/c/openstack/debtcollector/+/972447 |
|
FWIW. For reference, prior type hint work on wrapt was based on testing against mypy 1.18.1. I see there is a 1.19 now. I don't know if that newer version addresses issues I see. From memory if plain Callable was used instead of complex overload I ended up using, mypy wouldn't work properly when wrapt decorators were applied to methods of a class, it just wouldn't handle self/cls argument properly and always complained was wrong number of arguments. |
|
I could also have that round the wrong way, and it is pyright that didn't work without the mess of overloads. So making if just Callable could well work for mypy on either version, but then pylance in VS Code will not work properly. So unfortunately compromises are needed since these static type checkers tools are not fool proof and still a work in progress. |
|
So some of the suggested changes for Am not sure that: is correct though in returning I also need to understand why you use: and why existing: were not appropriate. Still have big concerns over simplification of: As seems to happen quite often to me, not having using VS Code for a couple of months for this, its type checking features are totally broken for pylance and don't know why, so hard to test things right now with it. |
|
As usual, start tweaking stuff and other stuff just starts breaking. This is going to take a while to sort out as usual. 😩 |
|
I may have found a solution. Perhaps can use:
Can't have one of the argument alternatives (need to investigate implications of that on other things). The |
|
FWIW, the changes also seem to make some things work (different to |
|
But all of Only solution I can see right now is to extract a minimal self contained example of the problem and report issues against tools that cannot handle it. |
|
Again, I'm away at the moment so won't reply in detail yet but on this point:
I called this out in one of the commit messages, but it's because the non-underscored variants are bound to the wrapper function arguments, so the underscored variants are needed for the wrapped callable's arguments since those are different. Without this, you can't do something like the below since the paramspec of the decorator and decorated function differs (i.e. the expected case). This change alone gives a marked improvement in results. |
|
Not sure what you are trying to say there without a full example. As far as I can work out, the separate That said, even when incorporate it in certain cases in a way that works for those three tools and still works for So I don't really see it as directly related to difference between I would need to see a more complete example of the corresponding wrapper part of the decorator for what you are talking about. As is, the docs steer one away from using explicit types on the wrapper and instead say to use: Using Anyway, am incrementally working through things and redoing how I test things so can check for all four static type checker tools and come up with a table of what things don't work for which tools. I will be doing a bit more on it over the weekend and if happy with progress may make a 2.1.0dev2 release some with changes which progress things a bit. |
|
I have released 2.1.0dev2 to PyPi which incorporates your ideas but changes things partly back to how they were to ensure that Current issues I still know of:
FWIW, the repo have been using to test new type hints behaviour with wrapt without needing to modify wrapt itself is: |

These were spotted while working on adding type hints to debtcollector. I believe the simplified types are correct, as evidenced by me no longer needing
type: ignorestatements in debtcollector plus the reduced failures in the tests, but this should definitely receive the twice-over.