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

Confusing error message when dividing timedelta using / #45424

Closed
smontanaro opened this issue Sep 1, 2007 · 10 comments
Closed

Confusing error message when dividing timedelta using / #45424

smontanaro opened this issue Sep 1, 2007 · 10 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@smontanaro
Copy link
Contributor

BPO 1083
Nosy @brettcannon, @tebeka, @mdickinson, @abalkin, @vstinner
Superseder
  • bpo-1289118: timedelta multiply and divide by floating point
  • Files
  • py3k_datetime_1083.patch
  • 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 = <Date 2010-06-06.01:25:31.693>
    created_at = <Date 2007-09-01.19:19:34.033>
    labels = ['interpreter-core', 'type-feature']
    title = 'Confusing error message when dividing timedelta using /'
    updated_at = <Date 2010-06-06.01:25:31.690>
    user = 'https://github.com/smontanaro'

    bugs.python.org fields:

    activity = <Date 2010-06-06.01:25:31.690>
    actor = 'belopolsky'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-06-06.01:25:31.693>
    closer = 'belopolsky'
    components = ['Interpreter Core']
    creation = <Date 2007-09-01.19:19:34.033>
    creator = 'skip.montanaro'
    dependencies = []
    files = ['8792']
    hgrepos = []
    issue_num = 1083
    keywords = ['patch']
    message_count = 10.0
    messages = ['55566', '55904', '75725', '101281', '101969', '102006', '103725', '103771', '103778', '107170']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'tebeka', 'mark.dickinson', 'belopolsky', 'vstinner', 'webograph', 'l0nwlf']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '1289118'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue1083'
    versions = ['Python 3.2']

    @smontanaro
    Copy link
    Contributor Author

    I discovered the hard way today that this won't work:

    >>> import datetime
    >>> d = datetime.timedelta(1)
    >>> d / 2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'int'

    The error message is misleading, because in fact timedelta
    objects *do* support division by ints, just not with the
    / operator:

    >>> d // 2
    datetime.timedelta(0, 43200)

    Is there some way the error message can be improved,
    perhaps by identifying the denominator as effectively
    a float?

    @smontanaro smontanaro added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Sep 1, 2007
    @brettcannon
    Copy link
    Member

    If you set nb_true_div on timedelta objects to delta_divide (just like
    nb_floor_div) you won't have this problem as the division will just
    work. Otherwise there is no other good way as if the divisor doesn't
    work you need to return NotImplemented, which then tries the right-hand
    object which fails, and 'object' returns the TypeError. Best you could
    do is a warning or something.

    But it might make sense to only set it on nb_true_div as the division
    does not round anything.

    @vstinner
    Copy link
    Member

    The current behaviour is consistent with the integer divison:
    >>> 21 // 10
    2
    >>> timedelta(microseconds=20) // 10
    datetime.timedelta(0, 0, 2)
    
    Whereas int/int gives float:
    >>> 21 / 10
    2.1000000000000001
    >> timedelta(microseconds=20) / 1
    ...
    TypeError: unsupported operand type(s) for /: ...

    Now in the real world, it's hard to understand that the operator //
    should be used instead of /. So timedelta()/int might be an alias to
    timedelta()//int.

    @tebeka
    Copy link
    Mannequin

    tebeka mannequin commented Mar 18, 2010

    I see the same problem when "from __future__ import division" on the 2.x series. Seem like the timedelta objects is missing the __truediv__ method.

    @l0nwlf
    Copy link
    Mannequin

    l0nwlf mannequin commented Mar 31, 2010

    I do not understand why python2.7 is marked in Version tag ?

    I reproduced the error on 3.1 but no isues on 2.7

    06:39:30 l0nwlf-MBP:data $ python2.7
    Python 2.7a4+ (trunk:78750, Mar  7 2010, 08:09:00) 
    [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import datetime
    >>> d = datetime.timedelta(1)
    >>> d / 2
    datetime.timedelta(0, 43200)
    >>> d // 2
    datetime.timedelta(0, 43200)
    >>> 
    
    06:41:13 l0nwlf-MBP:data $ python3.1
    Python 3.1.1 (r311:74480, Mar 21 2010, 20:21:46) 
    [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import datetime
    >>> d = datetime.timedelta(1)
    >>> d / 2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'int'
    >>> d // 2
    datetime.timedelta(0, 43200)
    >>>

    @tebeka
    Copy link
    Mannequin

    tebeka mannequin commented Mar 31, 2010

    It's marked on 2.7 due to the following (this is svn 79528)

    >>> from datetime import timedelta
    >>> d = timedelta(1)
    >>> d / 2
    datetime.timedelta(0, 43200)
    >>> d // 2
    datetime.timedelta(0, 43200)
    >>> from __future__ import division
    >>> d / 2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'int'
    >>>

    @AlexanderBelopolsky
    Copy link
    Mannequin

    AlexanderBelopolsky mannequin commented Apr 20, 2010

    This is certainly not a bug, so I don't think targeting 2.7 is appropriate. I have explained in a comment on bpo-2706 (see msg75917) why I believe true division of timedelta by int should not be supported. In short, true division of timedelta by int is supposed to return fractional number of microseconds, but python lacks a type that can represent it.

    @mdickinson
    Copy link
    Member

    I think it's fine to do the division and round the result to the nearest whole number of microseconds. I don't see any good reason for disallowing timedelta / int (or even timedelta / float).

    @mdickinson mdickinson added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Apr 20, 2010
    @smontanaro
    Copy link
    Contributor Author

    Mark> I think it's fine to do the division and round the result to the
    Mark> nearest whole number of microseconds.

    Right. Just think of a timedelta as a floating point number of seconds with
    very limited precision (1e-6 seconds).

    @abalkin
    Copy link
    Member

    abalkin commented Jun 6, 2010

    Closing as duplicate of bpo-1289118. Division of timedelta by integer is supported in py3k since r81625.

    @abalkin abalkin closed this as completed Jun 6, 2010
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    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-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants