Skip to content

Commit f554f54

Browse files
authored
The four protocol-like ABCs outside of collections.abc inherit from ABC directly (#13005)
1 parent 7c7629d commit f554f54

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

stdlib/contextlib.pyi

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import abc
22
import sys
33
from _typeshed import FileDescriptorOrPath, Unused
4-
from abc import abstractmethod
4+
from abc import ABC, abstractmethod
55
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
66
from types import TracebackType
77
from typing import IO, Any, Generic, Protocol, TypeVar, overload, runtime_checkable
@@ -38,16 +38,22 @@ _P = ParamSpec("_P")
3838
_ExitFunc: TypeAlias = Callable[[type[BaseException] | None, BaseException | None, TracebackType | None], bool | None]
3939
_CM_EF = TypeVar("_CM_EF", bound=AbstractContextManager[Any, Any] | _ExitFunc)
4040

41+
# mypy and pyright object to this being both ABC and Protocol.
42+
# At runtime it inherits from ABC and is not a Protocol, but it is on the
43+
# allowlist for use as a Protocol.
4144
@runtime_checkable
42-
class AbstractContextManager(Protocol[_T_co, _ExitT_co]):
45+
class AbstractContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
4346
def __enter__(self) -> _T_co: ...
4447
@abstractmethod
4548
def __exit__(
4649
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, /
4750
) -> _ExitT_co: ...
4851

52+
# mypy and pyright object to this being both ABC and Protocol.
53+
# At runtime it inherits from ABC and is not a Protocol, but it is on the
54+
# allowlist for use as a Protocol.
4955
@runtime_checkable
50-
class AbstractAsyncContextManager(Protocol[_T_co, _ExitT_co]):
56+
class AbstractAsyncContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
5157
async def __aenter__(self) -> _T_co: ...
5258
@abstractmethod
5359
async def __aexit__(

stdlib/os/__init__.pyi

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ from _typeshed import (
1919
WriteableBuffer,
2020
structseq,
2121
)
22-
from abc import abstractmethod
22+
from abc import ABC, abstractmethod
2323
from builtins import OSError
2424
from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping, Sequence
2525
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
@@ -412,8 +412,11 @@ In the future, this property will contain the last metadata change time."""
412412
# Attributes documented as sometimes appearing, but deliberately omitted from the stub: `st_creator`, `st_rsize`, `st_type`.
413413
# See https://github.com/python/typeshed/pull/6560#issuecomment-991253327
414414

415+
# mypy and pyright object to this being both ABC and Protocol.
416+
# At runtime it inherits from ABC and is not a Protocol, but it will be
417+
# on the allowlist for use as a Protocol starting in 3.14.
415418
@runtime_checkable
416-
class PathLike(Protocol[AnyStr_co]):
419+
class PathLike(ABC, Protocol[AnyStr_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
417420
@abstractmethod
418421
def __fspath__(self) -> AnyStr_co: ...
419422

stdlib/typing_extensions.pyi

+4-1
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,11 @@ else:
411411
def __or__(self, right: Any) -> _SpecialForm: ...
412412
def __ror__(self, left: Any) -> _SpecialForm: ...
413413

414+
# mypy and pyright object to this being both ABC and Protocol.
415+
# At runtime it inherits from ABC and is not a Protocol, but it is on the
416+
# allowlist for use as a Protocol.
414417
@runtime_checkable
415-
class Buffer(Protocol):
418+
class Buffer(Protocol, abc.ABC): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
416419
# Not actually a Protocol at runtime; see
417420
# https://github.com/python/typeshed/issues/10224 for why we're defining it this way
418421
def __buffer__(self, flags: int, /) -> memoryview: ...

0 commit comments

Comments
 (0)