admin管理员组文章数量:1332881
I am trying to implement a decorator looks like
def decorator(cls):
class _Wrapper(cls):
def func(self):
super().func()
print("patched!")
return _Wrapper
and I am wondering how do I hint the cls
and the return value type. I tried to use _Class=TypeVar("_Class")
and cls: Type[_Class]
but mypy complained that value cls is not valid as a type. Also I do not know how to hint the return type.
I am trying to implement a decorator looks like
def decorator(cls):
class _Wrapper(cls):
def func(self):
super().func()
print("patched!")
return _Wrapper
and I am wondering how do I hint the cls
and the return value type. I tried to use _Class=TypeVar("_Class")
and cls: Type[_Class]
but mypy complained that value cls is not valid as a type. Also I do not know how to hint the return type.
2 Answers
Reset to default 1You should be able to use a Protocol
:
from __future__ import annotations
from typing import Protocol
class SupportsFunc(Protocol):
def func(self) -> None: ...
def decorator(cls: type[SupportsFunc]) -> type[SupportsFunc]:
class _Wrapper(cls):
def func(self) -> None:
super().func()
print("patched!")
return _Wrapper
@decorator
class Test:
def func(self) -> None:
print("original")
instance = Test()
instance.func()
However, MyPy will generate an error for the line class _Wrapper(cls):
:
main.py:9: error: Variable "cls" is not valid as a type [valid-type]
main.py:9: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
main.py:9: error: Invalid base class "cls" [misc]
Found 2 errors in 1 file (checked 1 source file)
This appears to be an open bug on MyPy and you may need to comment out those errors until it is fixed.
def decorator(cls: type[SupportsFunc]) -> type[SupportsFunc]:
class _Wrapper(cls): # type: ignore[valid-type, misc] # See https://github/python/mypy/issues/14458
def func(self) -> None:
super().func()
print("patched!")
return _Wrapper
The type of class is "type":
class new:
pass
print(type(new))
output:
<class 'type'>
you can set:
def decorator(cls: type) -> type:
class _Wrapper(cls):
def func(self):
super().func()
print("patched!")
return _Wrapper
Or you can define a custom type name, for example:
type _claas = type
def decorator(cls: _claas) -> _claas:
class _Wrapper(cls):
def func(self):
super().func()
print("patched!")
return _Wrapper
本文标签:
版权声明:本文标题:Function with Python type hint which accepts a class A and outputs a class that inherits from A - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742310831a2450834.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
type
argument and return atype
value may silence MyPy, its also not entirely correct as you want the decorator to take an argument that is "any class with thefunc
method" and return the same (otherwise you could wrap the decorator around a class without thefunc
method and MyPy would not complain). – MT0 Commented Nov 21, 2024 at 11:28Protocol
and use the correct type hints (combining bothtype
andProtocol
) it still won't (in the specific situation the OP is using) silence all the errors from the type checker. – MT0 Commented Nov 21, 2024 at 12:23func
method, wouldn't it be simpler to decorate that method instead of defining an unnecessary subclass? – chepner Commented Nov 24, 2024 at 15:16