mypy 无法正确推断生成器理解的类型
mypy cannot infer type of generator comprehension correctly
我正在使用 data-science-types 提供的存根文件让 mypy 能够检查我的 pandas 相关代码。可悲的是我得到以下行为:
为了
import pandas as pd
def test() -> pd.DataFrame:
pass
pd.concat((test() for _ in range(10)))
mypy 报告
test.py:6: error: Argument 1 to "concat" has incompatible type "Generator[DataFrame, None, None]"; expected "Union[Sequence[DataFrame], Mapping[str, DataFrame]]".
如果我使用 pd.concat([test() for _ in range(10)])
而不是 mypy 再次快乐。有人可以向我解释一下那里发生了什么吗?
以防万一它是相关的。我正在使用 python3.8.5、pandas 1.1.2、mypy 0.782 和数据科学类型 0.2.18.
您将生成器与序列混淆了。一个序列是,by definition,
An iterable which supports efficient element access using integer indices via the __getitem__()
special method and defines a __len__()
method that returns the length of the sequence.
生成器两者都不支持,它也不是一种映射,因此您不能将其传递给 pd.concat
。
首先,由于我代码中的生成器表达式具有 None 的 SendType 和 ReturnType,因此 Generator[DataFrame, None, None]
是正确的类型,请参阅 docs. So as chepner has pointed out the problem lies in the expected type. Even though pandas.concat
excepts generators data-science-types does not have it as possible input type for it. I would have considered that a bug but on their github page data-science-types他们写
Philosophy: [...] Often the actual APIs in the libraries is more permissive than the type signatures in our stubs; but this is (usually) a feature and not a bug.
我认为问题已解决。
我正在使用 data-science-types 提供的存根文件让 mypy 能够检查我的 pandas 相关代码。可悲的是我得到以下行为:
为了
import pandas as pd
def test() -> pd.DataFrame:
pass
pd.concat((test() for _ in range(10)))
mypy 报告
test.py:6: error: Argument 1 to "concat" has incompatible type "Generator[DataFrame, None, None]"; expected "Union[Sequence[DataFrame], Mapping[str, DataFrame]]".
如果我使用 pd.concat([test() for _ in range(10)])
而不是 mypy 再次快乐。有人可以向我解释一下那里发生了什么吗?
以防万一它是相关的。我正在使用 python3.8.5、pandas 1.1.2、mypy 0.782 和数据科学类型 0.2.18.
您将生成器与序列混淆了。一个序列是,by definition,
An iterable which supports efficient element access using integer indices via the
__getitem__()
special method and defines a__len__()
method that returns the length of the sequence.
生成器两者都不支持,它也不是一种映射,因此您不能将其传递给 pd.concat
。
首先,由于我代码中的生成器表达式具有 None 的 SendType 和 ReturnType,因此 Generator[DataFrame, None, None]
是正确的类型,请参阅 docs. So as chepner has pointed out the problem lies in the expected type. Even though pandas.concat
excepts generators data-science-types does not have it as possible input type for it. I would have considered that a bug but on their github page data-science-types他们写
Philosophy: [...] Often the actual APIs in the libraries is more permissive than the type signatures in our stubs; but this is (usually) a feature and not a bug.
我认为问题已解决。