Skip to content
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

Fix __exit__ return type of various context managers #849

Merged
merged 11 commits into from
Jan 2, 2025
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
(`#836 <https://github.com/agronholm/anyio/pull/836>`_; PR by @graingert)
- Fixed ``AssertionError`` when using ``nest-asyncio``
(`#840 <https://github.com/agronholm/anyio/issues/840>`_)
- Fixed return type annotation of various context managers' ``__exit__`` method
(`#847 <https://github.com/agronholm/anyio/issues/847>`_; PR by @Enegg)

**4.7.0**

Expand Down
5 changes: 2 additions & 3 deletions src/anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def __exit__(
# scope if necessary
self._restart_cancellation_in_parent()

# We only swallow the exception iff it was an AnyIO CancelledError, either
# We only swallow the exception if it was an AnyIO CancelledError, either
Enegg marked this conversation as resolved.
Show resolved Hide resolved
# directly as exc_val or inside an exception group and there are no cancelled
# parent cancel scopes visible to us here
if self._cancel_called and not self._parent_cancellation_is_visible_to_us:
Expand Down Expand Up @@ -2116,10 +2116,9 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool | None:
) -> None:
for sig in self._handled_signals:
self._loop.remove_signal_handler(sig)
return None

def __aiter__(self) -> _SignalReceiver:
return self
Expand Down
6 changes: 3 additions & 3 deletions src/anyio/_backends/_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def __exit__(
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool | None:
# https://github.com/python-trio/trio-typing/pull/79
return self.__original.__exit__(exc_type, exc_val, exc_tb)

def cancel(self) -> None:
Expand Down Expand Up @@ -186,9 +185,10 @@ async def __aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool | None:
) -> bool:
try:
return await self._nursery_manager.__aexit__(exc_type, exc_val, exc_tb)
# trio.Nursery.__exit__ returns bool; .open_nursery has wrong type
return await self._nursery_manager.__aexit__(exc_type, exc_val, exc_tb) # type: ignore[return-value]
except BaseExceptionGroup as exc:
if not exc.split(trio.Cancelled)[1]:
raise trio.Cancelled._create() from exc
Expand Down
3 changes: 1 addition & 2 deletions src/anyio/_core/_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,5 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool | None:
) -> None:
self._guarded = False
return None
Loading