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

gh-118948: add support for ISO 8601 basic format to datetime #120553

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
normalize test order
picnixz committed Jun 17, 2024
commit 9686b179445cad67919a6a550546771b9411b378
17 changes: 11 additions & 6 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
@@ -2079,18 +2079,21 @@ def test_roundtrip(self):
def test_isoformat(self):
t = self.theclass(1, 2, 3, 4, 5, 1, 123)
self.assertEqual(t.isoformat(), "0001-02-03T04:05:01.000123")
self.assertEqual(t.isoformat('T'), "0001-02-03T04:05:01.000123")
self.assertEqual(t.isoformat(' '), "0001-02-03 04:05:01.000123")
self.assertEqual(t.isoformat('\x00'), "0001-02-03\x0004:05:01.000123")

self.assertEqual(t.isoformat(basic=True), "00010203T040501.000123")
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like there has to be a better way to do this (It could also automatically convert it to basic so we don't have to enter both by hand), a helper function would cut down the amount of lines. (Well, it wouldn't increase.)

Copy link
Member Author

Choose a reason for hiding this comment

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

This would be out of scope for the PR and in this case, explicit is likely better than implicit IMO.

Copy link
Contributor

Choose a reason for hiding this comment

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

Out of scope is subjective, you pretty much doubled the amount of tests. I guess it could be a follow up pr. (I don't mind doing it)

def assertIsoFormats(self, t, expected):
    self.assertEqual(t.isoformat(), expected)
    expected_basic = expected_standard.replace("-", "").replace(":", "")
    self.assertEqual(t.isoformat(basic=True), expected_basic)

Four lines to save 40ish.

explicit is likely better than implicit IMO.

Any specific reason, for readability?

Copy link
Member Author

@picnixz picnixz Mar 22, 2025

Choose a reason for hiding this comment

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

Any specific reason, for readability?

Yes, and for tracking bugs in this case. However, if we want to assert the ISO format differently, we should do it in a refactoring PR and not in this one. The ISO format is not the only whose checks could be refactored in some sense IMO.


self.assertEqual(t.isoformat('T'), "0001-02-03T04:05:01.000123")
self.assertEqual(t.isoformat('T', basic=True), "00010203T040501.000123")

self.assertEqual(t.isoformat(' '), "0001-02-03 04:05:01.000123")
self.assertEqual(t.isoformat(' ', basic=True), "00010203 040501.000123")

self.assertEqual(t.isoformat('\x00'), "0001-02-03\x0004:05:01.000123")
self.assertEqual(t.isoformat('\x00', basic=True), "00010203\x00040501.000123")

# bpo-34482: Check that surrogates are handled properly.
self.assertEqual(t.isoformat('\ud800'),
"0001-02-03\ud80004:05:01.000123")
self.assertEqual(t.isoformat('\ud800'), "0001-02-03\ud80004:05:01.000123")
self.assertEqual(t.isoformat('\ud800', basic=True), "00010203\ud800040501.000123")

self.assertEqual(t.isoformat(timespec='hours'), "0001-02-03T04")
self.assertEqual(t.isoformat(timespec='hours', basic=True), "00010203T04")

@@ -2113,8 +2116,10 @@ def test_isoformat(self):
self.assertEqual(t.isoformat(sep=' ', timespec='minutes', basic=True), "00010203 0405")

self.assertRaises(ValueError, t.isoformat, timespec='foo')
self.assertRaises(ValueError, t.isoformat, timespec='foo', basic=True)
# bpo-34482: Check that surrogates are handled properly.
self.assertRaises(ValueError, t.isoformat, timespec='\ud800')
self.assertRaises(ValueError, t.isoformat, timespec='\ud800', basic=True)
# str is ISO format with the separator forced to a blank.
self.assertEqual(str(t), "0001-02-03 04:05:01.000123")