Skip to content

Commit

Permalink
Sync typeshed (#14449)
Browse files Browse the repository at this point in the history
Source commit:

python/typeshed@ea0ae21

Note that you will need to close and re-open the PR in order to trigger
CI.

Co-authored-by: mypybot <>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and hauntsaninja authored Jan 15, 2023
1 parent 28e5436 commit f37a0ae
Show file tree
Hide file tree
Showing 23 changed files with 315 additions and 247 deletions.
4 changes: 4 additions & 0 deletions mypy/typeshed/stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ AnyStr_co = TypeVar("AnyStr_co", str, bytes, covariant=True) # noqa: Y001
# "Incomplete | None" instead of "Any | None".
Incomplete: TypeAlias = Any

# To describe a function parameter that is unused and will work with anything.
Unused: TypeAlias = object

# stable
class IdentityFunction(Protocol):
def __call__(self, __x: _T) -> _T: ...
Expand Down Expand Up @@ -205,6 +208,7 @@ class HasFileno(Protocol):

FileDescriptor: TypeAlias = int # stable
FileDescriptorLike: TypeAlias = int | HasFileno # stable
FileDescriptorOrPath: TypeAlias = int | StrOrBytesPath

# stable
class SupportsRead(Protocol[_T_co]):
Expand Down
17 changes: 8 additions & 9 deletions mypy/typeshed/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 @@ -1320,13 +1320,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 @@ -1339,7 +1338,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 @@ -1352,7 +1351,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 @@ -1363,7 +1362,7 @@ def open(
) -> BufferedRandom: ...
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: OpenBinaryModeWriting,
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
Expand All @@ -1374,7 +1373,7 @@ def open(
) -> BufferedWriter: ...
@overload
def open(
file: _OpenFile,
file: FileDescriptorOrPath,
mode: OpenBinaryModeReading,
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
Expand All @@ -1387,7 +1386,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 @@ -1400,7 +1399,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
40 changes: 37 additions & 3 deletions mypy/typeshed/stdlib/compileall.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ __all__ = ["compile_dir", "compile_file", "compile_path"]
class _SupportsSearch(Protocol):
def search(self, string: str) -> Any: ...

if sys.version_info >= (3, 9):
if sys.version_info >= (3, 10):
def compile_dir(
dir: StrPath,
maxlevels: int | None = ...,
Expand All @@ -21,7 +21,7 @@ if sys.version_info >= (3, 9):
workers: int = ...,
invalidation_mode: PycInvalidationMode | None = ...,
*,
stripdir: str | None = ..., # TODO: change to StrPath | None once https://bugs.python.org/issue40447 is resolved
stripdir: StrPath | None = ...,
prependdir: StrPath | None = ...,
limit_sl_dest: StrPath | None = ...,
hardlink_dupes: bool = ...,
Expand All @@ -36,7 +36,41 @@ if sys.version_info >= (3, 9):
optimize: int = ...,
invalidation_mode: PycInvalidationMode | None = ...,
*,
stripdir: str | None = ..., # TODO: change to StrPath | None once https://bugs.python.org/issue40447 is resolved
stripdir: StrPath | None = ...,
prependdir: StrPath | None = ...,
limit_sl_dest: StrPath | None = ...,
hardlink_dupes: bool = ...,
) -> int: ...

elif sys.version_info >= (3, 9):
def compile_dir(
dir: StrPath,
maxlevels: int | None = ...,
ddir: StrPath | None = ...,
force: bool = ...,
rx: _SupportsSearch | None = ...,
quiet: int = ...,
legacy: bool = ...,
optimize: int = ...,
workers: int = ...,
invalidation_mode: PycInvalidationMode | None = ...,
*,
stripdir: str | None = ..., # https://bugs.python.org/issue40447
prependdir: StrPath | None = ...,
limit_sl_dest: StrPath | None = ...,
hardlink_dupes: bool = ...,
) -> int: ...
def compile_file(
fullname: StrPath,
ddir: StrPath | None = ...,
force: bool = ...,
rx: _SupportsSearch | None = ...,
quiet: int = ...,
legacy: bool = ...,
optimize: int = ...,
invalidation_mode: PycInvalidationMode | None = ...,
*,
stripdir: str | None = ..., # https://bugs.python.org/issue40447
prependdir: StrPath | None = ...,
limit_sl_dest: StrPath | None = ...,
hardlink_dupes: bool = ...,
Expand Down
4 changes: 2 additions & 2 deletions mypy/typeshed/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 mypy/typeshed/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
4 changes: 2 additions & 2 deletions mypy/typeshed/stdlib/email/message.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class Message:
) -> None: ...
def __init__(self, policy: Policy = ...) -> None: ...
# The following two methods are undocumented, but a source code comment states that they are public API
def set_raw(self, name: str, value: str) -> None: ...
def raw_items(self) -> Iterator[tuple[str, str]]: ...
def set_raw(self, name: str, value: _HeaderType) -> None: ...
def raw_items(self) -> Iterator[tuple[str, _HeaderType]]: ...

class MIMEPart(Message):
def __init__(self, policy: Policy | None = ...) -> None: ...
Expand Down
18 changes: 9 additions & 9 deletions mypy/typeshed/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: ...
29 changes: 10 additions & 19 deletions mypy/typeshed/stdlib/http/server.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _socket
import email.message
import io
import socketserver
Expand Down Expand Up @@ -52,25 +53,15 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
extensions_map: dict[str, str]
if sys.version_info >= (3, 12):
def __init__(
self,
request: socketserver._RequestType,
client_address: socketserver._AddressType,
server: socketserver.BaseServer,
*,
directory: str | None = ...,
index_pages: Sequence[str] | None = ...,
) -> None: ...
else:
def __init__(
self,
request: socketserver._RequestType,
client_address: socketserver._AddressType,
server: socketserver.BaseServer,
*,
directory: str | None = ...,
) -> None: ...

index_pages: ClassVar[tuple[str, ...]]
def __init__(
self,
request: socketserver._RequestType,
client_address: _socket._RetAddress,
server: socketserver.BaseServer,
*,
directory: str | None = ...,
) -> None: ...
def do_GET(self) -> None: ...
def do_HEAD(self) -> None: ...
def send_head(self) -> io.BytesIO | BinaryIO | None: ... # undocumented
Expand Down
6 changes: 3 additions & 3 deletions mypy/typeshed/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
6 changes: 6 additions & 0 deletions mypy/typeshed/stdlib/itertools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,9 @@ if sys.version_info >= (3, 10):
def __new__(cls, __iterable: Iterable[_T]) -> pairwise[tuple[_T, _T]]: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> _T_co: ...

if sys.version_info >= (3, 12):
class batched(Iterator[_T_co], Generic[_T_co]):
def __new__(cls: type[Self], iterable: Iterable[_T_co], n: int) -> Self: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> tuple[_T_co, ...]: ...
4 changes: 2 additions & 2 deletions mypy/typeshed/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: ...
Loading

0 comments on commit f37a0ae

Please sign in to comment.