8000 Use FileDescriptorOrPath in stdlib by Avasam · Pull Request #9513 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Use FileDescriptorOrPath in stdlib #9513

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

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import types
from _collections_abc import dict_items, dict_keys, dict_values
from _typeshed import (
AnyStr_co,
FileDescriptorOrPath,
OpenBinaryMode,
OpenBinaryModeReading,
OpenBinaryModeUpdating,
OpenBinaryModeWriting,
OpenTextMode,
ReadableBuffer,
Self,
StrOrBytesPath,
SupportsAdd,
SupportsAiter,
SupportsAnext,
Expand Down Expand Up @@ -1412,13 +1412,12 @@ def next(__i: SupportsNext[_T]) -> _T: ...
def next(__i: SupportsNext[_T], __default: _VT) -> _T | _VT: ...
def oct(__number: int | SupportsIndex) -> str: ...

_OpenFile = StrOrBytesPath | int # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed
_Opener: TypeAlias = Callable[[str, int], int]

# Text mode: always returns a TextIOWrapper
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: OpenTextMode = ...,
buffering: int = ...,
encoding: str | None = ...,
Expand All @@ -1431,7 +1430,7 @@ def open(
# Unbuffered binary mode: returns a FileIO
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: OpenBinaryMode,
buffering: Literal[0],
encoding: None = ...,
Expand All @@ -1444,7 +1443,7 @@ def open(
# Buffering is on: return BufferedRandom, BufferedReader, or BufferedWriter
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: OpenBinaryModeUpdating,
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
Expand All @@ -1455,7 +1454,7 @@ def open(
) -> BufferedRandom: ...
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: OpenBinaryModeWriting,
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
Expand All @@ -1466,7 +1465,7 @@ def open(
) -> BufferedWriter: ...
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: OpenBinaryModeReading,
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
Expand All @@ -1479,7 +1478,7 @@ def open(
# Buffering cannot be determined: fall back to BinaryIO
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: OpenBinaryMode,
buffering: int = ...,
encoding: None = ...,
Expand All @@ -1492,7 +1491,7 @@ def open(
# Fallback if mode is not specified
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: str,
buffering: int = ...,
encoding: str | None = ...,
Expand Down
4 changes: 2 additions & 2 deletions stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import abc
import sys
from _typeshed import Self, StrOrBytesPath
from _typeshed import FileDescriptorOrPath, Self
from abc import abstractmethod
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
from types import TracebackType
Expand Down Expand Up @@ -193,7 +193,7 @@ else:
def __exit__(self, *exctype: object) -> None: ...

if sys.version_info >= (3, 11):
_T_fd_or_any_path = TypeVar("_T_fd_or_any_path", bound=int | StrOrBytesPath)
_T_fd_or_any_path = TypeVar("_T_fd_or_any_path", bound=FileDescriptorOrPath)

class chdir(AbstractContextManager[None], Generic[_T_fd_or_any_path]):
path: _T_fd_or_any_path
Expand Down
4 changes: 2 additions & 2 deletions stdlib/distutils/dist.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from _typeshed import StrOrBytesPath, SupportsWrite
from _typeshed import FileDescriptorOrPath, SupportsWrite
from collections.abc import Iterable, Mapping
from distutils.cmd import Command
from typing import IO, Any

class DistributionMetadata:
def __init__(self, path: int | StrOrBytesPath | None = ...) -> None: ...
def __init__(self, path: FileDescriptorOrPath | None = ...) -> None: ...
name: str | None
version: str | None
author: str | None
Expand Down
18 changes: 9 additions & 9 deletions stdlib/genericpath.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from _typeshed import BytesPath, StrOrBytesPath, StrPath, SupportsRichComparisonT
from _typeshed import BytesPath, FileDescriptorOrPath, StrPath, SupportsRichComparisonT
from collections.abc import Sequence
from typing import overload
from typing_extensions import Literal, LiteralString
Expand Down Expand Up @@ -31,16 +31,16 @@ def commonprefix(m: Sequence[BytesPath]) -> bytes | Literal[""]: ...
def commonprefix(m: Sequence[list[SupportsRichComparisonT]]) -> Sequence[SupportsRichComparisonT]: ...
@overload
def commonprefix(m: Sequence[tuple[SupportsRichComparisonT, ...]]) -> Sequence[SupportsRichComparisonT]: ...
def exists(path: StrOrBytesPath | int) -> bool: ...
def getsize(filename: StrOrBytesPath | int) -> int: ...
def isfile(path: StrOrBytesPath | int) -> bool: ...
def isdir(s: StrOrBytesPath | int) -> bool: ...
def exists(path: FileDescriptorOrPath) -> bool: ...
def getsize(filename: FileDescriptorOrPath) -> int: ...
def isfile(path: FileDescriptorOrPath) -> bool: ...
def isdir(s: FileDescriptorOrPath) -> bool: ...

# These return float if os.stat_float_times() == True,
# but int is a subclass of float.
def getatime(filename: StrOrBytesPath | int) -> float: ...
def getmtime(filename: StrOrBytesPath | int) -> float: ...
def getctime(filename: StrOrBytesPath | int) -> float: ...
def samefile(f1: StrOrBytesPath | int, f2: StrOrBytesPath | int) -> bool: ...
def getatime(filename: FileDescriptorOrPath) -> float: ...
def getmtime(filename: FileDescriptorOrPath) -> float: ...
def getctime(filename: FileDescriptorOrPath) -> float: ...
def samefile(f1: FileDescriptorOrPath, f2: FileDescriptorOrPath) -> bool: ...
def sameopenfile(fp1: int, fp2: int) -> bool: ...
def samestat(s1: os.stat_result, s2: os.stat_result) -> bool: ...
6 changes: 3 additions & 3 deletions stdlib/io.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import abc
import builtins
import codecs
import sys
from _typeshed import ReadableBuffer, Self, StrOrBytesPath, WriteableBuffer
from _typeshed import FileDescriptorOrPath, ReadableBuffer, Self, WriteableBuffer
from collections.abc import Callable, Iterable, Iterator
from os import _Opener
from types import TracebackType
Expand Down Expand Up @@ -92,9 +92,9 @@ class BufferedIOBase(IOBase):

class FileIO(RawIOBase, BinaryIO):
mode: str
name: StrOrBytesPath | int # type: ignore[assignment]
name: FileDescriptorOrPath # type: ignore[assignment]
def __init__(
self, file: StrOrBytesPath | int, mode: str = ..., closefd: bool = ..., opener: _Opener | None = ...
self, file: FileDescriptorOrPath, mode: str = ..., closefd: bool = ..., opener: _Opener | None = ...
) -> None: ...
@property
def closefd(self) -> bool: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/multiprocessing/resource_tracker.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from _typeshed import Incomplete, StrOrBytesPath
from _typeshed import FileDescriptorOrPath, Incomplete
from collections.abc import Sized

__all__ = ["ensure_running", "register", "unregister"]
Expand All @@ -15,4 +15,4 @@ register = _resource_tracker.register
unregister = _resource_tracker.unregister
getfd = _resource_tracker.getfd

def main(fd: StrOrBytesPath | int) -> None: ...
def main(fd: FileDescriptorOrPath) -> None: ...
39 changes: 22 additions & 17 deletions stdlib/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from _typeshed import (
AnyStr_co,
BytesPath,
FileDescriptorLike,
FileDescriptorOrPath,
GenericPath,
OpenBinaryMode,
OpenBinaryModeReading,
Expand Down Expand Up @@ -370,9 +371,6 @@ def listdir(path: StrPath | None = ...) -> list[str]: ...
def listdir(path: BytesPath) -> list[bytes]: ...
@overload
def listdir(path: int) -> list[str]: ...

_FdOrAnyPath: TypeAlias = int | StrOrBytesPath

@final
class DirEntry(Generic[AnyStr]):
# This is what the scandir iterator yields
Expand Down Expand Up @@ -676,16 +674,16 @@ if sys.platform != "win32":

def write(__fd: int, __data: ReadableBuffer) -> int: ...
def access(
path: _FdOrAnyPath, mode: int, *, dir_fd: int | None = ..., effective_ids: bool = ..., follow_symlinks: bool = ...
path: FileDescriptorOrPath, mode: int, *, dir_fd: int | None = ..., effective_ids: bool = ..., follow_symlinks: bool = ...
) -> bool: ...
def chdir(path: _FdOrAnyPath) -> None: ...
def chdir(path: FileDescriptorOrPath) -> None: ...

if sys.platform != "win32":
def fchdir(fd: FileDescriptorLike) -> None: ...

def getcwd() -> str: ...
def getcwdb() -> bytes: ...
def chmod(path: _FdOrAnyPath, mode: int, *, dir_fd: int | None = ..., follow_symlinks: bool = ...) -> None: ...
def chmod(path: FileDescriptorOrPath, mode: int, *, dir_fd: int | None = ..., follow_symlinks: bool = ...) -> None: ...

if sys.platform != "win32" and sys.platform != "linux":
def chflags(path: StrOrBytesPath, flags: int, follow_symlinks: bool = ...) -> None: ... # some flavors of Unix
Expand All @@ -694,7 +692,9 @@ if sys.platform != "win32" and sys.platform != "linux":

if sys.platform != "win32":
def chroot(path: StrOrBytesPath) -> None: ...
def chown(path: _FdOrAnyPath, uid: int, gid: int, *, dir_fd: int | None = ..., follow_symlinks: bool = ...) -> None: ...
def chown(
path: FileDescriptorOrPath, uid: int, gid: int, *, dir_fd: int | None = ..., follow_symlinks: bool = ...
) -> None: ...
def lchown(path: StrOrBytesPath, uid: int, gid: int) -> None: ...

def link(
Expand All @@ -718,7 +718,7 @@ if sys.platform != "win32":
def major(__device: int) -> int: ...
def minor(__device: int) -> int: ...
def makedev(__major: int, __minor: int) -> int: ...
def pathconf(path: _FdOrAnyPath, name: str | int) -> int: ... # Unix only
def pathconf(path: FileDescriptorOrPath, name: str | int) -> int: ... # Unix only

def readlink(path: GenericPath[AnyStr], *, dir_fd: int | None = ...) -> AnyStr: ...
def remove(path: StrOrBytesPath, *, dir_fd: int | None = ...) -> None: ...
Expand All @@ -739,20 +739,20 @@ def scandir(path: None = ...) -> _ScandirIterator[str]: ...
def scandir(path: int) -> _ScandirIterator[str]: ...
@overload
def scandir(path: GenericPath[AnyStr]) -> _ScandirIterator[AnyStr]: ...
def stat(path: _FdOrAnyPath, *, dir_fd: int | None = ..., follow_symlinks: bool = ...) -> stat_result: ...
def stat(path: FileDescriptorOrPath, *, dir_fd: int | None = ..., follow_symlinks: bool = ...) -> stat_result: ...

if sys.platform != "win32":
def statvfs(path: _FdOrAnyPath) -> statvfs_result: ... # Unix only
def statvfs(path: FileDescriptorOrPath) -> statvfs_result: ... # Unix only

def symlink(src: StrOrBytesPath, dst: StrOrBytesPath, target_is_directory: bool = ..., *, dir_fd: int | None = ...) -> None: ...

if sys.platform != "win32":
def sync() -> None: ... # Unix only

def truncate(path: _FdOrAnyPath, length: int) -> None: ... # Unix only up to version 3.4
def truncate(path: FileDescriptorOrPath, length: int) -> None: ... # Unix only up to version 3.4
def unlink(path: StrOrBytesPath, *, dir_fd: int | None = ...) -> None: ...
def utime(
path: _FdOrAnyPath,
path: FileDescriptorOrPath,
times: tuple[int, int] | tuple[float, float] | None = ...,
*,
ns: tuple[int, int] = ...,
Expand Down Expand Up @@ -786,11 +786,16 @@ if sys.platform != "win32":
dir_fd: int | None = ...,
) -> Iterator[tuple[bytes, list[bytes], list[bytes], int]]: ...
if sys.platform == "linux":
def getxattr(path: _FdOrAnyPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> bytes: ...
def listxattr(path: _FdOrAnyPath | None = ..., *, follow_symlinks: bool = ...) -> list[str]: ...
def removexattr(path: _FdOrAnyPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> None: ...
def getxattr(path: FileDescriptorOrPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> bytes: ...
def listxattr(path: FileDescriptorOrPath | None = ..., *, follow_symlinks: bool = ...) -> list[str]: ...
def removexattr(path: FileDescriptorOrPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> None: ...
def setxattr(
path: _FdOrAnyPath, attribute: StrOrBytesPath, value: ReadableBuffer, flags: int = ..., *, follow_symlinks: bool = ...
path: FileDescriptorOrPath,
attribute: StrOrBytesPath,
value: ReadableBuffer,
flags: int = ...,
*,
follow_symlinks: bool = ...,
) -> None: ...

def abort() -> NoReturn: ...
Expand Down Expand Up @@ -825,7 +830,7 @@ _ExecVArgs: TypeAlias = (
_ExecEnv: TypeAlias = Mapping[bytes, bytes | str] | Mapping[str, bytes | str]

def execv(__path: StrOrBytesPath, __argv: _ExecVArgs) -> NoReturn: ...
def execve(path: _FdOrAnyPath, argv: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
def execve(path: FileDescriptorOrPath, argv: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
def execvp(file: StrOrBytesPath, args: _ExecVArgs) -> NoReturn: ...
def execvpe(file: StrOrBytesPath, args: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
def _exit(status: int) -> NoReturn: ...
Expand Down
8 changes: 4 additions & 4 deletions stdlib/posixpath.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import AnyOrLiteralStr, BytesPath, StrOrBytesPath, StrPath
from _typeshed import AnyOrLiteralStr, BytesPath, FileDescriptorOrPath, StrOrBytesPath, StrPath
from collections.abc import Sequence
from genericpath import (
commonprefix as commonprefix,
Expand Down Expand Up @@ -147,6 +147,6 @@ def splitext(p: PathLike[AnyStr]) -> tuple[AnyStr, AnyStr]: ...
@overload
def splitext(p: AnyOrLiteralStr) -> tuple[AnyOrLiteralStr, AnyOrLiteralStr]: ...
def isabs(s: StrOrBytesPath) -> bool: ...
def islink(path: StrOrBytesPath | int) -> bool: ...
def ismount(path: StrOrBytesPath | int) -> bool: ...
def lexists(path: StrOrBytesPath | int) -> bool: ...
def islink(path: FileDescriptorOrPath) -> bool: ...
def ismount(path: FileDescriptorOrPath) -> bool: ...
def lexists(path: FileDescriptorOrPath) -> bool: ...
4 changes: 2 additions & 2 deletions stdlib/shutil.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
from _typeshed import BytesPath, StrOrBytesPath, StrPath, SupportsRead, SupportsWrite
from _typeshed import BytesPath, FileDescriptorOrPath, StrOrBytesPath, StrPath, SupportsRead, SupportsWrite
from collections.abc import Callable, Iterable, Sequence
from typing import Any, AnyStr, NamedTuple, Protocol, TypeVar, overload
from typing_extensions import TypeAlias
Expand Down Expand Up @@ -118,7 +118,7 @@ class _ntuple_diskusage(NamedTuple):
used: int
free: int

def disk_usage(path: int | StrOrBytesPath) -> _ntuple_diskusage: ...
def disk_usage(path: FileDescriptorOrPath) -> _ntuple_diskusage: ...

# While chown can be imported on Windows, it doesn't actually work;
# see https://bugs.python.org/issue33140. We keep it here because it's
Expand Down
4 changes: 2 additions & 2 deletions stdlib/tokenize.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import StrOrBytesPath
from _typeshed import FileDescriptorOrPath
from collections.abc import Callable, Generator, Iterable, Sequence
from re import Pattern
from token import *
Expand Down Expand Up @@ -125,7 +125,7 @@ def untokenize(iterable: Iterable[_Token]) -> Any: ...
def detect_encoding(readline: Callable[[], bytes | bytearray]) -> tuple[str, Sequence[bytes]]: ...
def tokenize(readline: Callable[[], bytes | bytearray]) -> Generator[TokenInfo, None, None]: ...
def generate_tokens(readline: Callable[[], str]) -> Generator[TokenInfo, None, None]: ... # undocumented
def open(filename: StrOrBytesPath | int) -> TextIO: ...
def open(filename: FileDescriptorOrPath) -> TextIO: ...
def group(*choices: str) -> str: ... # undocumented
def any(*choices: str) -> str: ... # undocumented
def maybe(*choices: str) -> str: ... # undocumented
Expand Down
4 changes: 2 additions & 2 deletions stdlib/xml/etree/ElementInclude.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import StrOrBytesPath
from _typeshed import FileDescriptorOrPath
from collections.abc import Callable
from xml.etree.ElementTree import Element

Expand All @@ -12,7 +12,7 @@ if sys.version_info >= (3, 9):

class FatalIncludeError(SyntaxError): ...

def default_loader(href: StrOrBytesPath | int, parse: str, encoding: str | None = ...) -> str | Element: ...
def default_loader(href: FileDescriptorOrPath, parse: str, encoding: str | None = ...) -> str | Element: ...

# TODO: loader is of type default_loader ie it takes a callable that has the
# same signature as default_loader. But default_loader has a keyword argument
Expand Down
6 changes: 3 additions & 3 deletions stdlib/xml/etree/ElementTree.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from _collections_abc import dict_keys
from _typeshed import FileDescriptor, ReadableBuffer, StrOrBytesPath, SupportsRead, SupportsWrite
from _typeshed import FileDescriptorOrPath, ReadableBuffer, SupportsRead, SupportsWrite
from collections.abc import Callable, Generator, ItemsView, Iterable, Iterator, Mapping, Sequence
from typing import Any, TypeVar, overload
from typing_extensions import Literal, SupportsIndex, TypeAlias, TypeGuard
Expand Down Expand Up @@ -38,8 +38,8 @@ if sys.version_info >= (3, 9):
__all__ += ["indent"]

_T = TypeVar("_T")
_FileRead: TypeAlias = StrOrBytesPath | FileDescriptor | SupportsRead[bytes] | SupportsRead[str]
_FileWriteC14N: TypeAlias = StrOrBytesPath | FileDescriptor | SupportsWrite[bytes]
_FileRead: TypeAlias = FileDescriptorOrPath | SupportsRead[bytes] | SupportsRead[str]
_FileWriteC14N: TypeAlias = FileDescriptorOrPath | SupportsWrite[bytes]
_FileWrite: TypeAlias = _FileWriteC14N | SupportsWrite[str]

VERSION: str
Expand Down
0