Issue
What is a best way in Python to hint a filename, so that it's acceptable to pass anything into a function that you can open as a file?
Especially both strings and files found via Pathlib.
def myfunc(filename: str) -> None:
with open(filename) as f1:
# do something here
Solution
I think what you are looking for is Structural Typing, which is not yet supported. It is proposed in PEP 544.
In the mean time, you could do a half-way effort by annotating with Union[str, bytes, os.PathLike]
.
Answered By - lxop
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.