Skip to content

Commit 16f9cdd

Browse files
author
hauntsaninja
committed
pathlib.Path.open: bring on the overloads
1 parent 3b6925b commit 16f9cdd

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

stdlib/3/pathlib.pyi

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
import sys
3-
from _typeshed import OpenBinaryMode, OpenTextMode
3+
from _typeshed import OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode
4+
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
45
from types import TracebackType
56
from typing import IO, Any, BinaryIO, Generator, List, Optional, Sequence, TextIO, Tuple, Type, TypeVar, Union, overload
7+
from typing_extensions import Literal
68

79
_P = TypeVar("_P", bound=PurePath)
810

@@ -83,6 +85,9 @@ class Path(PurePath):
8385
def mkdir(self, mode: int = ..., parents: bool = ...) -> None: ...
8486
else:
8587
def mkdir(self, mode: int = ..., parents: bool = ..., exist_ok: bool = ...) -> None: ...
88+
89+
# Adapted from builtins.open
90+
# Text mode: always returns a TextIOWrapper
8691
@overload
8792
def open(
8893
self,
@@ -91,11 +96,56 @@ class Path(PurePath):
9196
encoding: Optional[str] = ...,
9297
errors: Optional[str] = ...,
9398
newline: Optional[str] = ...,
94-
) -> TextIO: ...
99+
) -> TextIOWrapper: ...
100+
# Unbuffered binary mode: returns a FileIO
101+
@overload
102+
def open(
103+
self,
104+
mode: OpenBinaryMode,
105+
buffering: Literal[0],
106+
encoding: None = ...,
107+
errors: None = ...,
108+
newline: None = ...,
109+
) -> FileIO: ...
110+
# Buffering is on: return BufferedRandom, BufferedReader, or BufferedWriter
95111
@overload
96112
def open(
97-
self, mode: OpenBinaryMode, buffering: int = ..., encoding: None = ..., errors: None = ..., newline: None = ...
113+
self,
114+
mode: OpenBinaryModeUpdating,
115+
buffering: Literal[-1, 1] = ...,
116+
encoding: None = ...,
117+
errors: None = ...,
118+
newline: None = ...,
119+
) -> BufferedRandom: ...
120+
@overload
121+
def open(
122+
self,
123+
mode: OpenBinaryModeWriting,
124+
buffering: Literal[-1, 1] = ...,
125+
encoding: None = ...,
126+
errors: None = ...,
127+
newline: None = ...,
128+
) -> BufferedWriter: ...
129+
@overload
130+
def open(
131+
self,
132+
mode: OpenBinaryModeReading,
133+
buffering: Literal[-1, 1] = ...,
134+
encoding: None = ...,
135+
errors: None = ...,
136+
newline: None = ...,
137+
) -> BufferedReader: ...
138+
# Buffering cannot be determined: fall back to BinaryIO
139+
@overload
140+
def open(
141+
self,
142+
mode: OpenBinaryMode,
143+
buffering: int,
144+
encoding: None = ...,
145+
errors: None = ...,
146+
newline: None = ...,
98147
) -> BinaryIO: ...
148+
# Fallback if mode is not specified
99149
@overload
100150
def open(
101151
self,
@@ -105,6 +155,7 @@ class Path(PurePath):
105155
errors: Optional[str] = ...,
106156
newline: Optional[str] = ...,
107157
) -> IO[Any]: ...
158+
108159
def owner(self) -> str: ...
109160
if sys.version_info >= (3, 9):
110161
def readlink(self: _P) -> _P: ...

0 commit comments

Comments
 (0)