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 |
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
-
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.
Beta Was this translation helpful? Give feedback.
All reactions