Python 3.5 类型提示工厂类型

Python 3.5 Type Hinting Factory Type

如果我使用 boto3 创建到 aws-ec2 的连接,它有 return 类型 boto3.resources.factory.ec2.ServiceResource

import boto3
cnxn = boto3.Session().resource('ec2')
type(cnxn)
>>  boto3.resources.factory.ec2.ServiceResource

我希望能够将其用作类型提示的类型。但是,如果我尝试引用它,则会出现错误。

boto3.resources.factory.ec2.ServiceResource
AttributeError: module 'boto3.resources.factory' has no attribute 'ec2'

有什么方法可以使用这些类型作为提示吗?

编辑:导入也不起作用

import boto3.resources.factory.ec2
>> ImportError: No module named 'boto3.resources.factory.ec2'; 'boto3.resources.factory' is not a package

请参考python type documentation

你得到的是一个类型对象。如果您打算从类型中创建一个新对象,则使用 class type(name, bases, dict) 创建一个新对象。如示例所示:

>>> class X(object):
...     a = 1
...
>>> X = type('X', (object,), dict(a=1))

您将需要使用 forward references,因为这些类型在运行时才存在。