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

Use PEP-646 in stubs for asyncio #11015

Merged
merged 10 commits into from
Dec 8, 2023
Merged

Conversation

AlexWaygood
Copy link
Member

As with #11014, I think this will crash pytype for now -- but I'm curious to see the mypy_primer output

@AlexWaygood AlexWaygood added the status: deferred Issue or PR deferred until some precondition is fixed label Nov 10, 2023

This comment has been minimized.

This comment has been minimized.

@AlexWaygood
Copy link
Member Author

mypy_primer analysis

tornado

They're subclassing AbstractEventLoop; they'll just have to update their annotations on their subclass, unfortunately.

websockets

This is arguably a true positive: they're passing a function that expects an asyncio.Task as the first argument to call_soon(), then they're passing the result of asyncio.current_task() as the second object: https://github.com/python-websockets/websockets/blob/ec3bd2ab06278602c1d6018b476699e090036373/src/websockets/legacy/async_timeout.py#L237-L241

According to our stubs, asyncio.current_task() could return None, so mypy is right to complain here:

def current_task(loop: AbstractEventLoop | None = None) -> Task[Any] | None: ...

black

I think their annotations are slightly incorrect: see psf/black#4047

discord

I think their annotations are definitely incorrect. The code in question is here: https://github.com/Rapptz/discord.py/blob/a5d03d4a1e6f4314518f6f55d8b7130ab3f4336e/discord/ext/tasks/__init__.py#L107-L119. self.future is annotated as being an asyncio.Future[None], which implies that you should only ever call self.future.set_result(None) with None, due to our annotations for asyncio.Future.set_result(). But they're passing (indirectly, via loop.call_later) self.future.set_result(True) on lines 114 and 119.

@AlexWaygood AlexWaygood marked this pull request as ready for review November 14, 2023 15:49
@srittau
Copy link
Collaborator

srittau commented Nov 17, 2023

@AlexWaygood You marked this as ready for review, but it's still tagged as "deferred". Is the latter an oversight?

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Nov 17, 2023

@AlexWaygood You marked this as ready for review, but it's still tagged as "deferred". Is the latter an oversight?

It can't be merged until pytype no longer crashes whenever we use Unpack, so in that sense it is indeed deferred. But I think the PR itself is in good condition, so there's nothing stopping you from reviewing it if you'd like to :-)

I merged main in just now because psf/black#4047 was just merged, and I wanted to double-check that it got rid of the black mypy_primer hit

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I really like the additional type safety that Unpack brings us. One complaint, though.

stdlib/asyncio/events.pyi Outdated Show resolved Hide resolved

This comment has been minimized.

This comment has been minimized.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Dec 8, 2023

Oh, I thought #11120 would unblock this and #11014, but looks like I got mixed up :)

google/pytype@bbf10c1 didn't quite make it into pytype==2023.12.7 -- it'll be included in the next pytype release.

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Dec 8, 2023

Diff from mypy_primer, showing the effect of this PR on open source code:

tornado (https://github.com/tornadoweb/tornado)
+ tornado/platform/asyncio.py:705: error: Argument 3 of "add_reader" is incompatible with supertype "AbstractEventLoop"; supertype defines the argument type as "Unpack[_Ts]"  [override]
+ tornado/platform/asyncio.py:705: note: This violates the Liskov substitution principle
+ tornado/platform/asyncio.py:705: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
+ tornado/platform/asyncio.py:710: error: Argument 3 of "add_writer" is incompatible with supertype "AbstractEventLoop"; supertype defines the argument type as "Unpack[_Ts]"  [override]
+ tornado/platform/asyncio.py:710: note: This violates the Liskov substitution principle
+ tornado/platform/asyncio.py:710: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

websockets (https://github.com/aaugustin/websockets)
+ src/websockets/legacy/async_timeout.py:239: error: Argument 1 to "call_soon" of "AbstractEventLoop" has incompatible type "Callable[[Task[None]], None]"; expected "Callable[[Task[Any] | None], object]"  [arg-type]
+ src/websockets/legacy/async_timeout.py:241: error: Argument 2 to "call_at" of "AbstractEventLoop" has incompatible type "Callable[[Task[None]], None]"; expected "Callable[[Task[Any] | None], object]"  [arg-type]

discord.py (https://github.com/Rapptz/discord.py)
+ discord/ext/tasks/__init__.py:114: error: Argument 2 to "call_later" of "AbstractEventLoop" has incompatible type "Callable[[None], None]"; expected "Callable[[bool], object]"  [arg-type]
+ discord/ext/tasks/__init__.py:119: error: Argument 2 to "call_later" of "AbstractEventLoop" has incompatible type "Callable[[None], None]"; expected "Callable[[bool], object]"  [arg-type]

@AlexWaygood
Copy link
Member Author

And we're there!

@AlexWaygood AlexWaygood merged commit a08d2d5 into python:main Dec 8, 2023
60 checks passed
@AlexWaygood AlexWaygood deleted the asyncio-646 branch December 8, 2023 22:59
@AlexWaygood AlexWaygood removed the status: deferred Issue or PR deferred until some precondition is fixed label Dec 8, 2023
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.

None yet

3 participants