python – 类似于DynamoDB.Table这样的boto3资源的类型注释

boto3库提供了几种返回资源的工厂方法.例如:

dynamo = (
    boto3
    .resource('dynamodb')
    .Table(os.environ['DYNAMODB_TABLE'])
)

我想注释那些资源,以便我可以获得更好的类型检查和完成,但我能找到的唯一类型是来自boto3.dynamodb.table import TableResource.

当我添加该注释时:

dynamo: TableResource = (
    boto3
    .resource('dynamodb')
    .Table(os.environ['DYNAMODB_TABLE'])
)

自动完成提供的唯一方法是batch_writer(self,overwrite_by_pkeys),即使是文档lists several others.

这是用作注释的错误类吗?检查终端中的变量类型我可以看到它是< class'boto3.resources.factory.dynamodb.Table'>,但似乎不可能静态获取该类型.

解决方法:

类型和API方法不是静态存在的. boto3使用数据驱动架构,这是一种极其动态的设计,它使用JSON格式的数据(here是一个示例)来确定可能的API调用.他们这样做是为了便于更新库以包含新的API更改.我不确定,但我认为他们可能会在其他语言中使用相同的SDK策略,因此可以在几乎没有重复工作的情况下对多个SDK进行更改.

Here’s a quote from their blog:

Libraries must adapt to changes in users’ needs and also to changes in the platforms on which they run. As AWS’s growth accelerated over the years, the speed at which our APIs are updated has also gotten faster. This required us to devise a scalable method to quickly deliver support for multiple API updates every week, and this is why AWS API support in Boto3 is almost completely data-driven. Boto3 has ‘client’ classes that are driven by JSON-formatted API models that describe AWS APIs, so most new service features only require a simple model update. This allows us to deliver support for API changes very quickly, in consistent and reliable manner.

您还可以通过在调试器中进入方法调用(例如resource.Table)来了解这种情况.

上一篇:Anna-senpai帖子翻译与Mirai源代码使用


下一篇:python – 与DynamoDB库Boto3的事务