This rules raises an issue when trio.sleep() or anyio.sleep() is called with a duration greater than 24 hours.

Why is this an issue?

When using trio.sleep() or anyio.sleep() with very long intervals (greater than 24 hours), the intent is usually to wait indefinitely or for an extremely long period. Both libraries provide dedicated methods specifically designed for this purpose: trio.sleep_forever() and anyio.sleep_forever().

Using explicit sleep durations greater than 24 hours has several drawbacks:

In summary, using sleep_forever() is preferable when the intent is to sleep indefinitely as it clearly conveys this purpose, avoiding maintainability issues caused by using arbitrarily large sleep durations.

How to fix it in Trio

Replace calls to trio.sleep() that use intervals greater than 24 hours with calls to trio.sleep_forever().

If the intention is truly to wait for a specific long duration rather than indefinitely, consider expressing that intent more clearly by using named variables or constants.

Code examples

Noncompliant code example

import trio

async def long_wait():
    await trio.sleep(86400 * 365)  # Noncompliant

Compliant solution

import trio

async def long_wait():
    await trio.sleep_forever()

How to fix it in AnyIO

Replace calls to anyio.sleep() that use intervals greater than 24 hours with calls to anyio.sleep_forever().

If the intention is truly to wait for a specific long duration rather than indefinitely, consider expressing that intent more clearly by using named variables or constants.

Code examples

Noncompliant code example

import anyio

async def long_wait():
    await anyio.sleep(86400 * 30)  # Noncompliant

Compliant solution

import anyio

async def long_wait():
    await anyio.sleep_forever()

Resources

Documentation