Skip to content

Commit dc3ae1c

Browse files
make the type checker happy
1 parent d803f52 commit dc3ae1c

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

asyncstdlib/contextlib.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,15 @@ def push(self, exit: SE) -> SE:
328328
If a context manager must also be entered, use :py:meth:`~.enter_context`
329329
instead.
330330
"""
331-
try:
331+
aexit: Callable[..., Awaitable[Union[None, bool]]]
332+
if hasattr(exit, "__aexit__"):
332333
aexit = exit.__aexit__ # type: ignore
333-
except AttributeError:
334-
try:
335-
aexit = awaitify(
336-
exit.__exit__, # type: ignore
337-
)
338-
except AttributeError:
339-
assert callable(
340-
exit
341-
), f"Expected (async) context manager or callable, got {exit}"
342-
aexit = awaitify(exit)
334+
elif hasattr(exit, "__exit__"):
335+
aexit = exit.__aexit__ # type: ignore
336+
elif callable(exit):
337+
aexit = awaitify(exit) # type: ignore
338+
else:
339+
raise TypeError(f"Expected (async) context manager or callable, got {exit}")
343340
self._exit_callbacks.append(aexit) # pyright: ignore[reportUnknownArgumentType]
344341
return exit
345342

0 commit comments

Comments
 (0)