Boto3资源(如DynamoDB.Table)的类型注释 [英] Type annotation for boto3 resources like DynamoDB.Table

查看:100
本文介绍了Boto3资源(如DynamoDB.Table)的类型注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The boto3 library provides several factory methods that returns resources. For example:

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

我想注释这些资源,以便可以更好地进行类型检查和完成,但是唯一可以找到的类型是from boto3.dynamodb.table import TableResource.

I want to annotate those resources so I can get better type checking and completion, but the only type alike I could find was from boto3.dynamodb.table import TableResource.

当我添加该注释时:

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

即使文档列出其他几个人.

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

Is this the wrong class to use as annotation? Inspecting that variable type in the terminal I could see that it was <class 'boto3.resources.factory.dynamodb.Table'>, but it doesn't seem to be possible to get that type statically.

推荐答案

类型和API方法不是静态存在的. boto3使用数据驱动的体系结构,这是一种非常动态的设计,使用JSON格式的数据(

The types and API methods don't exist statically. boto3 uses data driven architecture, an extremely dynamic design that uses data in JSON format (here is an example) to determine what API calls are possible. They do this to make it easy to update the library to include new API changes. I'm not sure but I think they might use the same strategy for SDKs in other languages, so changes to multiple SDKs can be made with little duplicated work.

这是他们的引文博客:

图书馆必须适应用户需求的变化,还必须适应用户运行平台的变化.随着AWS多年来的增长加速,我们API的更新速度也越来越快.这要求我们设计一种可扩展的方法,以每周快速提供对多个API更新的支持,这就是为什么Boto3中的AWS API支持几乎完全由数据驱动的原因. Boto3具有由描述AWS API的JSON格式的API模型驱动的客户端"类,因此大多数新服务功能仅需要简单的模型更新.这使我们能够以一致和可靠的方式非常迅速地提供对API更改的支持.

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之类的方法调用来了解这种情况.

You can also get a glimpse of this happening by stepping into a method call such as resource.Table in a debugger.

这篇关于Boto3资源(如DynamoDB.Table)的类型注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