python - boto3 资源(如 DynamoDB.Table)的类型注释

标签 python python-3.x amazon-web-services aws-sdk boto3

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

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

我想对这些资源进行注释,以便更好地进行类型检查和完成,但我能找到的唯一类似类型是 from 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)来了解这种情况。

关于python - boto3 资源(如 DynamoDB.Table)的类型注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49563445/

相关文章:

python - 如何修复 djangos 的 decotators 错误 : Wrapped class must subclass ModelAdmin

python - 我在 Python 中的可迭代对象是否也应该像大多数示例一样是迭代器

amazon-web-services - 有没有一种简单的方法来克隆胶水作业,但更改数据库连接?

amazon-web-services - id 为 [现有堆栈] 的堆栈不存在

python - 如果路由以 'post/' 开头,Django 与 CreateView 不匹配

python - 在文本文件中的两个单词之间查找/替换字符串

python-3.x - 接口(interface)继承声明

amazon-web-services - AWS EMR上的ClusterID与JobFlowID

python - 输入错误: No module named 'tkinter'

python - 是否可以在Python测试中模拟模块不可用的情况?