简体   繁体   English

Python 将返回类型注释为赋予函数的类型

[英]Python annotate return type as the type given to function

I am giving a type to function.我正在给一个类型的功能。 The function instantiates the type for me.该函数为我实例化类型。 The type implements BaseCustomObject but is one of many possible types.该类型实现BaseCustomObject但它是许多可能的类型之一。

def factory(object_to_create:[T], parameter:int=1) -> [T]:
    constructor_parameter = calculation(parameter)
    return object_to_create(constructor_parameter)

my_custom_object_instance = factory(MyCustomObject)

How do I annotate factory so that it is clear that it gives back whatever type I send it, and not just BaseCustomObject ?我如何注释factory以便很明显它返回我发送的任何类型,而不仅仅是BaseCustomObject I prefer not to use a Union type but to have the annotation/inspection/ide understand exactly which specific type I am getting back.我不喜欢使用联合类型,而是让注释/检查/ide 准确了解我要返回的特定类型。

use a TypeVar generic type.使用 TypeVar 泛型类型。 [T] means a list of T, not a T, so dont do that. [T] 表示 T 的列表,而不是 T,所以不要这样做。

If object_to_create is a function, do this.如果 object_to_create 是一个函数,请执行此操作。

# Use bound to limit the type to a specific ancestor.
T = TypeVar("T", bound="BaseCustomObject") 

# Callable[..., T] is the type of a function 
# that takes in whatever argument and returns an instance of T.

def my_func(
  object_creator: Callable[..., T], 
  params: Optional[int] = 1
) -> T:
  return object_creator(params) # Will return a T.

If object_to_create is a class and not a function you instead use Type[T] which will be the actual class and not a class instance.如果 object_to_create 是一个class而不是一个函数,您可以使用Type[T] ,它是实际的类而不是类实例。

# Use bound to limit the type to a specific ancestor.
T = TypeVar("T", bound="BaseCustomObject") 

def my_func(
  object_to_create: Type[T], 
  params: Optional[int] = 1
) -> T:
  return object_to_create(params) # Will return an instance of object_to_create.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用另一个函数的返回类型来注释Python函数? - How to annotate Python function using return type of another function? Python: type annotate class 装饰器返回类型 - Python: type annotate class decorator return type 如何将python函数的返回类型注释为与参数之一完全相同? - How to annotate the return type of python function to be exactly the same as one of the arguments? 如何根据输入“shape”键入注释 function 并返回“shape”? - How to type annotate a function with return "shape" based on input "shape"? Python 类型提示:函数返回类型,作为参数给出,但类型是通用别名,如 Union - Python type hinting: function return type, given as argument, but type is Generic Alias like Union 如何在 Python 中键入注释 function 指针(函数变量)? - How to type annotate a function pointer (function variable) in Python? Python 将类型注释为正则表达式模式 - Python annotate type as regex pattern 如何在Python 3.7中注解一个async function的装饰器类型? - How to annotate the type of a decorator of an async function in Python 3.7? 如何在 Python 中注释 `apply` 的类型? - How to annotate the type of `apply` in Python? Python:这个函数的返回类型是什么? - Python: what is the return type of this function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM