Skip to content

Commit f37a0ae

Browse files
Sync typeshed (#14449)
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>
1 parent 28e5436 commit f37a0ae

File tree

23 files changed

+315
-247
lines changed

23 files changed

+315
-247
lines changed

mypy/typeshed/stdlib/_typeshed/__init__.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ AnyStr_co = TypeVar("AnyStr_co", str, bytes, covariant=True) # noqa: Y001
3636
# "Incomplete | None" instead of "Any | None".
3737
Incomplete: TypeAlias = Any
3838

39+
# To describe a function parameter that is unused and will work with anything.
40+
Unused: TypeAlias = object
41+
3942
# stable
4043
class IdentityFunction(Protocol):
4144
def __call__(self, __x: _T) -> _T: ...
@@ -205,6 +208,7 @@ class HasFileno(Protocol):
205208

206209
FileDescriptor: TypeAlias = int # stable
207210
FileDescriptorLike: TypeAlias = int | HasFileno # stable
211+
FileDescriptorOrPath: TypeAlias = int | StrOrBytesPath
208212

209213
# stable
210214
class SupportsRead(Protocol[_T_co]):

mypy/typeshed/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,
@@ -1320,13 +1320,12 @@ def next(__i: SupportsNext[_T]) -> _T: ...
13201320
def next(__i: SupportsNext[_T], __default: _VT) -> _T | _VT: ...
13211321
def oct(__number: int | SupportsIndex) -> str: ...
13221322

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

13261325
# Text mode: always returns a TextIOWrapper
13271326
@overload
13281327
def open(
1329-
file: _OpenFile,
1328+
file: FileDescriptorOrPath,
13301329
mode: OpenTextMode = ...,
13311330
buffering: int = ...,
13321331
encoding: str | None = ...,
@@ -1339,7 +1338,7 @@ def open(
13391338
# Unbuffered binary mode: returns a FileIO
13401339
@overload
13411340
def open(
1342-
file: _OpenFile,
1341+
file: FileDescriptorOrPath,
13431342
mode: OpenBinaryMode,
13441343
buffering: Literal[0],
13451344
encoding: None = ...,
@@ -1352,7 +1351,7 @@ def open(
13521351
# Buffering is on: return BufferedRandom, BufferedReader, or BufferedWriter
13531352
@overload
13541353
def open(
1355-
file: _OpenFile,
1354+
file: FileDescriptorOrPath,
13561355
mode: OpenBinaryModeUpdating,
13571356
buffering: Literal[-1, 1] = ...,
13581357
encoding: None = ...,
@@ -1363,7 +1362,7 @@ def open(
13631362
) -> BufferedRandom: ...
13641363
@overload
13651364
def open(
1366-
file: _OpenFile,
1365+
file: FileDescriptorOrPath,
13671366
mode: OpenBinaryModeWriting,
13681367
buffering: Literal[-1, 1] = ...,
13691368
encoding: None = ...,
@@ -1374,7 +1373,7 @@ def open(
13741373
) -> BufferedWriter: ...
13751374
@overload
13761375
def open(
1377-
file: _OpenFile,
1376+
file: FileDescriptorOrPath,
13781377
mode: OpenBinaryModeReading,
13791378
buffering: Literal[-1, 1] = ...,
13801379
encoding: None = ...,
@@ -1387,7 +1386,7 @@ def open(
13871386
# Buffering cannot be determined: fall back to BinaryIO
13881387
@overload
13891388
def open(
1390-
file: _OpenFile,
1389+
file: FileDescriptorOrPath,
13911390
mode: OpenBinaryMode,
13921391
buffering: int = ...,
13931392
encoding: None = ...,
@@ -1400,7 +1399,7 @@ def open(
14001399
# Fallback if mode is not specified
14011400
@overload
14021401
def open(
1403-
file: _OpenFile,
1402+
file: FileDescriptorOrPath,
14041403
mode: str,
14051404
buffering: int = ...,
14061405
encoding: str | None = ...,

mypy/typeshed/stdlib/compileall.pyi

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ __all__ = ["compile_dir", "compile_file", "compile_path"]
88
class _SupportsSearch(Protocol):
99
def search(self, string: str) -> Any: ...
1010

11-
if sys.version_info >= (3, 9):
11+
if sys.version_info >= (3, 10):
1212
def compile_dir(
1313
dir: StrPath,
1414
maxlevels: int | None = ...,
@@ -21,7 +21,7 @@ if sys.version_info >= (3, 9):
2121
workers: int = ...,
2222
invalidation_mode: PycInvalidationMode | None = ...,
2323
*,
24-
stripdir: str | None = ..., # TODO: change to StrPath | None once https://bugs.python.org/issue40447 is resolved
24+
stripdir: StrPath | None = ...,
2525
prependdir: StrPath | None = ...,
2626
limit_sl_dest: StrPath | None = ...,
2727
hardlink_dupes: bool = ...,
@@ -36,7 +36,41 @@ if sys.version_info >= (3, 9):
3636
optimize: int = ...,
3737
invalidation_mode: PycInvalidationMode | None = ...,
3838
*,
39-
stripdir: str | None = ..., # TODO: change to StrPath | None once https://bugs.python.org/issue40447 is resolved
39+
stripdir: StrPath | None = ...,
40+
prependdir: StrPath | None = ...,
41+
limit_sl_dest: StrPath | None = ...,
42+
hardlink_dupes: bool = ...,
43+
) -> int: ...
44+
45+
elif sys.version_info >= (3, 9):
46+
def compile_dir(
47+
dir: StrPath,
48+
maxlevels: int | None = ...,
49+
ddir: StrPath | None = ...,
50+
force: bool = ...,
51+
rx: _SupportsSearch | None = ...,
52+
quiet: int = ...,
53+
legacy: bool = ...,
54+
optimize: int = ...,
55+
workers: int = ...,
56+
invalidation_mode: PycInvalidationMode | None = ...,
57+
*,
58+
stripdir: str | None = ..., # https://bugs.python.org/issue40447
59+
prependdir: StrPath | None = ...,
60+
limit_sl_dest: StrPath | None = ...,
61+
hardlink_dupes: bool = ...,
62+
) -> int: ...
63+
def compile_file(
64+
fullname: StrPath,
65+
ddir: StrPath | None = ...,
66+
force: bool = ...,
67+
rx: _SupportsSearch | None = ...,
68+
quiet: int = ...,
69+
legacy: bool = ...,
70+
optimize: int = ...,
71+
invalidation_mode: PycInvalidationMode | None = ...,
72+
*,
73+
stripdir: str | None = ..., # https://bugs.python.org/issue40447
4074
prependdir: StrPath | None = ...,
4175
limit_sl_dest: StrPath | None = ...,
4276
hardlink_dupes: bool = ...,

mypy/typeshed/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

mypy/typeshed/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

mypy/typeshed/stdlib/email/message.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class Message:
7676
) -> None: ...
7777
def __init__(self, policy: Policy = ...) -> None: ...
7878
# The following two methods are undocumented, but a source code comment states that they are public API
79-
def set_raw(self, name: str, value: str) -> None: ...
80-
def raw_items(self) -> Iterator[tuple[str, str]]: ...
79+
def set_raw(self, name: str, value: _HeaderType) -> None: ...
80+
def raw_items(self) -> Iterator[tuple[str, _HeaderType]]: ...
8181

8282
class MIMEPart(Message):
8383
def __init__(self, policy: Policy | None = ...) -> None: ...

mypy/typeshed/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: ...

mypy/typeshed/stdlib/http/server.pyi

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _socket
12
import email.message
23
import io
34
import socketserver
@@ -52,25 +53,15 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
5253
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
5354
extensions_map: dict[str, str]
5455
if sys.version_info >= (3, 12):
55-
def __init__(
56-
self,
57-
request: socketserver._RequestType,
58-
client_address: socketserver._AddressType,
59-
server: socketserver.BaseServer,
60-
*,
61-
directory: str | None = ...,
62-
index_pages: Sequence[str] | None = ...,
63-
) -> None: ...
64-
else:
65-
def __init__(
66-
self,
67-
request: socketserver._RequestType,
68-
client_address: socketserver._AddressType,
69-
server: socketserver.BaseServer,
70-
*,
71-
directory: str | None = ...,
72-
) -> None: ...
73-
56+
index_pages: ClassVar[tuple[str, ...]]
57+
def __init__(
58+
self,
59+
request: socketserver._RequestType,
60+
client_address: _socket._RetAddress,
61+
server: socketserver.BaseServer,
62+
*,
63+
directory: str | None = ...,
64+
) -> None: ...
7465
def do_GET(self) -> None: ...
7566
def do_HEAD(self) -> None: ...
7667
def send_head(self) -> io.BytesIO | BinaryIO | None: ... # undocumented

mypy/typeshed/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: ...

mypy/typeshed/stdlib/itertools.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,9 @@ if sys.version_info >= (3, 10):
271271
def __new__(cls, __iterable: Iterable[_T]) -> pairwise[tuple[_T, _T]]: ...
272272
def __iter__(self: Self) -> Self: ...
273273
def __next__(self) -> _T_co: ...
274+
275+
if sys.version_info >= (3, 12):
276+
class batched(Iterator[_T_co], Generic[_T_co]):
277+
def __new__(cls: type[Self], iterable: Iterable[_T_co], n: int) -> Self: ...
278+
def __iter__(self: Self) -> Self: ...
279+
def __next__(self) -> tuple[_T_co, ...]: ...

0 commit comments

Comments
 (0)