Skip to content

datetime error message is different between _pydatetime.py and _datetimemodule.c #109798

Closed

Description

@mattip

Bug report

Bug description:

import _datetime, _pydatetime
_datetime.date(1, 1, 50)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: day is out of range for month```

_pydatetime.date(1, 1, 50)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/_pydatetime.py", line 960, in __new__
    year, month, day = _check_date_fields(year, month, day)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/_pydatetime.py", line 535, in _check_date_fields
    raise ValueError('day must be in 1..%d' % dim, day)
ValueError: ('day must be in 1..31', 50)

The error message differs between the two implementations of datetime. This came up when testing PyPy, which uses the pure-python datetime implementation. xref conda-forge/rtoml-feedstock#1 (comment)

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Sep 24, 2023
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 26, 2023
  3. donbarbos commented on Nov 25, 2024

    @donbarbos
    Contributor

    @mattip So, you suggest to leave the option as in the _datetime implementation?

    I can send a PR

  4. mattip commented on Nov 27, 2024

    @mattip
    ContributorAuthor

    I don't really have an opinion which error message is better, only that they be the same.

  5. donbarbos commented on Nov 27, 2024

    @donbarbos
    Contributor

    Ok, I will leave error message as in the c implementation

    Firstly, it is better to output a string (not tuple), and in the python implementation, the second value in tuple is redundant.
    Secondly, it seems to me that it is the main implementation.

  6. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Nov 27, 2024
  7. donbarbos commented on Nov 27, 2024

    @donbarbos
    Contributor

    I also add more inconsistency in the error messages that I found in the code

    1. Errors about date and time have inconsistent messages, for example problem with year field may raise next errors:

    ValueError: ('year must be in 1..9999', {year})
    ValueError: Year is out of range: {year}
    ValueError: year {year} is out of range
    ValueError: Year {year} is out of range

    We need to make same message template for the fields year, month, day, hour, minute, second, microsecond and fold.

    2. Method date.fromisocalendar may raise next errors depends on implementation:

    ValueError: Invalid weekday: {day} (range is [1, 7])
    ValueError: Invalid day: {day} (range is [1, 7])

    3. Error with offset argument in timezone may raise next errors:

    ValueError: offset must be a timedelta strictly between -timedelta(hours=24) and timedelta(hours=24), not datetime.timedelta(days=21).  # with dot
    ValueError: offset must be a timedelta strictly between -timedelta(hours=24) and timedelta(hours=24), not datetime.timedelta(days=21)   # without dot
    ValueError: offset must be a timedelta strictly between -timedelta(hours=24) and timedelta(hours=24).
    ValueError: {name}()={offset}, must be a timedelta strictly between -timedelta(hours=24) and timedelta(hours=24)

    4. Errors about type of arugment

    for example if i call _pydatetime.datetime() and _datetime.datetime() with tz i get:

    TypeError: tzinfo argument must be None or of a tzinfo subclass                  # in _pydatetime
    TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'int'  # in _datetime
  8. added a commit that references this issue on Feb 12, 2025
  9. StanFromIreland commented on Mar 10, 2025

    @StanFromIreland
    Member

    PR was merged, issue is complete :-)

    It seems there was no plan to back port which is for the best for backwards compatibility.

  10. mattip commented on Mar 11, 2025

    @mattip
    ContributorAuthor

    Thanks!

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

    3.12only security fixes3.13only security fixes3.14bugs and security fixesextension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions