Skip to content

Incorrect type annotation for connect_robust return value #683

@hasansezertasan

Description

@hasansezertasan

Incorrect type annotation for connect_robust return value

Bug Description

The return type annotation for aio_pika.connect_robust is currently AbstractRobustConnection, but in practice, when passing connection_class=aio_pika.RobustConnection, the returned object is an instance of RobustConnection. This causes a type mismatch error in static type checkers like mypy when annotating the result as aio_pika.RobustConnection, even though the runtime type is correct. The type signature of connect_robust does not reflect the actual return type when a specific connection_class is provided, leading to false-positive type errors.

How to Reproduce

I prepared a minimal reproducible example to demonstrate the issue.

# /// script
# requires-python = ">=3.10"
# dependencies = ["aio-pika"]
# ///
import asyncio

import aio_pika


async def loop() -> None:
    connection: aio_pika.RobustConnection = await aio_pika.connect_robust(
        "amqp://guest:[email protected]/",
        connection_class=aio_pika.RobustConnection,
    )
    print(type(connection))  # <class 'aio_pika.robust_connection.RobustConnection'>


def main() -> None:
    asyncio.run(loop())


if __name__ == "__main__":
    main()
> uvx mypy main.py
main.py:11: error: Incompatible types in assignment (expression has type "AbstractRobustConnection", variable has type "RobustConnection")  [assignment]
        connection: aio_pika.RobustConnection = await aio_pika.connect_robust(
                                                ^
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I expect the connection variable to have aio_pika.RobustConnection type.

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