23
23
IO ,
24
24
Any ,
25
25
AnyStr ,
26
+ Generic ,
27
+ Literal ,
26
28
Mapping ,
27
29
cast ,
30
+ overload ,
28
31
)
29
32
from urllib .parse import (
30
33
urljoin ,
@@ -78,7 +81,7 @@ class IOArgs:
78
81
79
82
80
83
@dataclasses .dataclass
81
- class IOHandles :
84
+ class IOHandles ( Generic [ AnyStr ]) :
82
85
"""
83
86
Return value of io/common.py:get_handle
84
87
@@ -92,7 +95,7 @@ class IOHandles:
92
95
is_wrapped: Whether a TextIOWrapper needs to be detached.
93
96
"""
94
97
95
- handle : Buffer
98
+ handle : Buffer [ AnyStr ]
96
99
compression : CompressionDict
97
100
created_handles : list [Buffer ] = dataclasses .field (default_factory = list )
98
101
is_wrapped : bool = False
@@ -118,7 +121,7 @@ def close(self) -> None:
118
121
self .created_handles = []
119
122
self .is_wrapped = False
120
123
121
- def __enter__ (self ) -> IOHandles :
124
+ def __enter__ (self ) -> IOHandles [ AnyStr ] :
122
125
return self
123
126
124
127
def __exit__ (self , * args : Any ) -> None :
@@ -533,16 +536,47 @@ def check_parent_directory(path: Path | str) -> None:
533
536
raise OSError (fr"Cannot save file into a non-existent directory: '{ parent } '" )
534
537
535
538
539
+ @overload
536
540
def get_handle (
537
541
path_or_buf : FilePathOrBuffer ,
538
542
mode : str ,
543
+ * ,
544
+ encoding : str | None = ...,
545
+ compression : CompressionOptions = ...,
546
+ memory_map : bool = ...,
547
+ is_text : Literal [False ],
548
+ errors : str | None = ...,
549
+ storage_options : StorageOptions = ...,
550
+ ) -> IOHandles [bytes ]:
551
+ ...
552
+
553
+
554
+ @overload
555
+ def get_handle (
556
+ path_or_buf : FilePathOrBuffer ,
557
+ mode : str ,
558
+ * ,
559
+ encoding : str | None = ...,
560
+ compression : CompressionOptions = ...,
561
+ memory_map : bool = ...,
562
+ is_text : Literal [True ] = True ,
563
+ errors : str | None = ...,
564
+ storage_options : StorageOptions = ...,
565
+ ) -> IOHandles [str ]:
566
+ ...
567
+
568
+
569
+ def get_handle (
570
+ path_or_buf : FilePathOrBuffer ,
571
+ mode : str ,
572
+ * ,
539
573
encoding : str | None = None ,
540
574
compression : CompressionOptions = None ,
541
575
memory_map : bool = False ,
542
576
is_text : bool = True ,
543
577
errors : str | None = None ,
544
578
storage_options : StorageOptions = None ,
545
- ) -> IOHandles :
579
+ ) -> IOHandles [ str ] | IOHandles [ bytes ] :
546
580
"""
547
581
Get file handle for given path/buffer and mode.
548
582
0 commit comments