class __init__ 中的 TypeVar 类型提示
TypeVar in class __init__ type hinting
我正在尝试使用 TypeVar 将 init 参数指示为特定类型。
但是我做错了,或者根本不可能。
from typing import TypeVar
T=TypeVar("T")
class TestClass:
def __init__(self,value:T):
self._value=value
a = TestClass(value=10)
b = TestClass(value="abc")
reveal_type(a._value)
reveal_type(b._value)
我希望 a._value
的显示类型是 int
,b._value
是 string
。
但它们都显示为 'T`-1'
感谢任何帮助或见解!
[编辑]
稍微扩展一点的例子。
BaseClass 将被覆盖,实际类型提示由覆盖 class.
提供
from typing import TypeVar
T=TypeVar("T")
class BaseClass:
def __init__(self,value):
self._value = value
class Class1(BaseClass):
def __init__(self,value:str):
super().__init__(value)
class Class2(BaseClass):
def __init__(self,value:int):
super().__init__(value)
a = Class1("A value")
b = Class2(10)
reveal_type(a._value)
reveal_type(b._value)
默认情况下,使用 TypeVar 会将其范围限制在它用作注释的 method/function 范围内。为了将 TypeVar 的范围限定为实例和所有 methods/attributes,将 class 声明为 Generic
.
from typing import TypeVar, Generic
T=TypeVar("T")
class BaseClass(Generic[T]): # Scope of `T` is the class:
def __init__(self, value: T): # Providing some `T` on `__init__`
self._value = value # defines the class' `T`
这允许将子class声明为通用的或具体的。
class Class1(BaseClass[str]): # "is a" BaseClass where `T = str`
pass # No need to repeat ``__init__``
class ClassT(BaseClass[T]): # "is a" BaseClass where `T = T'`
@property
def value(self) -> T:
return self._value
reveal_type(Class1("Hello World")._value) # Revealed type is 'builtins.str*'
reveal_type(Class1(b"Uh Oh!")._value) # error: Argument 1 to "Class1" has incompatible type "bytes"; expected "str"
reveal_type(ClassT(42).value) # Revealed type is 'builtins.int*'
我正在尝试使用 TypeVar 将 init 参数指示为特定类型。 但是我做错了,或者根本不可能。
from typing import TypeVar
T=TypeVar("T")
class TestClass:
def __init__(self,value:T):
self._value=value
a = TestClass(value=10)
b = TestClass(value="abc")
reveal_type(a._value)
reveal_type(b._value)
我希望 a._value
的显示类型是 int
,b._value
是 string
。
但它们都显示为 'T`-1'
感谢任何帮助或见解!
[编辑]
稍微扩展一点的例子。 BaseClass 将被覆盖,实际类型提示由覆盖 class.
提供from typing import TypeVar
T=TypeVar("T")
class BaseClass:
def __init__(self,value):
self._value = value
class Class1(BaseClass):
def __init__(self,value:str):
super().__init__(value)
class Class2(BaseClass):
def __init__(self,value:int):
super().__init__(value)
a = Class1("A value")
b = Class2(10)
reveal_type(a._value)
reveal_type(b._value)
默认情况下,使用 TypeVar 会将其范围限制在它用作注释的 method/function 范围内。为了将 TypeVar 的范围限定为实例和所有 methods/attributes,将 class 声明为 Generic
.
from typing import TypeVar, Generic
T=TypeVar("T")
class BaseClass(Generic[T]): # Scope of `T` is the class:
def __init__(self, value: T): # Providing some `T` on `__init__`
self._value = value # defines the class' `T`
这允许将子class声明为通用的或具体的。
class Class1(BaseClass[str]): # "is a" BaseClass where `T = str`
pass # No need to repeat ``__init__``
class ClassT(BaseClass[T]): # "is a" BaseClass where `T = T'`
@property
def value(self) -> T:
return self._value
reveal_type(Class1("Hello World")._value) # Revealed type is 'builtins.str*'
reveal_type(Class1(b"Uh Oh!")._value) # error: Argument 1 to "Class1" has incompatible type "bytes"; expected "str"
reveal_type(ClassT(42).value) # Revealed type is 'builtins.int*'