Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing overloads to CloudPath.open() #465

Closed
0xbe7a opened this issue Aug 23, 2024 · 1 comment · Fixed by #464
Closed

Add typing overloads to CloudPath.open() #465

0xbe7a opened this issue Aug 23, 2024 · 1 comment · Fixed by #464

Comments

@0xbe7a
Copy link
Contributor

0xbe7a commented Aug 23, 2024

Currently, the return type of CloudPath.open() is always IO[Any]. This differs from pathlib.Path.open(), which returns more specific types like TextIOWrapper, FileIO, BufferedRandom, BufferedWriter, BufferedReader, BinaryIO, or IO[Any] depending on arguments such as mode, buffering, and encoding. The more specific return types are useful for catching typing issues where a library expects a file handle in binary or text mode, specified as IO[bytes] or IO[str], respectively. With the current behavior, passing a file handle returned by CloudPath.open() often requires using typing.cast() or # type: ignore.

Here’s an example illustrating the current behavior compared to pathlib.Path.open():

from pathlib import Path
from cloudpathlib import CloudPath

cloud_path = CloudPath("s3://bucket/key")  # type: ignore
local_path = Path("test.txt")

with local_path.open("w") as f:
    reveal_type(f)  # Revealed type: io.TextIOWrapper

with local_path.open("wb") as f:
    reveal_type(f)  # Revealed type: io.BufferedWriter

with cloud_path.open("w") as f:
    reveal_type(f)  # Revealed type: typing.IO[Any]

with cloud_path.open("wb") as f:
    reveal_type(f)  # Revealed type: typing.IO[Any]
@pjbull
Copy link
Member

pjbull commented Aug 29, 2024

Thanks @0xbe7a, this is released now in v0.19.0 if you want to upgrade and test your use case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants