Return 基于类型参数的类型
Return type based on type argument
我有这段代码,它可以自己检查类型:
import pandas as pd
from typing import Tuple, Type
def df2objects(df: pd.DataFrame, object_type: Type[BaseObject]) -> Tuple[BaseObject, ...]:
return tuple(object_type(**kwargs) for kwargs in df.to_dict(orient='records')
但如果我尝试使用它:
def use_it(df: pd.DataFrame) -> Tuple[DerivedObject,...]:
return df2objects(df, DerivedObject)
我明白了
Incompatible return value type (got "Tuple[BaseObject, ...]", expected "Tuple[DerivedObject, ...]")mypy(error)
我可以用
解决这个问题
import typing
def use_it(df: pd.DataFrame) -> Tuple[DerivedObject,...]:
return typing.cast(Tuple[DerivedObject,...], df2objects(df, DerivedObject))
但我真正想做的是指定 df2objects returns 一个 object_type 的元组,像这样:
def df2objects(df: pd.DataFrame, object_type: Type[BaseObject]) -> Tuple[object_type, ...]:
或者这个:
def df2objects(df: pd.DataFrame, object_type: Type[BaseObject]) -> Tuple[BaseObject, ...]:
return typing.cast(Tuple[object_type,...],tuple(object_type(**kwargs) for kwargs in df.to_dict(orient='records'))
当然,这些都不起作用。
正如评论中提到的那样,我相信使用 TypeVar
是这里的解决方案。假设 DerivedObject
是 BaseObject
的子类,以下应该有效:
import pandas as pd
from typing import Type, TypeVar, Tuple
T = TypeVar('T', bound=BaseObject)
def df2objects(df: pd.DataFrame, object_type: Type[T]) -> Tuple[T, ...]:
return tuple(object_type(**kwargs) for kwargs in df.to_dict(orient='records')
def use_it(df: pd.DataFrame) -> Tuple[DerivedObject,...]:
return df2objects(df, DerivedObject)
通过使用 bound
关键字参数,我们将 TypeVar
T
可能的类型限制为 DerivedObject
及其子类。
可以找到关于通用函数的 mypy 文档 here, the docs for TypeVar
can be found here, and information on bound TypeVar
s can be found here。
我有这段代码,它可以自己检查类型:
import pandas as pd
from typing import Tuple, Type
def df2objects(df: pd.DataFrame, object_type: Type[BaseObject]) -> Tuple[BaseObject, ...]:
return tuple(object_type(**kwargs) for kwargs in df.to_dict(orient='records')
但如果我尝试使用它:
def use_it(df: pd.DataFrame) -> Tuple[DerivedObject,...]:
return df2objects(df, DerivedObject)
我明白了
Incompatible return value type (got "Tuple[BaseObject, ...]", expected "Tuple[DerivedObject, ...]")mypy(error)
我可以用
解决这个问题import typing
def use_it(df: pd.DataFrame) -> Tuple[DerivedObject,...]:
return typing.cast(Tuple[DerivedObject,...], df2objects(df, DerivedObject))
但我真正想做的是指定 df2objects returns 一个 object_type 的元组,像这样:
def df2objects(df: pd.DataFrame, object_type: Type[BaseObject]) -> Tuple[object_type, ...]:
或者这个:
def df2objects(df: pd.DataFrame, object_type: Type[BaseObject]) -> Tuple[BaseObject, ...]:
return typing.cast(Tuple[object_type,...],tuple(object_type(**kwargs) for kwargs in df.to_dict(orient='records'))
当然,这些都不起作用。
正如评论中提到的那样,我相信使用 TypeVar
是这里的解决方案。假设 DerivedObject
是 BaseObject
的子类,以下应该有效:
import pandas as pd
from typing import Type, TypeVar, Tuple
T = TypeVar('T', bound=BaseObject)
def df2objects(df: pd.DataFrame, object_type: Type[T]) -> Tuple[T, ...]:
return tuple(object_type(**kwargs) for kwargs in df.to_dict(orient='records')
def use_it(df: pd.DataFrame) -> Tuple[DerivedObject,...]:
return df2objects(df, DerivedObject)
通过使用 bound
关键字参数,我们将 TypeVar
T
可能的类型限制为 DerivedObject
及其子类。
可以找到关于通用函数的 mypy 文档 here, the docs for TypeVar
can be found here, and information on bound TypeVar
s can be found here。