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 @@ -13,6 +13,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
9 changes: 4 additions & 5 deletions src/anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool | None:
) -> bool:
del exc_tb

if not self._active:
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 Expand Up @@ -2448,7 +2447,7 @@ def create_capacity_limiter(cls, total_tokens: float) -> abc.CapacityLimiter:
return CapacityLimiter(total_tokens)

@classmethod
async def run_sync_in_worker_thread(
async def run_sync_in_worker_thread( # type: ignore[return]
cls,
func: Callable[[Unpack[PosArgsT]], T_Retval],
args: tuple[Unpack[PosArgsT]],
Expand All @@ -2470,7 +2469,7 @@ async def run_sync_in_worker_thread(

async with limiter or cls.current_default_thread_limiter():
with CancelScope(shield=not abandon_on_cancel) as scope:
future: asyncio.Future = asyncio.Future()
future = asyncio.Future[T_Retval]()
root_task = find_root_task()
if not idle_workers:
worker = WorkerThread(root_task, workers, idle_workers)
Expand Down
8 changes: 4 additions & 4 deletions src/anyio/_backends/_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool | None:
# https://github.com/python-trio/trio-typing/pull/79
) -> bool:
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
2 changes: 1 addition & 1 deletion src/anyio/_core/_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool | None:
) -> bool:
raise NotImplementedError


Expand Down
2 changes: 1 addition & 1 deletion src/anyio/to_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
_default_process_limiter: RunVar[CapacityLimiter] = RunVar("_default_process_limiter")


async def run_sync(
async def run_sync( # type: ignore[return]
func: Callable[[Unpack[PosArgsT]], T_Retval],
*args: Unpack[PosArgsT],
cancellable: bool = False,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,8 @@ async def task(task_status: TaskStatus) -> NoReturn:
completed = True
scope.shield = False
await sleep(1)
pytest.fail("Execution should not reach this point")

pytest.fail("Execution should not reach this point")

async with create_task_group() as tg:
await tg.start(task)
Expand Down
Loading