类类型的类型提示作为参数

问题描述

在ros2的python客户端库中,有一个函数接受类类型作为参数(不是该类的实例,而是定义)。

例如

x <- tibble(
  A = parse_datetime("2020-08-18 19:00"),B = if_else(TRUE,A,as.POSIXct(NA_character_,tz = "UTC")),C = if_else(FALSE,tz = "UTC"),A)
)

# # A tibble: 1 x 3
#   A                   B                   C                  
#   <dttm>              <dttm>              <dttm>             
# 1 2020-08-18 19:00:00 2020-08-18 19:00:00 2020-08-18 19:00:00

如何使用python类型注释来注释这样的东西?

一种方法是使用R > lubridate::tz(x$A) [1] "UTC" R > lubridate::tz(x$B) [1] "UTC" R > lubridate::tz(x$C) [1] "UTC" 类型提示,因为可以用from std_msgs.msg import String #String is python class i.e,#class String: # ... class MyNode(Node) def __init__(self): super().__init__('my_node') self.add_subscription(String,'chatter',lambda msg : print(msg)) #Note that we pass in the type String,not an object,i.e String() 调用类类型以构造对象。但这并不能很好地传达其真实含义。

Executable

最好是有这样的东西:

()

有没有类似的东西?这样做的标准方法是什么?

理想情况下,我希望它可以与MyPy静态类型检查系统一起使用。

解决方法

实际上有一个您想要的类型提示,它只是叫做Type而不是Class,这里是它的文档:https://docs.python.org/3/library/typing.html#typing.Type

如您所见,您可以告诉它应该是什么类,因此在您给出的示例中将是:

Type[String]

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...