如何从botocore-stubs导入_RetryDict以通过mypy类型检查?
解决botocore Config重试配置的mypy类型检查问题
要让你的代码通过mypy类型检查,按以下步骤操作:
- 确保已安装
botocore-stubs:pip install botocore-stubs - 从正确路径导入
_RetryDict类型,它的定义跟随botocore原包结构,并非在botocore.stubs下 - 给数据类字段添加明确的类型标注
修改后的代码如下:
from dataclasses import dataclass from botocore.config import Config, _RetryDict @dataclass class COMMON_CONSTANTS: region: str = "eu-west-1" retry_config: _RetryDict = { 'max_attempts': 10, 'mode': 'adaptive', } conf = Config(retries=COMMON_CONSTANTS.retry_config)
说明
你之前尝试从botocore.stubs导入类型是错误的,botocore-stubs的类型定义与原botocore包的目录结构完全对齐,_RetryDict的类型声明在botocore.config的stubs文件中,因此直接从botocore.config导入即可,mypy会自动识别stubs中的类型定义。
内容的提问来源于stack exchange,提问作者Shuzheng
相关产品推荐
相关产品推荐

