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

DefaultEventLoopPolicy is not valid as a type / base class #7452

Closed
henryiii opened this issue Mar 7, 2022 · 6 comments
Closed

DefaultEventLoopPolicy is not valid as a type / base class #7452

henryiii opened this issue Mar 7, 2022 · 6 comments

Comments

@henryiii
Copy link
Contributor

henryiii commented Mar 7, 2022

The following code fails mypy tmp.py:

import asyncio

class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
    pass

with:

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:

class _UnixDefaultEventLoopPolicy(BaseDefaultEventLoopPolicy):
def get_child_watcher(self) -> AbstractChildWatcher: ...
def set_child_watcher(self, watcher: AbstractChildWatcher | None) -> None: ...
SelectorEventLoop = _UnixSelectorEventLoop
DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy
?

@AlexWaygood
Copy link
Member

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-dir option, or by setting the MYPYPATH environment variable.

@srittau
Copy link
Collaborator

srittau commented Mar 7, 2022

@AlexWaygood was faster, but I agree with the analysis.

@henryiii
Copy link
Contributor Author

henryiii commented 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.

$ mypy tmp.py --custom-typeshed-dir ~/git/software/typeshed
Success: no issues found in 1 source file

@henryiii henryiii closed this as completed Mar 7, 2022
@srittau
Copy link
Collaborator

srittau commented Mar 7, 2022

Before, this line was in asyncio/__init__.pyi:

DefaultEventLoopPolicy: type[AbstractEventLoopPolicy]

This has been changed to a star import.

@AlexWaygood
Copy link
Member

Yes, now the DefaultEventLoopPolicy name is imported either from asyncio.unix_events or asyncio.windows_events, depending on whether the machine running mypy is a Windows or Unix machine. This mirrors the behavior at runtime when importing DefaultEventLoopPolicy from asyncio.

@henryiii
Copy link
Contributor Author

henryiii commented Mar 7, 2022

Ah, I missed that, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants