Skip to content

time.sleep error message misleading #81267

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

Closed
JustinFay mannequin opened this issue May 29, 2019 · 6 comments
Closed

time.sleep error message misleading #81267

JustinFay mannequin opened this issue May 29, 2019 · 6 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@JustinFay
Copy link
Mannequin

JustinFay mannequin commented May 29, 2019

BPO 37086
Nosy @ericvsmith, @csabella, @Windsooon
PRs
  • bpo-37086: fixed time.sleep error message #13768
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-05-29.10:37:02.861>
    labels = []
    title = 'time.sleep error message misleading'
    updated_at = <Date 2019-06-03.03:43:22.478>
    user = 'https://bugs.python.org/JustinFay'

    bugs.python.org fields:

    activity = <Date 2019-06-03.03:43:22.478>
    actor = 'Windson Yang'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2019-05-29.10:37:02.861>
    creator = 'Justin Fay'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37086
    keywords = ['patch']
    message_count = 6.0
    messages = ['343880', '343894', '343904', '343909', '343958', '344374']
    nosy_count = 4.0
    nosy_names = ['eric.smith', 'cheryl.sabella', 'Windson Yang', 'Justin Fay']
    pr_nums = ['13768']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue37086'
    versions = ['Python 3.6']

    Linked PRs

    @JustinFay
    Copy link
    Mannequin Author

    JustinFay mannequin commented May 29, 2019

    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
    Copy link
    Mannequin

    mangrisano mannequin commented 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
    Copy link
    Member

    I think it's reasonable to change the TypeError to say integer or float. That's what _PyTime_FromObject is looking for.

    @csabella
    Copy link
    Contributor

    While not exactly the same, bpo-35707 is also about time.sleep and floats.

    @JustinFay
    Copy link
    Mannequin Author

    JustinFay mannequin commented 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
    Copy link
    Mannequin

    Windsooon mannequin commented Jun 3, 2019

    I just create a PR for it.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @AlexWaygood AlexWaygood added the type-bug An unexpected behavior, bug, or error label Apr 10, 2022
    @iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 23, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    Archived in project
    Development

    No branches or pull requests

    5 participants