You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using python3.6 and calling time.sleep with an invalid argument the TypeError raised has the error message "TypeError: an integer is required". This is not the case as a float or integer is acceptable. Using python 2.7 the error message given is better "TypeError: a float is required".
mangrisano commented on May 29, 2019
mangrisanomannequin
Mannequin
More actions
The doc (3.7) says that the argument "may be a floating point number to indicate a more precise sleep time."
I think that TypeError message is right because you can choose how much precision you need but it's optional.
What do you think about that?
While not exactly the same, bpo-35707 is also about time.sleep and floats.
JustinFay commented on May 30, 2019
JustinFaymannequin
MannequinAuthor
More actions
From looking at the code for this (note I am not a C programmer so may have gotten this wrong) _PyTime_FromObject first checks if the object is a float using PyFloat_Check(obj) this is evident as passing nan to time.sleep raises a ValueError with the message "Invalid value NaN (not a number)". However if the object is not a float it next assumes the only valid value to be an integer, this logic appears fine to me. The problem however is if the object is not an integer the code raises the error with the message that an integer is required, it is unaware that a before this branch of the code executes a float would have been an accepted value. In python I imagine the fix for this would be along the lines
try:
float(obj)
except (TypeError, ValueError):
try:
int(obj):
except (TypeError, ValueError):
raise TypeError('an float or integer is required')
Activity
JustinFay commentedon May 29, 2019
Using python3.6 and calling
time.sleepwith an invalid argument theTypeErrorraised has the error message "TypeError: an integer is required". This is not the case as a float or integer is acceptable. Using python 2.7 the error message given is better "TypeError: a float is required".mangrisano commentedon May 29, 2019
The doc (3.7) says that the argument "may be a floating point number to indicate a more precise sleep time."
I think that TypeError message is right because you can choose how much precision you need but it's optional.
What do you think about that?
ericvsmith commentedon May 29, 2019
I think it's reasonable to change the TypeError to say integer or float. That's what _PyTime_FromObject is looking for.
csabella commentedon May 29, 2019
While not exactly the same, bpo-35707 is also about time.sleep and floats.
JustinFay commentedon May 30, 2019
From looking at the code for this (note I am not a C programmer so may have gotten this wrong) _PyTime_FromObject first checks if the object is a float using PyFloat_Check(obj) this is evident as passing nan to time.sleep raises a ValueError with the message "Invalid value NaN (not a number)". However if the object is not a float it next assumes the only valid value to be an integer, this logic appears fine to me. The problem however is if the object is not an integer the code raises the error with the message that an integer is required, it is unaware that a before this branch of the code executes a float would have been an accepted value. In python I imagine the fix for this would be along the lines
try:
float(obj)
except (TypeError, ValueError):
try:
int(obj):
except (TypeError, ValueError):
raise TypeError('an float or integer is required')
Windsooon commentedon Jun 3, 2019
I just create a PR for it.
gh-81267: Correct time.sleep() error message (#131055)