Skip to content

Fix the signatures of @(async)contextmanager #12087

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
8d59204
Use overloads to deprecate the less specific signature, less accurate…
Sachaa-Thanasius Jun 2, 2024
37638ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 2, 2024
d68ad23
Add asynccontextmanager too, why not. It has the same issue.
Sachaa-Thanasius Jun 2, 2024
6a357cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 2, 2024
91ff757
Merge branch 'main' into contexmanager/deprecate-iterator-signature
Sachaa-Thanasius Jun 3, 2024
35f10aa
Update phrasing of `@deprecated` messages based on feedback.
Sachaa-Thanasius Jun 3, 2024
5045680
Merge branch 'main' into contexmanager/deprecate-iterator-signature
Sachaa-Thanasius Aug 2, 2024
da47550
Merge branch 'main' into contexmanager/deprecate-iterator-signature
Sachaa-Thanasius Aug 8, 2024
fba4b4f
Merge branch 'main' into contexmanager/deprecate-iterator-signature
Sachaa-Thanasius Sep 2, 2024
840b918
Merge branch 'main' into contexmanager/deprecate-iterator-signature
srittau Oct 21, 2024
338f605
Merge branch 'main' into contexmanager/deprecate-iterator-signature
Sachaa-Thanasius Apr 24, 2025
799f4d2
Be more lenient with the generator's return type, since contextmanage…
Sachaa-Thanasius Apr 24, 2025
a683c99
Fix line length.
Sachaa-Thanasius Apr 24, 2025
b1087ef
Temporarily change primer workflow to emit deprecations as notes
Sachaa-Thanasius Apr 24, 2025
f9f4397
Ensure the deprecations are actually emitted in some fashion
Sachaa-Thanasius Apr 24, 2025
e47ba50
Make deprecations errors, just in case primer didn't pick up notes so…
Sachaa-Thanasius Apr 24, 2025
3262047
Merge branch 'main' into contexmanager/deprecate-iterator-signature
Sachaa-Thanasius Apr 24, 2025
c1cc48a
Try the deprecated flag again with slightly different syntax.
Sachaa-Thanasius Apr 24, 2025
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
1 change: 1 addition & 0 deletions .github/workflows/mypy_primer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
--num-shards 4 --shard-index ${{ matrix.shard-index }} \
--debug \
--output concise \
--additional-flags="--enable-error-code deprecated" \
| tee diff_${{ matrix.shard-index }}.txt
) || [ $? -eq 1 ]
- if: ${{ matrix.shard-index == 0 }}
Expand Down
15 changes: 14 additions & 1 deletion stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from abc import ABC, abstractmethod
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
from types import TracebackType
from typing import IO, Any, Generic, Protocol, TypeVar, overload, runtime_checkable
from typing_extensions import ParamSpec, Self, TypeAlias
from typing_extensions import ParamSpec, Self, TypeAlias, deprecated

__all__ = [
"contextmanager",
Expand Down Expand Up @@ -85,6 +85,12 @@ class _GeneratorContextManager(
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> bool | None: ...

@overload
def contextmanager(func: Callable[_P, Generator[_T_co, None, object]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ...
@overload
@deprecated(
"Annotating the return type as `-> Iterator[Foo]` with `@contextmanager` is deprecated. Use `-> Generator[Foo]` instead."
)
def contextmanager(func: Callable[_P, Iterator[_T_co]]) -> Callable[_P, _GeneratorContextManager[_T_co]]: ...

if sys.version_info >= (3, 10):
Expand All @@ -111,6 +117,13 @@ else:
self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> bool | None: ...

@overload
def asynccontextmanager(func: Callable[_P, AsyncGenerator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...
@overload
@deprecated(
"Annotating the return type as `-> AsyncIterator[Foo]` with `@contextmanager` is deprecated. "
"Use `-> AsyncGenerator[Foo]` instead."
)
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...

class _SupportsClose(Protocol):
Expand Down