Skip to content

Commit dc8ff96

Browse files
authored
feat: Add overloads for CloudPath.open() (#464)
* feat: Add overloads for CloudPath.open() * add comment * add HISTORY entry * dont vendor typeshed * format
1 parent 5656879 commit dc8ff96

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Changed `LocalClient` so that client instances using the default storage access the default local storage directory through the `get_default_storage_dir` rather than having an explicit reference to the path set at instantiation. This means that calling `get_default_storage_dir` will reset the local storage for all clients using the default local storage, whether the client has already been instantiated or is instantiated after resetting. This fixes unintuitive behavior where `reset_local_storage` did not reset local storage when using the default client. (Issue [#414](https://github.com/drivendataorg/cloudpathlib/issues/414))
1616
- Added a new `local_storage_dir` property to `LocalClient`. This will return the current local storage directory used by that client instance.
1717
by reference through the `get_default_ rather than with an explicit.
18+
- Refined the return type annotations for `CloudPath.open()` to match the behavior of `pathlib.Path.open()`. The method now returns specific types (`TextIOWrapper`, `FileIO`, `BufferedRandom`, `BufferedWriter`, `BufferedReader`, `BinaryIO`, `IO[Any]`) based on the provided `mode`, `buffering`, and `encoding` arguments. ([Issue #465](https://github.com/drivendataorg/cloudpathlib/issues/465), [PR #464](https://github.com/drivendataorg/cloudpathlib/pull/464))
1819
- Added Azure Data Lake Storage Gen2 support (Issue [#161](https://github.com/drivendataorg/cloudpathlib/issues/161), PR [#450](https://github.com/drivendataorg/cloudpathlib/pull/450)), thanks to [@M0dEx](https://github.com/M0dEx) for PR [#447](https://github.com/drivendataorg/cloudpathlib/pull/447) and PR [#449](https://github.com/drivendataorg/cloudpathlib/pull/449)
1920

2021
## v0.18.1 (2024-02-26)

cloudpathlib/cloudpath.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import defaultdict
33
import collections.abc
44
from contextlib import contextmanager
5+
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
56
import os
67
from pathlib import ( # type: ignore
78
Path,
@@ -14,6 +15,8 @@
1415
import shutil
1516
import sys
1617
from typing import (
18+
BinaryIO,
19+
Literal,
1720
overload,
1821
Any,
1922
Callable,
@@ -34,10 +37,20 @@
3437
from urllib.parse import urlparse
3538
from warnings import warn
3639

40+
if TYPE_CHECKING:
41+
from _typeshed import (
42+
OpenBinaryMode,
43+
OpenBinaryModeReading,
44+
OpenBinaryModeUpdating,
45+
OpenBinaryModeWriting,
46+
OpenTextMode,
47+
)
48+
3749
if sys.version_info >= (3, 10):
3850
from typing import TypeGuard
3951
else:
4052
from typing_extensions import TypeGuard
53+
4154
if sys.version_info >= (3, 11):
4255
from typing import Self
4356
else:
@@ -543,6 +556,90 @@ def walk(
543556
else:
544557
raise
545558

559+
@overload
560+
def open(
561+
self,
562+
mode: "OpenTextMode" = "r",
563+
buffering: int = -1,
564+
encoding: Optional[str] = None,
565+
errors: Optional[str] = None,
566+
newline: Optional[str] = None,
567+
force_overwrite_from_cloud: Optional[bool] = None,
568+
force_overwrite_to_cloud: Optional[bool] = None,
569+
) -> "TextIOWrapper": ...
570+
571+
@overload
572+
def open(
573+
self,
574+
mode: "OpenBinaryMode",
575+
buffering: Literal[0],
576+
encoding: None = None,
577+
errors: None = None,
578+
newline: None = None,
579+
force_overwrite_from_cloud: Optional[bool] = None,
580+
force_overwrite_to_cloud: Optional[bool] = None,
581+
) -> "FileIO": ...
582+
583+
@overload
584+
def open(
585+
self,
586+
mode: "OpenBinaryModeUpdating",
587+
buffering: Literal[-1, 1] = -1,
588+
encoding: None = None,
589+
errors: None = None,
590+
newline: None = None,
591+
force_overwrite_from_cloud: Optional[bool] = None,
592+
force_overwrite_to_cloud: Optional[bool] = None,
593+
) -> "BufferedRandom": ...
594+
595+
@overload
596+
def open(
597+
self,
598+
mode: "OpenBinaryModeWriting",
599+
buffering: Literal[-1, 1] = -1,
600+
encoding: None = None,
601+
errors: None = None,
602+
newline: None = None,
603+
force_overwrite_from_cloud: Optional[bool] = None,
604+
force_overwrite_to_cloud: Optional[bool] = None,
605+
) -> "BufferedWriter": ...
606+
607+
@overload
608+
def open(
609+
self,
610+
mode: "OpenBinaryModeReading",
611+
buffering: Literal[-1, 1] = -1,
612+
encoding: None = None,
613+
errors: None = None,
614+
newline: None = None,
615+
force_overwrite_from_cloud: Optional[bool] = None,
616+
force_overwrite_to_cloud: Optional[bool] = None,
617+
) -> "BufferedReader": ...
618+
619+
@overload
620+
def open(
621+
self,
622+
mode: "OpenBinaryMode",
623+
buffering: int = -1,
624+
encoding: None = None,
625+
errors: None = None,
626+
newline: None = None,
627+
force_overwrite_from_cloud: Optional[bool] = None,
628+
force_overwrite_to_cloud: Optional[bool] = None,
629+
) -> "BinaryIO": ...
630+
631+
@overload
632+
def open(
633+
self,
634+
mode: str,
635+
buffering: int = -1,
636+
encoding: Optional[str] = None,
637+
errors: Optional[str] = None,
638+
newline: Optional[str] = None,
639+
force_overwrite_from_cloud: Optional[bool] = None,
640+
force_overwrite_to_cloud: Optional[bool] = None,
641+
) -> "IO[Any]": ...
642+
546643
def open(
547644
self,
548645
mode: str = "r",
@@ -552,7 +649,7 @@ def open(
552649
newline: Optional[str] = None,
553650
force_overwrite_from_cloud: Optional[bool] = None, # extra kwarg not in pathlib
554651
force_overwrite_to_cloud: Optional[bool] = None, # extra kwarg not in pathlib
555-
) -> IO[Any]:
652+
) -> "IO[Any]":
556653
# if trying to call open on a directory that exists
557654
if self.exists() and not self.is_file():
558655
raise CloudPathIsADirectoryError(

0 commit comments

Comments
 (0)