Skip to content

time.sleep error message misleading #81267

Closed

Description

@JustinFay
mannequin
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

    Activity

    1. JustinFay commented on May 29, 2019

      JustinFaymannequin
      MannequinAuthor

      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".

    2. mangrisano commented on May 29, 2019

      mangrisanomannequin
      Mannequin

      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?

    3. ericvsmith commented on May 29, 2019

      @ericvsmith
      Member

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

    4. csabella commented on May 29, 2019

      @csabella
      Contributor

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

    5. JustinFay commented on May 30, 2019

      JustinFaymannequin
      MannequinAuthor

      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')

    6. Windsooon commented on Jun 3, 2019

      Windsooonmannequin
      Mannequin

      I just create a PR for it.

    7. transferred this issue fromon Apr 10, 2022
    8. added a commit that references this issue on Mar 12, 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

      interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions