简体   繁体   English

Pycharm 类型提示警告打开路径 object

[英]Pycharm type hint warning on opening Path object

Using the following code:使用以下代码:

from pathlib import Path

file = Path("test.txt")

with open(file) as fl:
    pass 

Pycharm gives the following warning on file Pycharm 在file中给出以下警告

Unexpected type(s): (Path) 

Possible types: 
  (Union[str, bytes, int]) 
  (Union[str, bytes, int, PathLike]) less... (Ctrl+F1) 

Inspection info: This inspection detects type errors in function call expressions. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Types of function parameters can be specified in docstrings or in Python 3 function annotations.

Am I doing something wrong?难道我做错了什么?

You can use it like this你可以像这样使用它

    file = "test.txt"

    with open(file) as fl:
        pass 
  • Pycharm even gives you prompts to possible files that you might want to use. Pycharm 甚至会提示您可能要使用的文件。 I use it like this with no problems.我这样使用它没有问题。
  • If the file you are looking for isn't in the same directory as the code you need to add full path如果您要查找的文件与您需要添加完整路径的代码不在同一目录中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM