Type hints and tooltips for decorators/wrappers/proxy objects #4426
Replies: 1 comment 4 replies
-
Yes, this is a reliable workaround. Pyright captures the docstring from function/method captured by the ParamSpec and uses it in the resulting (wrapped) callable. Yes, this use case should be resolved by the The error about The type aliases you're defining are generic type aliases. For example, |
Beta Was this translation helpful? Give feedback.
-
I often use decorators/wrappers/proxy objects to extend classes from third party libraries (instead of inheritance). I find this is usually much easier to implement than mucking with
super()
and taking care to uphold the LSP.Say I've got the following class in a third party library that I want to extend:
and I write the following wrapper
The problem with this is that one looses both the type hints and the doc string this way. Unfortunately
functool.wraps
also does currently not work to restore these to us so that VS Code is able to display them.I saw there are multiple issues about this already, in particular #3939, microsoft/pylance-release#442 and python/typeshed#6670. But since I don't want to wait around for typeshed to fix this eventually, I've hacked away and ended up with this
that when used with the wrapper class
tricks VS Code into displaying the original type hints and doc string ✨ 🚀
I though this might be of some use for others but I've also got a couple of questions/issues:
TypeVar "W" appears only once in generic function signaturePylance[reportInvalidTypeVarUse]
?wraps_method
up by replacing the type definitions by more compact type aliases everything stopped working. Is this a bug or did I miss something there?Beta Was this translation helpful? Give feedback.
All reactions