Skip to content

fix: pendulum.parse('2007-12-13/14:30') raised TypeError instead of uniform ValueError#954

Open
alexanderlukanin13 wants to merge 1 commit into
python-pendulum:masterfrom
alexanderlukanin13:interval_parsing_error
Open

fix: pendulum.parse('2007-12-13/14:30') raised TypeError instead of uniform ValueError#954
alexanderlukanin13 wants to merge 1 commit into
python-pendulum:masterfrom
alexanderlukanin13:interval_parsing_error

Conversation

@alexanderlukanin13

Copy link
Copy Markdown
  • Added tests for changed code.
  • Updated documentation for changed code - not applicable

When trying to parse non-standard Interval format mixing date/datetime and time (both individually valid), error message is inconsistent because Time is falling through unchecked.

Behavior in 3.2.0:

>>> pendulum.parse('2007-12-13/14:30')
Traceback (most recent call last):
  File "<python-input-177>", line 1, in <module>
    pendulum.parse('2007-12-13/14:30')
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "<..>/python3.14/site-packages/pendulum/parser.py", line 36, in parse
    return _parse(text, **options)
  File "<..>/python3.14/site-packages/pendulum/parser.py", line 112, in _parse
    return pendulum.interval(
           ~~~~~~~~~~~~~~~~~^
        pendulum.instance(
        ^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        ),
        ^^
    )
    ^
  File "<..>/python3.14/site-packages/pendulum/__init__.py", line 335, in interval
    return Interval(start, end, absolute=absolute)
  File "<..>/python3.14/site-packages/pendulum/interval.py", line 115, in __new__
    delta: timedelta = _end - _start
                       ~~~~~^~~~~~~~
TypeError: unsupported operand type(s) for -: 'Time' and 'datetime.date'

>>> pendulum.parse('2007-12-13T14:30/15:45')
Traceback (most recent call last):
  File "<python-input-178>", line 1, in <module>
    pendulum.parse('2007-12-13T14:30/15:45')
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<..>/python3.14/site-packages/pendulum/parser.py", line 36, in parse
    return _parse(text, **options)
  File "<..>/python3.14/site-packages/pendulum/parser.py", line 112, in _parse
    return pendulum.interval(
           ~~~~~~~~~~~~~~~~~^
        pendulum.instance(
        ^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        ),
        ^^
    )
    ^
  File "<..>/python3.14/site-packages/pendulum/__init__.py", line 335, in interval
    return Interval(start, end, absolute=absolute)
  File "<..>/python3.14/site-packages/pendulum/interval.py", line 45, in __new__
    raise ValueError(
        "Both start and end of an Interval must have the same type"
    )
ValueError: Both start and end of an Interval must have the same type

After fix the exception is uniform in all cases:

>>> pendulum.parse('2007-12-13/14:30')
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    pendulum.parse('2007-12-13/14:30')
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "<..>/python3.14/site-packages/pendulum/parser.py", line 36, in parse
    return _parse(text, **options)
  File "<..>/python3.14/site-packages/pendulum/parser.py", line 112, in _parse
    return pendulum.interval(
           ~~~~~~~~~~~~~~~~~^
        pendulum.instance(
        ^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        ),
        ^^
    )
    ^
  File "<..>/python3.14/site-packages/pendulum/__init__.py", line 334, in interval
    return Interval(start, end, absolute=absolute)
  File "<..>/python3.14/site-packages/pendulum/interval.py", line 46, in __new__
    raise ValueError(
        "Both start and end of an Interval must have the same type"
    )
ValueError: Both start and end of an Interval must have the same type

Note: format 2007-12-13T14:30/15:45 is allowed in some lenient ISO 8601 implementations (e.g. aniso8601 package). I believe it's a good thing that pendulum does not allow it because it's ambiguous.

…niform ValueError

Time object was falling through type checks in Interval.__new__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant