2017-01-30 9 views
0

objectを返品タイプboto3.resources.factory.ec2.Instanceに置き換えるにはどうすればよいですか?それはインテリセンスの目的のためです。AWS Boto3タイプの明示的返品の定義

# Note: self._resource -> boto3.resource(...) 

def instance(self, instance_id: str) -> object: 
    """ 
    Gets EC2 instance based on Instance ID. 
    :param instance_id: AWS EC2 Instance ID 
    """ 
    instance = self._resource.Instance(instance_id) 
    return instance 

type(ec2.instance('i-123456'))によれば、(この例では、ec2instance()メソッドを含むクラスのインスタンスである)、それは<class 'boto3.resources.factory.ec2.Instance'>を返します。ただし、boto3はファクトリパターンを使用してInstanceクラスのインスタンスを返します。これはInstanceクラスの定義の場所を隠しています。

答えて

1

You specify an rtype in your docstring when you want to specify the return type。ほとんどのIDEはそれを拾うのに十分なほどスマートになります。


    def instance(self, instance_id: str): 
     """ 
     Gets EC2 instance based on Instance ID. 
     :param instance_id: AWS EC2 Instance ID 
     :rtype: boto3.resources.factory.ec2.Instance 
     """ 
     instance = self._resource.Instance(instance_id) 
     return instance 

あなたはPythonの型のヒンティングを使用する場合は、いくつかの追加の安全性を確保したいので、あなたがTypeVarを使用することがあります:

from typing import TypeVar 
T = TypeVar('T', bound='boto3.resources.factory.ec2.Instance') 
関連する問題