【发布时间】:2019-05-14 14:20:31
【问题描述】:
使用 mypy 静态检查我的一些代码并遇到此问题。代码示例:
import csv
d: csv.Dialect = csv.excel
d = csv.Sniffer().sniff("a")
但是 mypy 在第一次分配给d 时给出了这个错误:
error: Incompatible types in assignment (expression has type "Type[excel]", variable has type "Dialect")
因此,解决此问题的自然方法是更改变量的类型。
from typing import Type
import csv
d: Type[csv.Dialect] = csv.excel
d = csv.Sniffer().sniff("a")
但现在我在第二次分配给 d 时收到此错误:
error: Incompatible types in assignment (expression has type "Dialect", variable has type "Type[Dialect]")
但这很奇怪,因为csv.excel 是sniff 函数的有效返回,所以它们肯定具有相同的类型。
Python 3.7.3,mypy 0.701
【问题讨论】:
标签: python-3.x csv mypy typeshed