GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-109798: Normalize `_datetime` and `datetime` error messages by donbarbos · Pull Request #127345 · python/cpython

Conversation

@donbarbos

@donbarbos donbarbos commented Nov 27, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I only updated the messages, I fixed everything I found.
From these 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

i made this one error:

ValueError: year must be in 1..9999, not {year} 

and I made the same message template for the fields year, month, day, hour, minute, second, microsecond and fold because I decided that it was worth leaving the field that was received in the output and at the same time using one string instead of tuple.
with the rest of the error messages it's easier, I just reduced them to one type

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@donbarbos
Update error messages to be the same in datetime
764eb1b
@donbarbos
donbarbos requested review from abalkin and pganssle as code owners November 27, 2024 19:16
@bedevere-app

bedevere-app Bot commented Nov 27, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting review label Nov 27, 2024
@donbarbos donbarbos changed the title Update error messages to be the same in datetime gh-109798: Update error messages to be the same in datetime Nov 27, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Nov 27, 2024
datetime error message is different between _pydatetime.py and _datetimemodule.c #109798
Closed
donbarbos added 2 commits November 27, 2024 23:29
@donbarbos
Add NEWS.d/next
2bf419f
@donbarbos
fixed syntax errors
4363e9e
erlend-aasland
erlend-aasland reviewed Nov 29, 2024
View reviewed changes
Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@donbarbos
Move Py_DECREF after PyErr_Format
9da0dfc
@donbarbos
donbarbos force-pushed the issue-109798 branch from 1d65305 to 9da0dfc Compare November 29, 2024 12:31
@donbarbos
Add more info in message error in _pydatetime impl
179423d
erlend-aasland
erlend-aasland reviewed Nov 29, 2024
View reviewed changes
Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Nov 29, 2024

Copy link
Copy Markdown
Contributor

BTW, please do not force-push; it makes reviewing harder. Moreover, all commits are squashed upon merge anyway, so there's no need for the PR/branch to be cluttered with amendment commits. See also the devguide.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@donbarbos

donbarbos commented Nov 29, 2024

Copy link
Copy Markdown
Contributor Author

@erlend-aasland sorry, i got it.
These are all the inconsistencies in error messages I have found so far.
I'm done with this and that's enough for now.

erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland erlend-aasland changed the title gh-109798: Update error messages to be the same in datetime gh-109798: Normalize _datetime and datetime error messages Nov 29, 2024
erlend-aasland
erlend-aasland reviewed Nov 29, 2024
View reviewed changes
Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

donbarbos and others added 6 commits November 29, 2024 13:30
@donbarbos @erlend-aasland
Update Modules/_datetimemodule.c
f691251
Co-authored-by: Erlend E. Aasland <[email protected]>
@donbarbos @erlend-aasland
Update Modules/_datetimemodule.c
d8973cf
Co-authored-by: Erlend E. Aasland <[email protected]>
@donbarbos @erlend-aasland
Update Modules/_datetimemodule.c
5eea62f
Co-authored-by: Erlend E. Aasland <[email protected]>
@donbarbos
Update Modules/_datetimemodule.c for optimisation
79543cc
@donbarbos
Revert last update Modules/_datetimemodule.c
d174497
@donbarbos
Update Modules/_datetimemodule.c
498c4ba
erlend-aasland
erlend-aasland reviewed Nov 29, 2024
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2024-11-27-23-29-05.gh-issue-109798.OPj1CT.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

donbarbos added 4 commits November 29, 2024 18:36
@donbarbos
Update Misc/NEWS.d message
216d0fe
@donbarbos
Update Misc/NEWS.d message
c409fec
@donbarbos
Update Misc/NEWS.d message
3f454f6
@donbarbos
Update Misc/NEWS.d message
a2b8f7a
ZeroIntensity
ZeroIntensity reviewed Nov 30, 2024
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nitpicks, and two notes:

  • I'm happy with doing this, but we do need to be aware that this is a slight breaking change for anybody relying on the exception messages. They shouldn't be doing that, but we need to be aware of it anyway.
  • It's fine right now, but PyErr_Format with %R can have unintended side effects if the __repr__ of the passed object is evil. Again, not a problem here as far as I can tell, but it's definitely worth noting.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

donbarbos and erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
Comment thread Lib/_pydatetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_pydatetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_pydatetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_pydatetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_pydatetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_pydatetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2024-11-27-23-29-05.gh-issue-109798.OPj1CT.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

donbarbos and others added 6 commits November 30, 2024 04:17
@donbarbos @ZeroIntensity
Update Lib/_pydatetime.py
4d31d33
Co-authored-by: Peter Bierma <[email protected]>
@donbarbos @ZeroIntensity
Update Lib/_pydatetime.py
f05ebba
Co-authored-by: Peter Bierma <[email protected]>
@donbarbos @ZeroIntensity
Update Lib/_pydatetime.py
f840105
Co-authored-by: Peter Bierma <[email protected]>
@donbarbos @ZeroIntensity
Update Lib/_pydatetime.py
61c95a5
Co-authored-by: Peter Bierma <[email protected]>
@donbarbos @ZeroIntensity
Update Lib/_pydatetime.py
2ab77b3
Co-authored-by: Peter Bierma <[email protected]>
@donbarbos
Update Lib/_pydatetime.py
b1e272a
ZeroIntensity
ZeroIntensity reviewed Nov 30, 2024
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, how far fetched is it to ask for some assertRaisesRegex tests 😄

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

donbarbos reacted with thumbs up emoji erlend-aasland reacted with laugh emoji
All reactions
  • 👍 1 reaction
  • 😄 1 reaction
