Skip to content

Use more precise return types in asyncio methods #6937

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 1 commit into from
Jan 17, 2022

Conversation

drtyrsa
Copy link
Contributor

@drtyrsa drtyrsa commented Jan 17, 2022

No description provided.

@github-actions
Copy link
Contributor

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

websockets (https://github.com/aaugustin/websockets)
+ src/websockets/legacy/client.py:641: error: "_ProtocolT" has no attribute "close"
+ src/websockets/legacy/client.py:655: error: Incompatible types in assignment (expression has type "WebSocketClientProtocol", variable has type "_ProtocolT")
+ src/websockets/legacy/client.py:659: error: "_ProtocolT" has no attribute "handshake"
+ src/websockets/legacy/client.py:661: error: "_ProtocolT" has no attribute "origin"
+ src/websockets/legacy/client.py:662: error: "_ProtocolT" has no attribute "available_extensions"
+ src/websockets/legacy/client.py:663: error: "_ProtocolT" has no attribute "available_subprotocols"
+ src/websockets/legacy/client.py:664: error: "_ProtocolT" has no attribute "extra_headers"
+ src/websockets/legacy/client.py:667: error: "_ProtocolT" has no attribute "fail_connection"
+ src/websockets/legacy/client.py:668: error: "_ProtocolT" has no attribute "wait_closed"
+ src/websockets/legacy/client.py:672: error: Incompatible return value type (got "_ProtocolT", expected "WebSocketClientProtocol")

anyio (https://github.com/agronholm/anyio)
+ src/anyio/_backends/_asyncio.py:1365: error: Redundant cast to "StreamProtocol"  [redundant-cast]
+ src/anyio/_backends/_asyncio.py:1555: error: Redundant cast to "DatagramProtocol"  [redundant-cast]

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/connector.py:1295: error: Redundant cast to "ResponseHandler"  [redundant-cast]

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.

LGTM, the anyio and aiohttp primer output looks promising. websockets is doing some complicated stuff when creating protocols, so I think they'd just need to update their annotations. I'd like a second opinion, though.

@Akuli
Copy link
Collaborator

Akuli commented Jan 17, 2022

websockets does this:

        if kwargs.pop("unix", False):
            ...
            create_connection = functools.partial(
                loop.create_unix_connection, factory, path, **kwargs
            )
        else:
            ...
            create_connection = functools.partial(
                loop.create_connection, factory, host, port, **kwargs
            )
        ...
        self._create_connection = create_connection

Code like this isn't very typing friendly, and I don't think there's a good way for us to support it anyway. Websockets already has casts.

@Akuli Akuli merged commit e558fed into python:master Jan 17, 2022
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