Skip to content

Abstract(Async)ContextManager.__enter__ return type defaults to Self #13951

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 10 additions & 1 deletion stdlib/@tests/test_cases/check_contextlib.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from __future__ import annotations

from contextlib import ExitStack
from contextlib import AbstractContextManager, ExitStack
from typing_extensions import assert_type


class CM1(AbstractContextManager):
def __exit__(self, *args) -> None:
return None


with CM1() as cm1:
assert_type(cm1, CM1)


# See issue #7961
class Thing(ExitStack):
pass
Expand Down
9 changes: 5 additions & 4 deletions stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
_T_io = TypeVar("_T_io", bound=IO[str] | None)
_EnterT_co = TypeVar("_EnterT_co", covariant=True, default=Self)

Check failure on line 34 in stdlib/contextlib.pyi

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"Self" is not valid in this context (reportGeneralTypeIssues)
_ExitT_co = TypeVar("_ExitT_co", covariant=True, bound=bool | None, default=bool | None)
_F = TypeVar("_F", bound=Callable[..., Any])
_G_co = TypeVar("_G_co", bound=Generator[Any, Any, Any] | AsyncGenerator[Any, Any], covariant=True)
Expand All @@ -46,8 +47,8 @@
# At runtime it inherits from ABC and is not a Protocol, but it is on the
# allowlist for use as a Protocol.
@runtime_checkable
class AbstractContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
def __enter__(self) -> _T_co: ...
class AbstractContextManager(ABC, Protocol[_EnterT_co, _ExitT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
def __enter__(self) -> _EnterT_co: ...
@abstractmethod
def __exit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, /
Expand All @@ -57,8 +58,8 @@
# At runtime it inherits from ABC and is not a Protocol, but it is on the
# allowlist for use as a Protocol.
@runtime_checkable
class AbstractAsyncContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
async def __aenter__(self) -> _T_co: ...
class AbstractAsyncContextManager(ABC, Protocol[_EnterT_co, _ExitT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
async def __aenter__(self) -> _EnterT_co: ...
@abstractmethod
async def __aexit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None, /
Expand Down
Loading