Skip to content

Commit

Permalink
Move 'overload' types inline.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 7, 2023
1 parent 6fb137a commit 1465f6a
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 187 deletions.
186 changes: 186 additions & 0 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
# Concatenate paths with /
foo_txt = Path("bar") / "foo.txt"
"""
from __future__ import annotations

import builtins
import sys
import warnings
import os
Expand Down Expand Up @@ -49,6 +51,35 @@
with contextlib.suppress(ImportError):
import grp

from io import (
BufferedRandom,
BufferedReader,
BufferedWriter,
FileIO,
TextIOWrapper,
)
from typing import (
Any,
BinaryIO,
Callable,
IO,
Iterator,
Optional,
overload,
)

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from _typeshed import (
OpenBinaryMode,
OpenBinaryModeUpdating,
OpenBinaryModeReading,
OpenBinaryModeWriting,
OpenTextMode,
)
from typing_extensions import Literal

from . import matchers
from . import masks
from . import classes
Expand Down Expand Up @@ -615,6 +646,97 @@ def iglob(self, pattern):
#
# --- Reading or writing an entire file at once.

@overload
def open(
self,
mode: OpenTextMode = ...,
buffering: int = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Optional[Callable[[str, int], int]] = ...,
) -> TextIOWrapper:
...

@overload
def open(
self,
mode: OpenBinaryMode,
buffering: Literal[0],
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Callable[[str, int], int] = ...,
) -> FileIO:
...

@overload
def open(
self,
mode: OpenBinaryModeUpdating,
buffering: Literal[-1, 1] = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Callable[[str, int], int] = ...,
) -> BufferedRandom:
...

@overload
def open(
self,
mode: OpenBinaryModeReading,
buffering: Literal[-1, 1] = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Callable[[str, int], int] = ...,
) -> BufferedReader:
...

@overload
def open(
self,
mode: OpenBinaryModeWriting,
buffering: Literal[-1, 1] = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Callable[[str, int], int] = ...,
) -> BufferedWriter:
...

@overload
def open(
self,
mode: OpenBinaryMode,
buffering: int,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Callable[[str, int], int] = ...,
) -> BinaryIO:
...

@overload
def open(
self,
mode: str,
buffering: int = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Callable[[str, int], int] = ...,
) -> IO[Any]:
...

def open(self, *args, **kwargs):
"""Open this file and return a corresponding file object.
Expand All @@ -628,6 +750,48 @@ def bytes(self):
with self.open('rb') as f:
return f.read()

@overload
def chunks(
self,
size: int,
mode: OpenTextMode = ...,
buffering: int = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Optional[Callable[[str, int], int]] = ...,
) -> Iterator[str]:
...

@overload
def chunks(
self,
size: int,
mode: OpenBinaryMode,
buffering: int = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Optional[Callable[[str, int], int]] = ...,
) -> Iterator[builtins.bytes]:
...

@overload
def chunks(
self,
size: int,
mode: str,
buffering: int = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Optional[Callable[[str, int], int]] = ...,
) -> Iterator[Union[str, builtins.bytes]]:
...

def chunks(self, size, *args, **kwargs):
"""Returns a generator yielding chunks of the file, so it can
be read piece by piece with a simple for loop.
Expand Down Expand Up @@ -681,6 +845,28 @@ def text(self, encoding=None, errors='strict'):
)
return U_NEWLINE.sub('\n', self.read_text(encoding, errors))

@overload
def write_text(
self,
text: str,
encoding: Optional[str] = ...,
errors: str = ...,
linesep: Optional[str] = ...,
append: bool = ...,
) -> None:
...

@overload
def write_text(
self,
text: builtins.bytes,
encoding: None = ...,
errors: str = ...,
linesep: Optional[str] = ...,
append: bool = ...,
) -> None:
...

def write_text(
self, text, encoding=None, errors='strict', linesep=os.linesep, append=False
):
Expand Down
Loading

0 comments on commit 1465f6a

Please sign in to comment.