python - PyCharm 中文字类型检查错误的 Python 列表(与带有文字键的 dict 相同)
问题描述
我想在 Python 中使用 Literal 类型提示的优点,但似乎 Literal 没有被正确识别为 dict 键或列表:
from typing import Literal, Dict, List
T = Literal['foo', 'bar']
L = List[T]
D = Dict[T, int]
q: T = 'foo' # ok
l: L = ['foo', 'bar'] # Expected type 'List[Literal['foo', 'bar']]', got 'List[str]' instead
d: D = {'foo': 1} # Expected type 'Dict[Literal['foo', 'bar'], int]', got 'Dict[str, int]' instead
如何在列表中正确使用文字类型或作为字典键?
解决方案
推荐阅读
- node.js - 使用 sendgrid 和 nodeJS 发送电子邮件
- spring-boot - 迁移到 Camel 3 后发布 IBM MQ JMS 标头的问题
- python-3.x - Pytorch:重新缩放后RGB值范围为0-1,如何标准化图像?
- prolog - 如何使用谓词逻辑表示以下语句?
- bash - GIT_ASKPASS 不止回声
- mongodb - 如何使用 $lookup 获取 mongodb 中外部字段的计数?
- angular9 - Angular 9 ng-bootstrap 6.1 模态组件打开方法不起作用
- python - python中具有许多参数的函数
- bash - Bash 计数未注释的行并使用文件名写入输出
- firebase - 如何使用颤振将我在firestore中的所有文档返回到列表中