mypy (typeshed) 拒绝 csv.excel 作为 csv.Sniffer.sniff 的 return 的可能值

mypy (typeshed) rejects csv.excel as a possible value for the return of csv.Sniffer.sniff

使用 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.excelsniff 函数的有效 return,所以它们肯定具有相同的类型。

Python 3.7.3, mypy 0.701

我认为这是 typeshed 中的错误:我提出了 issue

根据 typeshedSniffer.sniff returns 类型 csv.Dialect 的值,而实际上 returns 类型 Type[csv.Dialect] 的值