Skip to content

asyncio protocol factory types are too broad #6936

Closed
@drtyrsa

Description

@drtyrsa

Here's an example:

import asyncio


class MyProtocol(asyncio.Protocol):
    def hello(self) -> None:
        pass


async def f() -> None:
    _, protocol = await asyncio.get_event_loop().create_connection(MyProtocol)
    protocol.hello()

Mypy will say that error: "BaseProtocol" has no attribute "hello". That's because current stubs say that returned protocol type will be BaseProtocol. But we know for sure it will be the same as a type of the value returned by protocol factory (first argument).

It could be made like this:

_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
async def create_connection(self, protocol_factory: Callable[[], _ProtocolT], ...) -> tuple[BaseTransport, _ProtocolT]:

This way type checking will be more precise and there will be no need for casts. What to you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions