Misunderstanding wrapping functions that use Self@Class
#7083
Replies: 2 comments
-
Your understanding of the error is correct. The pre-decorated method is not expecting to receive a I see a couple of straightforward ways to fix this. The first is to use GreeterMethod: TypeAlias = Callable[Concatenate[Any, P], T] If you want to protect against that coding error, you could define G = TypeVar("G", bound="Greeter")
GreeterMethod: TypeAlias = Callable[Concatenate[G, P], T]
def decorator(func: GreeterMethod[G, P, T]) -> GreeterMethod[G, P, T]:
... This will preserve the |
Beta Was this translation helpful? Give feedback.
-
Thank you so much! That's exactly what I was looking for! |
Beta Was this translation helpful? Give feedback.
-
I have a convenience wrapper in my codebase that does some introspection on
self
, but I'm getting an unexpected type error as ofpyright@1.1.338
(python@3.10
).Consider the following code.
Before 1.1.338, this passed our typechecks, but now I'm getting a type error.
It seems that this new error could be related to the following that I noticed in the 1.1.338 release notes.
I think I understand why this is an error - because there is no guarantee that the
self
parameter is an instance of aGreeter
, it could be a sub-class or something else that is not covariant.However, this code does do what I expect at runtime, so what I don't know or understand is the right way to type this decorator so that it knows how to access class
Greeter
's properties.Any help would be much appreciated! Thanks!
Beta Was this translation helpful? Give feedback.
All reactions