Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio.sleep(float('nan')) does not raise ValueError #105331

Closed
jakkdl opened this issue Jun 5, 2023 · 5 comments
Closed

asyncio.sleep(float('nan')) does not raise ValueError #105331

jakkdl opened this issue Jun 5, 2023 · 5 comments
Labels
topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@jakkdl
Copy link

jakkdl commented Jun 5, 2023

Bug report

time.sleep(float('nan')) raises ValueError, but asyncio.sleep(float('nan')) is valid and sleeps for 0. This seems bad, and asyncio.sleep should probably also raise ValueError.

import asyncio
import time

try:
    time.sleep(float('nan'))
except ValueError:
    print("time.sleep raised ValueError")

async def foo() -> None:
    await asyncio.sleep(float('nan'))
    print("asyncio.sleep does not raise any error")

asyncio.run(foo())
$ python foo.py
time.sleep raised ValueError
asyncio.sleep does not raise any error

time.sleep and asyncio.sleep do differ in behaviour when it comes to negative numbers and changing that behaviour was considered a breaking change without strong reason: #83879
nan feels like it should very rare to be passed intentionally, but the potential gain might also be small enough that this isn't worth fixing.

Related: python-trio/trio#2493

Your environment

  • CPython versions tested on: 3.8 to 3.11
  • Operating system and architecture: Linux 6.3.4-arch1-1

Linked PRs

@jakkdl jakkdl added the type-bug An unexpected behavior, bug, or error label Jun 5, 2023
@sobolevn
Copy link
Member

sobolevn commented Jun 6, 2023

It is also different for negative values.

For example:

>>> import asyncio
>>> async def a():
...     await asyncio.sleep(-1)
... 
>>> asyncio.run(a())

and

>>> import time
>>> time.sleep(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: sleep length must be non-negative

@jakkdl
Copy link
Author

jakkdl commented Jun 6, 2023

It is also different for negative values.

Yes, I wrote

time.sleep and asyncio.sleep do differ in behaviour when it comes to negative numbers and changing that behaviour was considered a breaking change without strong reason: #83879

@kumaraditya303
Copy link
Contributor

I don't think we should change it at this point, I can accept a docs improvement for this but I'm -1 on changing the behavior.

@Zac-HD
Copy link
Contributor

Zac-HD commented Jun 9, 2023

fwiw I'd expect it to follow whatever time.sleep() does with weird arguments, and think that there's a pretty strong argument in favor of consistency.

@gvanrossum
Copy link
Member

Hm, I expect at this point there is probably plenty of working code out there that passes a negative delay to asyncio.sleep (perhaps the result of trying to sleep until some target time that's slightly in the past).

I don't see how NaN could be the result of such a computation, so I agree NaN is a "weird" argument (and leads to undefined behavior).

So I think I can only approve of the NaN check.

weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 11, 2023
weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 11, 2023
weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 11, 2023
weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 12, 2023
weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 12, 2023
weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 12, 2023
weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 12, 2023
weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 12, 2023
weijay0804 added a commit to weijay0804/cpython that referenced this issue Jun 12, 2023
kumaraditya303 added a commit that referenced this issue Jun 12, 2023
…5641)

Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
carljm added a commit to carljm/cpython that referenced this issue Jun 12, 2023
* main:
  pythongh-105540: Fix code generator tests (python#105707)
  pythongh-105375: Explicitly initialise all {Pickler,Unpickler}Object fields (python#105686)
  pythongh-105331: Change `asyncio.sleep` to raise ``ValueError` for nan (python#105641)
  Remove support for legacy bytecode instructions (python#105705)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

6 participants