Skip to content

Impossible to identify asyncio.TimeoutError from other TimeoutError #124308

@ArtsiomAntropau

Description

@ArtsiomAntropau

Feature or enhancement

Proposal:

Abstract

Sometimes there might be a case when a block inside async with asyncio.timeout(10) can raise TimeoutError and there are no proper way to identify one TimeoutError from another.

async with asyncio.timeout(10):
    if ...:
        raise TimeoutError

then here engineers will not be able to identify from where TimeoutError was received

Simplest example

import asyncio


async def main():
    try:
        async with asyncio.timeout(1):
            raise TimeoutError
    except asyncio.TimeoutError:  # will catch both, but should only exception from asyncio.timeout
        print('catched asyncio.TimeoutError')

asyncio.run(main())

More complex example

try:
    async with asyncio.timeout(10):
        if ...:
            raise TimeoutError
except TimeoutError:  # will catch both
    pass
except asyncio.TimeoutError:  # will catch both, but should only exception from asyncio.timeout
    pass

Proposal

asyncio should raise asyncio.TimeoutError from asyncio.exceptions and use some kind of inheritance, e.g.

class TimeoutError(builtins.TimeoutError):
    pass

then engineers will be able to use native TimeoutError from builtins as well as asyncio.TimeoutError for their try/except or isinstance checks.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions