Skip to content

Rename asyncio.Timeout to be unlike asyncio.timeout #141401

Description

@xitop

Feature or enhancement

Proposal:

This renaming was proposed on Discourse as a solution for the annoyance that two very similar names exist and both can be used in the same statement without any error, because both are async context managers (CM):

async with asyncio.timeout(99): await ...  # timeout is using relative time as expected
async with asyncio.Timeout(99): await ...  # Timeout is using loop clock absolute time;
                                           # small values are already in the past,
                                           # so it cancels immediately

It is very easy to make an error and write the upper-case Timeout by mistake. For instance we already have the upper-case TaskGroup async CM.

Using timeout creates a Timeout object. Because Timeout is not intended to be used directly, giving it a new name and deprecating the old one was suggested in the linked Discourse thread.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/error-prone-naming-asyncio-timeout-vs-asyncio-timeout/104727

Activity

  1. hugovk commented on Nov 11, 2025

    @hugovk
    Member

    What do you propose as the new name?

    The old name would need deprecating.

  2. xitop commented on Nov 11, 2025

    @xitop
    Author

    I'm leaving the choice of a new name to Python developers and native speakers.

    But if we need something to start with, maybe a TimeoutManager.

  3. nineteendo commented on Nov 11, 2025

    @nineteendo
    Contributor

    Maybe TimeoutAt?

  4. mikeshardmind commented on Nov 13, 2025

    @mikeshardmind
    Contributor

    TimeoutAt isn't really correct for what this is. The absolute loop time in the initialization is only for the initial timeout, the main reason both exist, as well as another function that is a context manager with an absolute loop time, is that this form is reschedulable.

    TimeoutManager seems fine.

  5. sharktide commented on Nov 14, 2025

    @sharktide
    Contributor

    I agree with TimeoutManger

  6. picnixz commented on Nov 15, 2025

    @picnixz
    Member

    Using timeout creates a Timeout object. Because Timeout is not intended to be used directly, giving it a new name and deprecating the old one was suggested in the linked Discourse thread.

    It is documented: https://docs.python.org/3/library/asyncio-task.html#asyncio.Timeout. So yes it can be used directly and shouldn't be deprecated (if we want it to be deprecated, I would rather make it private and only indicate the methods that are present on the returned CM instead of exposing the name)

    For instance we already have the upper-case TaskGroup async CM.

    but do we have asyncio.taskGroup? or asyncio.Taskgroup? I don't think we should change this honestly. Sure, it's a bit annoying to make an error but this is not something to change IMO.

  7. mikeshardmind commented on Nov 15, 2025

    @mikeshardmind
    Contributor

    This does also strike me as something a linter could catch., which might be a more productive way to ensure fewer users make this mistake. If you use asyncio.Timeout with a literal timeout, rather than a computed value, it's pretty likely you meant asyncio.timeout.

  8. added
    pendingThe issue will be closed if no feedback is provided
    on Nov 15, 2025
  9. xitop commented on Nov 16, 2025

    @xitop
    Author

    If you use asyncio.Timeout with a literal timeout, rather than a computed value, it's pretty likely you meant asyncio.timeout.

    According to the documentation, the counterpart of asyncio.timeout that uses absolute time is asyncio.timeout_at, i.e. not the asyncio.Timeout. The asyncio.Timeout is introduced in the docs by the last paragraph of asyncio.timeout: The context manager produced by asyncio.timeout() can be rescheduled ....

  10. xitop commented on Nov 16, 2025

    @xitop
    Author

    but do we have asyncio.taskGroup?

    The TaskGroup example only shows that there is no consistency for asyncio context manager names. Some are CamelCase and some are lowercase. That increases odds of making a mistake.

  11. 3 remaining items

  12. picnixz commented on Nov 28, 2025

    @picnixz
    Member

    I personally would leave it as is. Deprecation is really a long and annoying process. What I suggested is not making the name private by the way. What I meant is to not expose it in the docs and only say that the CM return an object which has the "following methods".

    People would still be able to use it at runtime but the docs wouldn't mention it. It's unfortunate but this also means that anyone doing from asyncio import * should not be broken by the fact that we deprecate Timeout (it's exposed in timeout.py::__all__ and then later re-exported to asyncio).

    Now, what I do not want to see:

    • from asyncio import * raising DeprecationWarning because of a deprecated name. If we provide an __all__, then it was meant for it to be *-importable IMO.
    • from asyncio import Timeout or from asyncio.timeout import Timeout should still be entirely correct. I didn't think about it before but users that want to type their code may want to keep that class, whether it's its interface or its name. So even my proposal would need to be amended in favor of a simple docs emphasis about "prefer using the CM directly" rather than "do not use it".

    The best course of action would be to leave it to the linters, so that asyncio.Timeout is not used explicitly. So I'm willing to adding a recommendation in the docs, but leave the runtime as is.

  13. xitop commented on Nov 28, 2025

    @xitop
    Author

    @picnixz, I'm confused by your replies:

    "would rather make it private"

    "What I suggested is not making the name private by the way."

    Just editing the docs cannot solve the problem when somebody types a 'T' instead of the 't'.

    I understand that the question is if doing the renaming process is worth the effort. I don't know the answer.

  14. picnixz commented on Nov 28, 2025

    @picnixz
    Member

    About my reply: that's before I actually saw that it's part of the public API. I should have been clearer.

    Just editing the docs cannot solve the problem when somebody types a 'T' instead of the 't'.

    Yes but we cannot also help them more without penalizing others. I'm sorry, but the best we can do is simply change the docs so that the linters could take this as a sign that a new rule could be added.

    I understand that the question is if doing the renaming process is worth the effort. I don't know the answer.

    For me, it's a clear no. Deprecation requires at least 5 years. And as I said, it's not that trivial. It breaks code that would rely just on the class name for typing purposes and it would break code that does from asyncio import *. It's also breaking the contract about a public API. We don't want to make it deprecated and have everyone else getting warnings because of the change, so we likely need to make it so that the warning is raised at construction time. But we don't want that warning to be raised if the caller is asyncio.timeout[at].

    I will recategorize this issue as a doc issue. We can simply indicate in the docs that the interface is documented and can be used but users should instead use timeout and timeout_at. From a usage ratio, I think there are as many people making that typo than users using it, but there are likely more users doing from asyncio import * in their everyday's script (what is costly is the wrapper implementation just to avoid a warning when doing from asyncio import * or other solutions).

  15. added
    docsDocumentation in the Doc dir
    and removed
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    pendingThe issue will be closed if no feedback is provided
    on Nov 28, 2025
  16. picnixz commented on Nov 28, 2025

    @picnixz
    Member

    Or maybe better, let's close this issue as the discussion could be reopened and let's create a separate issue.

  17. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    and removed
    docsDocumentation in the Doc dir
    on Nov 28, 2025
  18. moved this from Todo to Done in asyncioon Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions