You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 06:35:11