Closed
Description
The following code fails mypy tmp.py:
import asyncio
class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
passwith:
tmp.py:3: error: Variable "asyncio.DefaultEventLoopPolicy" is not valid as a type
tmp.py:3: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
tmp.py:3: error: Invalid base class "asyncio.DefaultEventLoopPolicy"
Found 2 errors in 1 file (checked 1 source file)
This MWE is copied from the Python docs here: https://docs.python.org/3/library/asyncio-policy.html#custom-policies.
Maybe this is because of the assignment here:
typeshed/stdlib/asyncio/unix_events.pyi
Lines 94 to 99 in fd750bf
| class _UnixDefaultEventLoopPolicy(BaseDefaultEventLoopPolicy): | |
| def get_child_watcher(self) -> AbstractChildWatcher: ... | |
| def set_child_watcher(self, watcher: AbstractChildWatcher | None) -> None: ... | |
| SelectorEventLoop = _UnixSelectorEventLoop | |
| DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy |
Metadata
Metadata
Assignees
Labels
No labels
Activity
AlexWaygood commentedon Mar 7, 2022
The bug appears to already be fixed on typeshed master as a result of #7343 (merged just seven days ago) 😃
mypy's bundled version of the typeshed stubs won't be updated until at least 0.940. For now, though, you can clone typeshed and use mypy with the
--custom-typeshed-diroption, or by setting theMYPYPATHenvironment variable.srittau commentedon Mar 7, 2022
@AlexWaygood was faster, but I agree with the analysis.
henryiii commentedon Mar 7, 2022
That's fantastic. While I don't fully understand why this fixed the problem, I can verify that does in fact now pass.
srittau commentedon Mar 7, 2022
Before, this line was in
asyncio/__init__.pyi:typeshed/stdlib/asyncio/__init__.pyi
Line 113 in 35bddd2
This has been changed to a star import.
AlexWaygood commentedon Mar 7, 2022
Yes, now the
DefaultEventLoopPolicyname is imported either fromasyncio.unix_eventsorasyncio.windows_events, depending on whether the machine running mypy is a Windows or Unix machine. This mirrors the behavior at runtime when importingDefaultEventLoopPolicyfromasyncio.henryiii commentedon Mar 7, 2022
Ah, I missed that, thanks!