agronholm / apscheduler

Task scheduling library for Python
MIT License
5.98k stars 694 forks source link

`__{a}exit__` typing #874

Closed peterschutt closed 4 months ago

peterschutt commented 4 months ago

Things to check first

Version

4.0.0a4

What happened?

Passing an AsyncScheduler instance to AsyncExitStack.enter_async_context results in:

src/infra/__init__.py:61: error: Argument 1 to "enter_async_context" of "AsyncExitStack" has incompatible type "AsyncScheduler"; expected "AbstractAsyncContextManager[AsyncScheduler]"  [arg-type]
src/infra/__init__.py:61: note: Following member(s) of "AsyncScheduler" have conflicts:
src/infra/__init__.py:61: note:     Expected:
src/infra/__init__.py:61: note:         def __aexit__(self, type[BaseException] | None, BaseException | None, TracebackType | None, /) -> Coroutine[Any, Any, bool | None]
src/infra/__init__.py:61: note:     Got:
src/infra/__init__.py:61: note:         def __aexit__(self, type[BaseException], BaseException, TracebackType, /) -> Coroutine[Any, Any, None]

To conform to the ABC the args received by the exit method must be union with None.

How can we reproduce the bug?

    async with AsyncExitStack() as stack:
        scheduler = AsyncScheduler()
        scheduler = await stack.enter_async_context(scheduler)