donbarbos added 2 commits November 30, 2024 08:36
@donbarbos
Update Lib/_pydatetime.py
7a35bd4
@donbarbos
Add tests
cfd18cb
@donbarbos

donbarbos commented Nov 30, 2024

Copy link
Copy Markdown
Contributor Author

@ZeroIntensity i added tests ✅ ,
but if you need tests for the fromisoformat method - need to merge this PR #127242 because it solves many problems, for example, the rules for different implementations may seem different

ZeroIntensity reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@donbarbos
donbarbos requested review from ZeroIntensity and erlend-aasland November 30, 2024 06:58
ZeroIntensity
ZeroIntensity reviewed Nov 30, 2024
View reviewed changes
Comment thread Modules/_datetimemodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Nov 30, 2024
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting close to ready :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/_pydatetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_pydatetime.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/_pydatetime.py Outdated
Comment on lines +63 to +70
assert 1 <= month <= 12, f"month must be in 1..12, but got {month}"
return _DAYS_BEFORE_MONTH[month] + (month > 2 and _is_leap(year))

def _ymd2ord(year, month, day):
"year, month, day -> ordinal, considering 01-Jan-0001 as day 1."
assert 1 <= month <= 12, 'month must be in 1..12'
assert 1 <= month <= 12, f"month must be in 1..12, but got {month}"
dim = _days_in_month(year, month)
assert 1 <= day <= dim, ('day must be in 1..%d' % dim)
assert 1 <= day <= dim, f"day must be in 1..{dim}, but got {day}"

@ZeroIntensity ZeroIntensity Nov 30, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth changing these to ifs and ValueErrors; assertions are disabled when Python is ran with -O. (Though, it's theoretically possible that will break code for people relying on AssertionError. I'm not sure how big of an issue that is.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@donbarbos donbarbos Dec 1, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the practice of calling assert is also found in other modules and I think this issue should be raised separately (not in this PR)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ZeroIntensity ZeroIntensity Dec 1, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to do it in this PR, because IMO, unifying the error types fall under the category of "normalizing." I'm not super crazy about it though--any opinion @erlend-aasland?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
donbarbos and others added 4 commits December 1, 2024 07:43
@donbarbos @ZeroIntensity
Update _pydatetime.py
2827514
Co-authored-by: Peter Bierma <[email protected]>
@donbarbos @ZeroIntensity
Update _pydatetime.py
cd3bdc1
Co-authored-by: Peter Bierma <[email protected]>
@donbarbos
Change but got to not
9915dfe
@donbarbos
Correct line break
1da5a3a
@donbarbos

donbarbos commented Dec 20, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@erlend-aasland could you merge it? (if you have time :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Dec 20, 2024

Copy link
Copy Markdown
Member

FYI, most of the core devs are done for the holidays. You'll have to wait until after the new year probably.

donbarbos reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pganssle
pganssle approved these changes Feb 11, 2025
View reviewed changes

@pganssle pganssle left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly suspect that this is going to break people who are testing against exact error messages, but that's really not part of our public API, so that is fine.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

donbarbos and srinivasreddy reacted with rocket emoji
All reactions
  • 🚀 2 reactions
Comment thread Lib/_pydatetime.py
raise ValueError("offset must be a timedelta "
"strictly between -timedelta(hours=24) and "
"timedelta(hours=24).")
f"timedelta(hours=24), not {offset!r}")

@pganssle pganssle Feb 11, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not going to be a great user experience here because of how bad the timedelta formatter is:

>>> print(f"{timedelta(hours=-25)!r}")
datetime.timedelta(days=-2, seconds=82800)

Maybe it will be clearer if we do it this way:

if offset < timedelta(0):
    offset_str = f"-{-offset)}"
else:
     offset_str = str(offset)

That will print stuff like -1 day, 1:00:00 instead of timedelta(days=-2, seconds=82800). Though looking at it, I realize that the way it gets printed is misleading, since that's the output you get from print(timedelta(hours=-23)) and users have no way of knowing what is going on under the hood there.

Probably a more elaborate timedelta formatter would be better, since we basically always want this in HH:MM:SS.fff format, since that reflects what we care about best and also is unambigous, but we don't have a particularly easy way to do that. I guess we can revisit this when/if #85426 is implemented.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@donbarbos donbarbos Feb 11, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pganssle
Okay, then let's solve this issue in a separate PR linked to that issue (I can send the changes soon).
I think this one can be merged for now and I would be glad if you review another PR #127242 for datetime module :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Feb 11, 2025
pganssle added 2 commits February 12, 2025 09:26
@pganssle
Update 2024-11-27-23-29-05.gh-issue-109798.OPj1CT.rst
610f067
@pganssle
Merge branch 'main' into issue-109798
410e0ce
@donbarbos

donbarbos commented Feb 12, 2025

Copy link
Copy Markdown
Contributor Author

@pganssle CI successfully passed 👍

srinivasreddy reacted with hooray emoji srinivasreddy reacted with heart emoji
All reactions
  • 🎉 1 reaction
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pganssle
pganssle merged commit 3e222e3 into python:main Feb 12, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 12, 2025
encukou added a commit to picnixz/cpython that referenced this pull request Feb 19, 2025
@encukou
Merge in the main branch to resolve conflict with python#127345
f108a14
@donbarbos donbarbos mentioned this pull request Mar 6, 2025
Inconsistent error messages when returning the wrong type for the type-conversion magic methods #130821
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ZeroIntensity ZeroIntensity ZeroIntensity left review comments
@pganssle pganssle pganssle approved these changes
@abalkin abalkin Awaiting requested review from abalkin
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@donbarbos @erlend-aasland @ZeroIntensity @pganssle
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.