• Re: Fwd: timedelta object recursion bug

    From Jon Ribbens@21:1/5 to Ben Hirsig on Thu Jul 28 15:22:48 2022
    On 2022-07-28, Ben Hirsig <iamgusi@gmail.com> wrote:
    Hi, I noticed this when using the requests library in the response.elapsed object (type timedelta). Tested using the standard datetime library alone with the example displayed on https://docs.python.org/3/library/datetime.html#examples-of-usage-timedelta

    It appears as though the timedelta object recursively adds its own
    attributes (min, max, resolution) as further timedelta objects. I’m not sure how deep they go, but presumably hitting the recursion limit.

    from datetime import timedelta
    year = timedelta(days=365)
    print(year.max)
    999999999 days, 23:59:59.999999
    print(year.max.min.max.resolution.max.min)
    -999999999 days, 0:00:00

    Why do you think this is a bug?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Hirsig@21:1/5 to All on Thu Jul 28 19:54:54 2022
    Hi, I noticed this when using the requests library in the response.elapsed object (type timedelta). Tested using the standard datetime library alone
    with the example displayed on https://docs.python.org/3/library/datetime.html#examples-of-usage-timedelta



    It appears as though the timedelta object recursively adds its own
    attributes (min, max, resolution) as further timedelta objects. I’m not
    sure how deep they go, but presumably hitting the recursion limit.



    from datetime import timedelta

    year = timedelta(days=365)

    print(year.max)

    999999999 days, 23:59:59.999999

    print(year.max.min.max.resolution.max.min)

    -999999999 days, 0:00:00



    I’m using 3.10.3



    Cheers

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Ben Hirsig on Thu Jul 28 17:18:01 2022
    On 28/07/2022 10:54, Ben Hirsig wrote:
    Hi, I noticed this when using the requests library in the response.elapsed object (type timedelta). Tested using the standard datetime library alone with the example displayed on https://docs.python.org/3/library/datetime.html#examples-of-usage-timedelta



    It appears as though the timedelta object recursively adds its own
    attributes (min, max, resolution) as further timedelta objects. I’m not sure how deep they go, but presumably hitting the recursion limit.



    from datetime import timedelta

    year = timedelta(days=365)

    print(year.max)

    999999999 days, 23:59:59.999999

    print(year.max.min.max.resolution.max.min)

    -999999999 days, 0:00:00



    I’m using 3.10.3

    It's not recursion, it's a reference cycle. In fact, more than one:

    from datetime import timedelta
    year = timedelta(days=365)
    type(year)
    <class 'datetime.timedelta'>
    type(year.max)
    <class 'datetime.timedelta'>
    year.max is year.max.max
    True
    type(year.min)
    <class 'datetime.timedelta'>
    year.min is year.min.min
    True

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Ben Hirsig on Thu Jul 28 19:54:09 2022
    Ben Hirsig wrote at 2022-7-28 19:54 +1000:
    Hi, I noticed this when using the requests library in the response.elapsed >object (type timedelta). Tested using the standard datetime library alone >with the example displayed on >https://docs.python.org/3/library/datetime.html#examples-of-usage-timedelta



    It appears as though the timedelta object recursively adds its own
    attributes (min, max, resolution) as further timedelta objects. I’m not >sure how deep they go, but presumably hitting the recursion limit.

    If you look at the source, you will see that `min`, `max`, `resolution`
    are class level attributes. Their values are `timedelta` instances.
    Therefore, you can access e.g. `timedelta(days=365).min.max.resolution`.
    But this is nothing to worry about.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Ben Hirsig on Thu Jul 28 23:21:16 2022
    Please stay on the list (such that others can help, too)

    Ben Hirsig wrote at 2022-7-29 06:53 +1000:
    Thanks for the replies, I'm just trying to understand why this would be >useful?

    E.g. why does max need a min/max/resolution, and why would these attributes >themselves need a min/max/resolution, etc, etc?

    `max` is a `timedelta` and as such inherits (e.g.) `resolution`
    from the class (as any other `timedelta` instance).

    Note that `timedelta` instances do not have a `max` (`min|resolution`)
    slot. When `max` is looked up, it is first searched in the instance
    (and not found), then in the class where it is found:
    all `max` accesses result in the same object.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)