python-trio / trio-typing

Type hints for Trio and related projects
Other
27 stars 16 forks source link

Make Listener covariant w.r.t. the resource #64

Closed VincentVanlaer closed 1 year ago

VincentVanlaer commented 2 years ago

This allows Listener[SubTypeOfStream] to be a subtype of Listener[Stream]. In this way the following function typechecks

async def open_listener(
  port: int,
  should_use_ssl: bool
) -> Sequence[Listener[Stream]]:
  if should_use_ssl:
    return await open_ssl_over_tcp_listeners(port, ssl.create_default_context())
  else:
    return await open_tcp_listeners(port)