From 2ede7d73e55f8d1a2279d78861af0009d96219fb Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 19 Dec 2021 14:26:44 -0800 Subject: [PATCH] Fix annotations on `__exit__` and `__aexit__` (#274) * Fix annotations on `__exit__` and `__aexit__` All parameters should be Optional, because they're None when no exception is thrown. * changelog * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- CHANGES/274.misc | 1 + async_timeout/__init__.py | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) create mode 100644 CHANGES/274.misc diff --git a/CHANGES/274.misc b/CHANGES/274.misc new file mode 100644 index 0000000..4e2e6db --- /dev/null +++ b/CHANGES/274.misc @@ -0,0 +1 @@ +Improve type annotations for `__exit__` and `__aexit__` methods. diff --git a/async_timeout/__init__.py b/async_timeout/__init__.py index e5b2caa..26c50c2 100644 --- a/async_timeout/__init__.py +++ b/async_timeout/__init__.py @@ -109,9 +109,9 @@ def __enter__(self) -> "Timeout": def __exit__( self, - exc_type: Type[BaseException], - exc_val: BaseException, - exc_tb: TracebackType, + exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType], ) -> Optional[bool]: self._do_exit(exc_type) return None @@ -122,9 +122,9 @@ async def __aenter__(self) -> "Timeout": async def __aexit__( self, - exc_type: Type[BaseException], - exc_val: BaseException, - exc_tb: TracebackType, + exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType], ) -> Optional[bool]: self._do_exit(exc_type) return None @@ -206,7 +206,7 @@ def _do_enter(self) -> None: self._state = _State.ENTER self._reschedule() - def _do_exit(self, exc_type: Type[BaseException]) -> None: + def _do_exit(self, exc_type: Optional[Type[BaseException]]) -> None: if exc_type is asyncio.CancelledError and self._state == _State.TIMEOUT: self._timeout_handler = None raise asyncio.TimeoutError