Skip to content

BaseEventLoop.create_server overload doesn't allow passing host with port=None #16157

Description

@timabroad

The type stubs for BaseEventLoop.create_server (here) consist over two overloads:

# The first one specifies:
host: str | Sequence[str] | None = None,
port: int = ...,
sock: None = None,

# The second one specifies:
host: None = None,
port: None = None,
sock: socket = ...,

But the first of these causes a typing error if you pass None for the port (pyright playground), despite succeeding at runtime:

import asyncio

async def start():
    loop = asyncio.get_running_loop()

    await loop.create_server(asyncio.Protocol)
    await loop.create_server(asyncio.Protocol, 'localhost')
    await loop.create_server(asyncio.Protocol, 'localhost', 8080)
    await loop.create_server(asyncio.Protocol, 'localhost')
    await loop.create_server(asyncio.Protocol, 'localhost', None) # Argument of type "None" cannot be assigned to parameter "port" of type "int" in function "create_server".

This is documented as being valid as far back as Python 3.9. For some reason the 3.8 and earlier docs don't provide any details for the port parameter, though I'm not aware of any changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedAn actionable problem of low to medium complexity where a PR would be very welcomestubs: false positiveType checkers report false errorstopic: asyncioAsyncio-related issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions