PyCharm 类型检查器预期类型为“无",在使用 Pandas dataframe.to_csv 时改为“str" [英] PyCharm type checker expected type 'None', got 'str' instead when using pandas dataframe.to_csv
问题描述
使用 PyCharm 2021.2(社区版)和 Pandas 1.3.1,代码如下:
Using PyCharm 2021.2 (Community Edition) and Pandas 1.3.1, with the following code:
import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
df.to_csv("testfile.csv")
PyCharm 将产生警告,突出显示testfile.csv",并显示Expected type 'None', got 'str' instead"将鼠标悬停在警告上方时.
PyCharm will produce a Warning, highlight "testfile.csv", and display "Expected type 'None', got 'str' instead" when hovering over the Warning.
testfile.csv 已创建,似乎没有发生任何问题.
testfile.csv is created and no issues appear to be happening.
有没有办法解决这个问题?有谁知道为什么会出现这个警告?
Is there a way to fix this? Does anyone know why this Warning appears?
谢谢
推荐答案
这似乎是 Pycharm 中的一个错误,导致它无法正确解释 |
;typing.Union
的新运算符.
This appears to be a bug in Pycharm that prevents it from properly interpreting |
; the new operator for typing.Union
.
如果我 ctrl+p 而在 to_csv
的参数列表中,我得到这个
If I ctrl+p while inside of the argument list of to_csv
, I get this
注意,它认为参数是 None
类型(由 : None
表示).
Note that it thinks the argument is of type None
(indicated by : None
).
问题是,这是函数的实际签名:
The problem is, this is the actual signature of the function:
def to_csv(
self,
path_or_buf: FilePathOrBuffer[AnyStr] | None = None,
. . .
参数的类型实际上是FilePathOrBuffer[AnyStr] |无
,在 Python 3.10 之前将是 typing.Union[FilePathOrBuffer[AnyStr], None]
或简单的 typing.Optional[FilePathOrBuffer[AnyStr]]
.
The type of the parameter is actually FilePathOrBuffer[AnyStr] | None
, which prior to Python 3.10 would be typing.Union[FilePathOrBuffer[AnyStr], None]
or simply typing.Optional[FilePathOrBuffer[AnyStr]]
.
Pycharm 显然还不能读取花哨的类型提示,尽管它说它支持 Python 3.10.
Pycharm apparently can't yet read fancy type hints, despite saying it supports Python 3.10.
这篇关于PyCharm 类型检查器预期类型为“无",在使用 Pandas dataframe.to_csv 时改为“str"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!