Skip to content

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

@haxtibal

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.

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy