综合影响范围和影响程度降序排列.
TypedDict 在描述较复杂的嵌套字典时表现乏力 | typehint主要表现为:
- 书写顺序混乱
- (pycharm) 对值的类型判断存在问题, 特别是处理后向引用时
- 结构可读性差
TODO:提供代码参考
open 函数的 encoding 默认值不是 ‘utf-8’ | builtins; open略.
Callable 对函数的私有属性无法识别 | typehintfrom typing import Callable
def xxx(func: Callable) -> None:
_ = func.__code__.co_argcount
# ~~~~~~~~~~ cannot find reference '__code__' in '(...) -> Any'!
相关阅读:
- https://stackoverflow.com/questions/37835179/how-can-i-specify-the-function-type-in-my-type-hints
import typing as t
def some_func(a: t.Iterable[str]):
for i in a: print(i)
print(len(a))
# ~~~~~~ expected type "Sized", got "Iterable[str]" instead!
import typing as t
def some_func(a: t.Sized):
for i in a: print(i)
# ~~~~~~~~~~ expected type "Iterable[str]", got "Sized" instead!
print(len(a))
已解决: 使用 typing.Sequence.
相关阅读: https://stackoverflow.com/questions/49427944/typehints-for-sized-iterable-in-python
windows 版 pip 在挂 vpn 的情况下使用 pip install 出现 ssl 认证失败 | pip; urllib3经观察, 是因为 ~/site-packages/pip/_vendor/urllib3 的版本引发的问题:
返回 tuple 时, 如果 tuple 只有一个元素, 写法上不明显, 可能看漏掉 | syntaxurllib3 v1.26+ 会出现此问题, 降级到 v1.25.9 可解决.
def foo() -> tuple[str, ...]:
return 'hello world',
# ^ 不够明显, 有时候会看漏.
对 *args 和 **kwargs 进行类型注解存在限制 | typehint
TODO:Description
self 无法在海象运算符中使用 | 海象运算符class AAA:
def xxx(self):
a = {
'aaa': (self.value := True),
# ~~~~ unresolved reference: 'self'!
}
重写 shelve 关闭方法, 导致内建函数 open 出现异常 | open
复现代码:
import shelve
class MyShelve(shelve.Shelve):
def close(self):
super().close()
with open('result.txt', 'w') as f:
# ~~~~ NameError: name 'open' is not defined!
f.write('program finished at {time}.'.format(time=...))
pycharm 相关的问题 pycharm 提示功能在显示 open 函数时严重影响体验
如下图, 键入 open() 后给出的提示完全遮住了编辑界面:
pycharm 对 typing.TypedDict 的实例化写法支持度非常差TODO:Description
使用使用到海象运算符和字典取值, 可能出现一个奇怪的弱警告a = {'a': 1, 'b': 2, 'c': 3}
if (x := a['c']) and x > 1:
# ^ 在这个位置偶尔会出现一个让人无法理解的弱警告: UnresolvedReference.
print(x)



