Skip to content

Help isinstance() recognize _SelectorDatagramTransport as subtype of asyncio.DatagramProtocol #8051

Description

@haxtibal

Consider a custom datagram-only asyncio protocol:

import asyncio

class MyProtocol(asyncio.DatagramProtocol):
    def connection_made(self, transport: asyncio.BaseTransport):  # signature as per typeshed super class
        if not isinstance(transport, asyncio.DatagramTransport):
            raise TypeError("%s not supported because it's not a datagram transport." % type(transport))
        # ...

For UDP connections on Linux, the above code results in

TypeError: <class 'asyncio.selector_events._SelectorDatagramTransport'> not supported because it's not a datagram transport.

Per implementation, class _SelectorDatagramTransport is a structural subtype of class DatagramTransport, but it does not actually inherit. However, from a users perspective I'd say both static and runtime type checks should succeed.

I'm unsure if the required change would have to go to cpython (let _SelectorDatagramTransport inherit DatagramTransport) or to typeshed (define BaseTransport and DatagramTransport as typing.Protocol with @runtime_checkable). What would you say?

If you think this is a valid request, I could try a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions