Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove unneeded Iterator base class from asyncio.Future #12827

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

Merged
merged 5 commits into from
Oct 17, 2024

Conversation

tungol
Copy link
Contributor

@tungol tungol commented Oct 16, 2024

Taking another look, Awaitable is typed as Awaitable[_T_co] implies __await__(self) -> Generator[Any, Any, _T_co]: .... This is not quite an exact match for the stubs for asyncio.Future, which don't specify the covariant part, just def __await__(self) -> Generator[Any, None, _T]: .... I'm interested to see if that matters to mypy-primer or not.

@tungol tungol marked this pull request as draft October 16, 2024 19:56

This comment has been minimized.

@tungol tungol changed the title remove Awaitable base class from asyncio.Future Experimenting with asyncio.Future inheritance Oct 16, 2024

This comment has been minimized.

@tungol tungol closed this Oct 16, 2024
@tungol tungol reopened this Oct 16, 2024
@tungol tungol closed this Oct 16, 2024

This comment has been minimized.

@tungol tungol changed the title Experimenting with asyncio.Future inheritance Remove unneeded Iterator base class from asyncio.Future Oct 16, 2024
@tungol tungol reopened this Oct 16, 2024
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@tungol
Copy link
Contributor Author

tungol commented Oct 16, 2024

A bit of churn above. It seems that Awaitable is required as a base class because it allows asyncio.Future to sometimes behave covariantly even though it's not typed as covariant itself.

I tested locally and could see that the mypy-primer results cleared up if the class was Future(Generic[_T_co]), so that definitely seems like the cause of the blow-up seen in #12818. With no way to make Future covariant directly, I believe Awaitable needs to stay.

Is it normal to have this kind of indirectly-covariant typing? I was surprised to find that it works that way, but I don't have a deep understanding of variance.

@tungol tungol marked this pull request as ready for review October 16, 2024 21:04
@hauntsaninja
Copy link
Collaborator

Yes, IIUC you would break the transitivity of subtyping. See also:

class A: ...
class B(A): ...
from typing import Sequence
def takes_seq_a(x: Sequence[A]): ...
def main(x: list[B]):
    takes_seq_a(x)

asyncio.Future is mutable, so cannot be covariant: python/mypy#13689 (comment)

(and if you want to break your brain a little bit, check out #8781 , which surfaced unsoundness and was patched in both pyright and mypy, see python/mypy#13714)

@JelleZijlstra JelleZijlstra merged commit 2412c7c into python:main Oct 17, 2024
62 checks passed
@tungol tungol deleted the future branch October 18, 2024 16:34
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

Successfully merging this pull request may close these issues.

3 participants