如何在 Python 中正确指定变量的类型以防止在 PyCharm 中出现未解析的引用?
How to correctly specify the type of a variable in Python in order to prevent unresolved reference in PyCharm?
我有一个功能:
def foo(path):
"""
:param path: Path to a folder
:type path: pathlib.Path
"""
new_path = path / 'tmp'
return new_path
它获取一个 pathlib.Path
对象并在此路径的末尾添加 'tmp'。但是 PyCharm 显示 new_path = path / 'tmp'
中 path
下的“未解决的引用”。
很明显,如果此类变量的类型是内置的,则不会发生这种情况。请注意,如果我导入 from pathlib import Path
并将 def foo(path)
更改为 def foo(path: Path)
,则可以解决此问题。但我想知道是否有任何方法可以在没有不必要的进口的情况下做到这一点。我阅读了有关 Python 打字的内容,但找不到解决方案。
在下面的示例中,PyCharm 确实发出警告 Unresolved reference 'Path'
,因为在注释中,类型提示 argument_path
的类型 Path
尚未从 [=17] 导入=].对参数和变量都发出警告。
def foo(argument_path: Path):
"""
:param path: Path to a folder
:type path: Path
"""
new_path: Path = argument_path / 'tmp'
return new_path
解决这个问题的明显方法是在模块顶部使用 from pathlib import Path
。
from pathlib import Path
def foo(argument_path: Path):
new_path: Path = argument_path / 'tmp'
But I want to know if there is any way to do this without unnecessary imports.
有两种方法可以在不导入的情况下执行此操作,或者在注释中使用完全限定名称 pathlib.Path
def foo(argument_path: pathlib.Path):
new_path: pathlib.Path = argument_path / 'tmp'
或者不使用类型注释,只需在文档字符串中指定类型。
PyCharm 的静态类型检查器不会在您声明一个无法在文档字符串中解析的类型时发出警告。相反,PyCharm 会在您尝试使用该功能时发出警告。在下面的示例中,签名或变量声明中没有使用类型提示,类型仅在文档字符串中指定。
def foo3(argument_path):
"""
:param argument_path: Path to a folder
:type argument_path: pathlib.Path
"""
new_path = path / 'tmp'
return new_path
# PyCharm will issue this warning:
foo3("a_string") # Expected type 'Path', got 'str' instead
最后,您在文档字符串中使用了 reStructuredText 语法,应在项目设置中指定此选项:Settings
>
Tools
>
Python Integrated Tools
>
DocString Format
我有一个功能:
def foo(path):
"""
:param path: Path to a folder
:type path: pathlib.Path
"""
new_path = path / 'tmp'
return new_path
它获取一个 pathlib.Path
对象并在此路径的末尾添加 'tmp'。但是 PyCharm 显示 new_path = path / 'tmp'
中 path
下的“未解决的引用”。
很明显,如果此类变量的类型是内置的,则不会发生这种情况。请注意,如果我导入 from pathlib import Path
并将 def foo(path)
更改为 def foo(path: Path)
,则可以解决此问题。但我想知道是否有任何方法可以在没有不必要的进口的情况下做到这一点。我阅读了有关 Python 打字的内容,但找不到解决方案。
在下面的示例中,PyCharm 确实发出警告 Unresolved reference 'Path'
,因为在注释中,类型提示 argument_path
的类型 Path
尚未从 [=17] 导入=].对参数和变量都发出警告。
def foo(argument_path: Path):
"""
:param path: Path to a folder
:type path: Path
"""
new_path: Path = argument_path / 'tmp'
return new_path
解决这个问题的明显方法是在模块顶部使用 from pathlib import Path
。
from pathlib import Path
def foo(argument_path: Path):
new_path: Path = argument_path / 'tmp'
But I want to know if there is any way to do this without unnecessary imports.
有两种方法可以在不导入的情况下执行此操作,或者在注释中使用完全限定名称 pathlib.Path
def foo(argument_path: pathlib.Path):
new_path: pathlib.Path = argument_path / 'tmp'
或者不使用类型注释,只需在文档字符串中指定类型。
PyCharm 的静态类型检查器不会在您声明一个无法在文档字符串中解析的类型时发出警告。相反,PyCharm 会在您尝试使用该功能时发出警告。在下面的示例中,签名或变量声明中没有使用类型提示,类型仅在文档字符串中指定。
def foo3(argument_path):
"""
:param argument_path: Path to a folder
:type argument_path: pathlib.Path
"""
new_path = path / 'tmp'
return new_path
# PyCharm will issue this warning:
foo3("a_string") # Expected type 'Path', got 'str' instead
最后,您在文档字符串中使用了 reStructuredText 语法,应在项目设置中指定此选项:Settings
>
Tools
>
Python Integrated Tools
>
DocString Format