Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 590
sanders Avatar asked Mar 18 '26 16:03

sanders


1 Answers

You can read file with Path function, like this:

file = Path("test.txt")    
with file.open(mode="r", encoding="utf-8") as fl:
    content = fl.read()
like image 155
Ben Avatar answered Mar 21 '26 04:03

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!