运行这个mypy要点( https://mypy-play.net/?mypy=latest&python=3.12&gist=58a8148f2c95c7a282a6f8a11ccd689a )
from collections import Counter C = Counter()
给出了mypy错误:
main.py:3: error: Need type annotation for "C" [var-annotated] Found 1 error in 1 file (checked 1 source file)
有没有办法让mypy推断出这种类型?
如果您初始化 Counter 带有一些数据,但如果不想在初始值设定项中传递任何内容,则必须给 C ,其中指定通用类型,例如。, C: Counter[str] .
Counter
C
C: Counter[str]
请参阅mypy文档中的此部分: Types of empty collections