Replies: 2 comments 7 replies
|
Class decorators are type checked now, but they can't change the type, which should be fine for your use-case. It looks like your example code already works just fine with the behavior you expect: https://mypy-play.net/?mypy=latest&python=3.12&gist=9843acb42af5acef8f7a982d2c7207ac |
5 replies
|
You could cast the decorator to from _typeshed import IdentityFunction
...
class Registry(dict[_KT, _VT]):
def register(self, name: _KT) -> IdentityFunction:
def decorator(func: _VT) -> _VT:
self[name] = func
return func
return cast(IdentityFunction, decorator) |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Came across a pattern today that left me wondering how it should be typed instead or if it's even possible to type probably.
It works for Mypy but I'm guessing this is just because class decorators aren't type checked at all python/mypy#3135.
All reactions