8000 Use the `FileDescriptorOrPath` alias consistently in the stdlib (#9513) · python/typeshed@aad1a14 · GitHub
[go: up one dir, main page]

Skip to content

Commit aad1a14

Browse files
authored
Use the FileDescriptorOrPath alias consistently in the stdlib (#9513)
1 parent f64807a commit aad1a14

12 files changed

+61
-57
lines changed

stdlib/builtins.pyi

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import types
44
from _collections_abc import dict_items, dict_keys, dict_values
55
from _typeshed import (
66
AnyStr_co,
7+
FileDescriptorOrPath,
78
OpenBinaryMode,
89
OpenBinaryModeReading,
910
OpenBinaryModeUpdating,
1011
OpenBinaryModeWriting,
1112
OpenTextMode,
1213
ReadableBuffer,
1314
Self,
14-
StrOrBytesPath,
1515
SupportsAdd,
1616
SupportsAiter,
1717
SupportsAnext,
@@ -1412,13 +1412,12 @@ def next(__i: SupportsNext[_T]) -> _T: ...
14121412
def next(__i: SupportsNext[_T], __default: _VT) -> _T | _VT: ...
14131413
def oct(__number: int | SupportsIndex) -> str: ...
14141414

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

14181417
# Text mode: always returns a TextIOWrapper
14191418
@overload
14201419
def open(
1421-
file: _OpenFile,
1420+
file: FileDescriptorOrPath,
14221421
mode: OpenTextMode = ...,
14231422
buffering: int = ...,
14241423
encoding: str | None = ...,
@@ -1431,7 +1430,7 @@ def open(
14311430
# Unbuffered binary mode: returns a FileIO
14321431
@overload
14331432
def open(
1434-
file: _OpenFile,
1433+
file: FileDescriptorOrPath,
14351434
mode: OpenBinaryMode,
14361435
buffering: Literal[0],
14371436
encoding: None = ...,
@@ -1444,7 +1443,7 @@ def open(
14441443
# Buffering is on: return BufferedRandom, BufferedReader, or BufferedWriter
14451444
@overload
14461445
def open(
1447-
file: _OpenFile,
1446+
file: FileDescriptorOrPath,
14481447
mode: OpenBinaryModeUpdating,
14491448
buffering: Literal[-1, 1] = ...,
14501449
encoding: None = ...,
@@ -1455,7 +1454,7 @@ def open(
14551454
) -> BufferedRandom: ...
14561455
@overload
14571456
def open(
1458-
file: _OpenFile,
1457+
file: FileDescriptorOrPath,
14591458
mode: OpenBinaryModeWriting,
14601459
buffering: Literal[-1, 1] = ...,
14611460
encoding: None = ...,
@@ -1466,7 +1465,7 @@ def open(
14661465
) -> BufferedWriter: ...
14671466
@overload
14681467
def open(
1469-
file: _OpenFile,
1468+
file: FileDescriptorOrPath,
14701469
mode: OpenBinaryModeReading,
14711470
buffering: Literal[-1, 1] = ...,
14721471
encoding: None = ...,
@@ -1479,7 +1478,7 @@ def open(
14791478
# Buffering cannot be determined: fall back to BinaryIO
14801479
@overload
14811480
def open(
1482-
file: _OpenFile,
1481+
file: FileDescriptorOrPath,
14831482
mode: OpenBinaryMode,
14841483
buffering: int = ...,
14851484
encoding: None = ...,
@@ -1492,7 +1491,7 @@ def open(
14921491
# Fallback if mode is not specified
14931492
@overload
14941493
def open(
1495-
file: _OpenFile,
1494+
file: FileDescriptorOrPath,
14961495
mode: str,
14971496
buffering: int = ...,
14981497
encoding: str | None = ...,

stdlib/contextlib.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
import sys
3-
from _typeshed import Self, StrOrBytesPath
3+
from _typeshed import FileDescriptorOrPath, Self
44
from abc import abstractmethod
55
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
66
from types import TracebackType
@@ -193,7 +193,7 @@ else:
193193
def __exit__(self, *exctype: object) -> None: ...
194194

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

198198
class chdir(AbstractContextManager[None], Generic[_T_fd_or_any_path]):
199199
path: _T_fd_or_any_path

stdlib/distutils/dist.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from _typeshed import StrOrBytesPath, SupportsWrite
1+
from _typeshed import FileDescriptorOrPath, SupportsWrite
22
from collections.abc import Iterable, Mapping
33
from distutils.cmd import Command
44
from typing import IO, Any
55

66
class DistributionMetadata:
7-
def __init__(self, path: int | StrOrBytesPath | None = ...) -> None: ...
7+
def __init__(self, path: FileDescriptorOrPath | None = ...) -> None: ...
88
name: str | None
99
version: str | None
1010
author: str | None

stdlib/genericpath.pyi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from _typeshed import BytesPath, StrOrBytesPath, StrPath, SupportsRichComparisonT
2+
from _typeshed import BytesPath, FileDescriptorOrPath, StrPath, SupportsRichComparisonT
33
from collections.abc import Sequence
44
from typing import overload
55
from typing_extensions import Literal, LiteralString
@@ -31,16 +31,16 @@ def commonprefix(m: Sequence[BytesPath]) -> bytes | Literal[""]: ...
3131
def commonprefix(m: Sequence[list[SupportsRichComparisonT]]) -> Sequence[SupportsRichComparisonT]: ...
3232
@overload
3333
def commonprefix(m: Sequence[tuple[SupportsRichComparisonT, ...]]) -> Sequence[SupportsRichComparisonT]: ...
34-
def exists(path: StrOrBytesPath | int) -> bool: ...
35-
def getsize(filename: StrOrBytesPath | int) -> int: ...
36-
def isfile(path: StrOrBytesPath | int) -> bool: ...
37-
def isdir(s: StrOrBytesPath | int) -> bool: ...
34+
def exists(path: FileDescriptorOrPath) -> bool: ...
35+
def getsize(filename: FileDescriptorOrPath) -> int: ...
36+
def isfile(path: FileDescriptorOrPath) -> bool: ...
37+
def isdir(s: FileDescriptorOrPath) -> bool: ...
3838

3939
# These return float if os.stat_float_times() == True,
4040
# but int is a subclass of float.
41-
def getatime(filename: StrOrBytesPath | int) -> float: ...
42-
def getmtime(filename: StrOrBytesPath | int) -> float: ...
43-
def getctime(filename: StrOrBytesPath | int) -> float: ...
44-
def samefile(f1: StrOrBytesPath | int, f2: StrOrBytesPath | int) -> bool: ...
41+
def getatime(filename: FileDescriptorOrPath) -> float: ...
42+
def getmtime(filename: FileDescriptorOrPath) -> float: ...
43+
def getctime(filename: FileDescriptorOrPath) -> float: ...
44+
def samefile(f1: FileDescriptorOrPath, f2: FileDescriptorOrPath) -> bool: ...
4545
def sameopenfile(fp1: int, fp2: int) -> bool: ...
4646
def samestat(s1: os.stat_result, s2: os.stat_result) -> bool: ...

stdlib/io.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import abc
22
import builtins
33
import codecs
44
import sys
5-
from _typeshed import ReadableBuffer, Self, StrOrBytesPath, WriteableBuffer
5+
from _typeshed import FileDescriptorOrPath, ReadableBuffer, Self, WriteableBuffer
66
from collections.abc import Callable, Iterable, Iterator
77
from os import _Opener
88
from types import TracebackType
@@ -92,9 +92,9 @@ class BufferedIOBase(IOBase):
9292

9393
class FileIO(RawIOBase, BinaryIO):
9494
mode: str
95-
name: StrOrBytesPath | int # type: ignore[assignment]
95+
name: FileDescriptorOrPath # type: ignore[assignment]
9696
def __init__(
97-
self, file: StrOrBytesPath | int, mode: str = ..., closefd: bool = ..., opener: _Opener | None = ...
97+
self, file: FileDescriptorOrPath, mode: str = ..., closefd: bool = ..., opener: _Opener | None = ...
9898
) -> None: ...
9999
@property
100100
def closefd(self) -> bool: ...

stdlib/multiprocessing/resource_tracker.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from _typeshed import Incomplete, StrOrBytesPath
1+
from _typeshed import FileDescriptorOrPath, Incomplete
22
from collections.abc import Sized
33

44
__all__ = ["ensure_running", "register", "unregister"]
@@ -15,4 +15,4 @@ register = _resource_tracker.register
1515
unregister = _resource_tracker.unregister
1616
getfd = _resource_tracker.getfd
1717

18-
def main(fd: StrOrBytesPath | int) -> None: ...
18+
def main(fd: FileDescriptorOrPath) -> None: ...

stdlib/os/__init__.pyi

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from _typeshed import (
33
AnyStr_co,
44
BytesPath,
55
FileDescriptorLike,
6+
FileDescriptorOrPath,
67
GenericPath,
78
OpenBinaryMode,
89
OpenBinaryModeReading,
@@ -370,9 +371,6 @@ def listdir(path: StrPath | None = ...) -> list[str]: ...
370371
def listdir(path: BytesPath) -> list[bytes]: ...
371372
@overload
372373
def listdir(path: int) -> list[str]: ...
373-
374-
_FdOrAnyPath: TypeAlias = int | StrOrBytesPath
375-
376374
@final
377375
class DirEntry(Generic[AnyStr]):
378376
# This is what the scandir iterator yields
@@ -676,16 +674,16 @@ if sys.platform != "win32":
676674

677675
def write(__fd: int, __data: ReadableBuffer) -> int: ...
678676
def access(
679-
path: _FdOrAnyPath, mode: int, *, dir_fd: int | None = ..., effective_ids: bool = ..., follow_symlinks: bool = ...
677+
path: FileDescriptorOrPath, mode: int, *, dir_fd: int | None = ..., effective_ids: bool = ..., follow_symlinks: bool = ...
680678
) -> bool: ...
681-
def chdir(path: _FdOrAnyPath) -> None: ...
679+
def chdir(path: FileDescriptorOrPath) -> None: ...
682680

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

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

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

695693
if sys.platform != "win32":
696694
def chroot(path: StrOrBytesPath) -> None: ...
697-
def chown(path: _FdOrAnyPath, uid: int, gid: int, *, dir_fd: int | None = ..., follow_symlinks: bool = ...) -> None: ...
695+
def chown(
696+
path: FileDescriptorOrPath, uid: int, gid: int, *, dir_fd: int | None = ..., follow_symlinks: bool = ...
697+
) -> None: ...
698698
def lchown(path: StrOrBytesPath, uid: int, gid: int) -> None: ...
699699

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

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

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

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

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

752-
def truncate(path: _FdOrAnyPath, length: int) -> None: ... # Unix only up to version 3.4
752+
def truncate(path: FileDescriptorOrPath, length: int) -> None: ... # Unix only up to version 3.4
753753
def unlink(path: StrOrBytesPath, *, dir_fd: int | None = ...) -> None: ...
754754
def utime(
755-
path: _FdOrAnyPath,
755+
path: FileDescriptorOrPath,
756756
times: tuple[int, int] | tuple[float, float] | None = ...,
757757
*,
758758
ns: tuple[int, int] = ...,
@@ -786,11 +786,16 @@ if sys.platform != "win32":
786786
dir_fd: int | None = ...,
787787
) -> Iterator[tuple[bytes, list[bytes], list[bytes], int]]: ...
788788
if sys.platform == "linux":
789-
def getxattr(path: _FdOrAnyPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> bytes: ...
790-
def listxattr(path: _FdOrAnyPath | None = ..., *, follow_symlinks: bool = ...) -> list[str]: ...
791-
def removexattr(path: _FdOrAnyPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> None: ...
789+
def getxattr(path: FileDescriptorOrPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> bytes: ...
790+
def listxattr(path: FileDescriptorOrPath | None = ..., *, follow_symlinks: bool = ...) -> list[str]: ...
791+
def removexattr(path: FileDescriptorOrPath, attribute: StrOrBytesPath, *, follow_symlinks: bool = ...) -> None: ...
792792
def setxattr(
793-
path: _FdOrAnyPath, attribute: StrOrBytesPath, value: ReadableBuffer, flags: int = ..., *, follow_symlinks: bool = ...
793+
path: FileDescriptorOrPath,
794+
attribute: StrOrBytesPath,
795+
value: ReadableBuffer,
796+
flags: int = ...,
797+
*,
798+
follow_symlinks: bool = ...,
794799
) -> None: ...
795800

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

827832
def execv(__path: StrOrBytesPath, __argv: _ExecVArgs) -> NoReturn: ...
828-
def execve(path: _FdOrAnyPath, argv: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
833+
def execve(path: FileDescriptorOrPath, argv: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
829834
def execvp(file: StrOrBytesPath, args: _ExecVArgs) -> NoReturn: ...
830835
def execvpe(file: StrOrBytesPath, args: _ExecVArgs, env: _ExecEnv) -> NoReturn: ...
831836
def _exit(status: int) -> NoReturn: ...

stdlib/posixpath.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import AnyOrLiteralStr, BytesPath, StrOrBytesPath, StrPath
2+
from _typeshed import AnyOrLiteralStr, BytesPath, FileDescriptorOrPath, StrOrBytesPath, StrPath
33
from collections.abc import Sequence
44
from genericpath import (
55
commonprefix as commonprefix,
@@ -147,6 +147,6 @@ def splitext(p: PathLike[AnyStr]) -> tuple[AnyStr, AnyStr]: ...
147147
@overload
148148
def splitext(p: AnyOrLiteralStr) -> tuple[AnyOrLiteralStr, AnyOrLiteralStr]: ...
149149
def isabs(s: StrOrBytesPath) -> bool: ...
150-
def islink(path: StrOrBytesPath | int) -> bool: ...
151-
def ismount(path: StrOrBytesPath | int) -> bool: ...
152-
def lexists(path: StrOrBytesPath | int) -> bool: ...
150+
def islink(path: FileDescriptorOrPath) -> bool: ...
151+
def ismount(path: FileDescriptorOrPath) -> bool: ...
152+
def lexists(path: FileDescriptorOrPath) -> bool: ...

stdlib/shutil.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
from _typeshed import BytesPath, StrOrBytesPath, StrPath, SupportsRead, SupportsWrite
3+
from _typeshed import BytesPath, FileDescriptorOrPath, StrOrBytesPath, StrPath, SupportsRead, SupportsWrite
44
from collections.abc import Callable, Iterable, Sequence
55
from typing import Any, AnyStr, NamedTuple, Protocol, TypeVar, overload
66
from typing_extensions import TypeAlias
@@ -118,7 +118,7 @@ class _ntuple_diskusage(NamedTuple):
118118
used: int
119119
free: int
120120

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

123123
# While chown can be imported on Windows, it doesn't actually work;
124124
# see https://bugs.python.org/issue33140. We keep it here because it's

stdlib/tokenize.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import StrOrBytesPath
2+
from _typeshed import FileDescriptorOrPath
33
from collections.abc import Callable, Generator, Iterable, Sequence
44
from re import Pattern
55
from token import *
@@ -125,7 +125,7 @@ def untokenize(iterable: Iterable[_Token]) -> Any: ...
125125
def detect_encoding(readline: Callable[[], bytes | bytearray]) -> tuple[str, Sequence[bytes]]: ...
126126
def tokenize(readline: Callable[[], bytes | bytearray]) -> Generator[TokenInfo, None, None]: ...
127127
def generate_tokens(readline: Callable[[], str]) -> Generator[TokenInfo, None, None]: ... # undocumented
128-
def open(filename: StrOrBytesPath | int) -> TextIO: ...
128+
def open(filename: FileDescriptorOrPath) -> TextIO: ...
129129
def group(*choices: str) -> str: ... # undocumented
130130
def any(*choices: str) -> str: ... # undocumented
131131
def maybe(*choices: str) -> str: ... # undocumented

stdlib/xml/etree/ElementInclude.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import StrOrBytesPath
2+
from _typeshed import FileDescriptorOrPath
33
from collections.abc import Callable
44
from xml.etree.ElementTree import Element
55

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

1313
class FatalIncludeError(SyntaxError): ...
1414

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

1717
# TODO: loader is of type default_loader ie it takes a callable that has the
1818
# same signature as default_loader. But default_loader has a keyword argument

stdlib/xml/etree/ElementTree.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from _collections_abc import dict_keys
3-
from _typeshed import FileDescriptor, ReadableBuffer, StrOrBytesPath, SupportsRead, SupportsWrite
3+
from _typeshed import FileDescriptorOrPath, ReadableBuffer, SupportsRead, SupportsWrite
44
from collections.abc import Callable, Generator, ItemsView, Iterable, Iterator, Mapping, Sequence
55
from typing import Any, TypeVar, overload
66
from typing_extensions import Literal, SupportsIndex, TypeAlias, TypeGuard
@@ -38,8 +38,8 @@ if sys.version_info >= (3, 9):
3838
__all__ += ["indent"]
3939

4040
_T = TypeVar("_T")
41-
_FileRead: TypeAlias = StrOrBytesPath | FileDescriptor | SupportsRead[bytes] | SupportsRead[str]
42-
_FileWriteC14N: TypeAlias = StrOrBytesPath | FileDescriptor | SupportsWrite[bytes]
41+
_FileRead: TypeAlias = FileDescriptorOrPath | SupportsRead[bytes] | SupportsRead[str]
42+
_FileWriteC14N: TypeAlias = FileDescriptorOrPath | SupportsWrite[bytes]
4343
_FileWrite: TypeAlias = _FileWriteC14N | SupportsWrite[str]
4444

4545
VERSION: str

0 commit comments

Comments
 (0)
0