-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
bpo-36004: Add date.fromisocalendar #11888
Changes from all commits
6d323ad
fbab728
925de2f
3ecf02e
e645cd0
86edee5
74fb523
46c6458
28c7fb2
99e75d7
c269a5c
c8444b7
958ffff
3307865
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1795,6 +1795,82 @@ def test_fromisoformat_fails_typeerror(self): | |
with self.assertRaises(TypeError): | ||
self.theclass.fromisoformat(bad_type) | ||
|
||
def test_fromisocalendar(self): | ||
# For each test case, assert that fromisocalendar is the | ||
# inverse of the isocalendar function | ||
dates = [ | ||
(2016, 4, 3), | ||
(2005, 1, 2), # (2004, 53, 7) | ||
(2008, 12, 30), # (2009, 1, 2) | ||
(2010, 1, 2), # (2009, 53, 6) | ||
(2009, 12, 31), # (2009, 53, 4) | ||
(1900, 1, 1), # Unusual non-leap year (year % 100 == 0) | ||
(1900, 12, 31), | ||
(2000, 1, 1), # Unusual leap year (year % 400 == 0) | ||
(2000, 12, 31), | ||
(2004, 1, 1), # Leap year | ||
(2004, 12, 31), | ||
(1, 1, 1), | ||
(9999, 12, 31), | ||
(MINYEAR, 1, 1), | ||
(MAXYEAR, 12, 31), | ||
] | ||
|
||
for datecomps in dates: | ||
with self.subTest(datecomps=datecomps): | ||
dobj = self.theclass(*datecomps) | ||
isocal = dobj.isocalendar() | ||
|
||
d_roundtrip = self.theclass.fromisocalendar(*isocal) | ||
|
||
self.assertEqual(dobj, d_roundtrip) | ||
|
||
def test_fromisocalendar_value_errors(self): | ||
isocals = [ | ||
(2019, 0, 1), | ||
(2019, -1, 1), | ||
(2019, 54, 1), | ||
(2019, 1, 0), | ||
(2019, 1, -1), | ||
(2019, 1, 8), | ||
(2019, 53, 1), | ||
(10000, 1, 1), | ||
(0, 1, 1), | ||
(9999999, 1, 1), | ||
(2<<32, 1, 1), | ||
(2019, 2<<32, 1), | ||
(2019, 1, 2<<32), | ||
] | ||
|
||
for isocal in isocals: | ||
with self.subTest(isocal=isocal): | ||
with self.assertRaises(ValueError): | ||
self.theclass.fromisocalendar(*isocal) | ||
|
||
def test_fromisocalendar_type_errors(self): | ||
err_txformers = [ | ||
str, | ||
float, | ||
lambda x: None, | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to use 3 loops to test all combinations, rather than generate these combinations manualy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I've converted it over. I think it's a little harder to understand what's going on in the version where the test cases are generated (compared to manual), but using a loop also has its advantages. |
||
|
||
# Take a valid base tuple and transform it to contain one argument | ||
# with the wrong type. Repeat this for each argument, e.g. | ||
# [("2019", 1, 1), (2019, "1", 1), (2019, 1, "1"), ...] | ||
isocals = [] | ||
base = (2019, 1, 1) | ||
for i in range(3): | ||
for txformer in err_txformers: | ||
err_val = list(base) | ||
err_val[i] = txformer(err_val[i]) | ||
isocals.append(tuple(err_val)) | ||
|
||
for isocal in isocals: | ||
with self.subTest(isocal=isocal): | ||
with self.assertRaises(TypeError): | ||
self.theclass.fromisocalendar(*isocal) | ||
|
||
|
||
############################################################################# | ||
# datetime tests | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Added new alternate constructors :meth:`datetime.date.fromisocalendar` and | ||
:meth:`datetime.datetime.fromisocalendar`, which construct date objects from | ||
ISO year, week number and weekday; these are the inverse of each class's | ||
``isocalendar`` method. Patch by Paul Ganssle. |
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.
Maybe use MINYEAR/MAXYEAR here.
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.
Instead of converting these over, I have just added a
MINYEAR
andMAXYEAR
test. Even though it's redundant, I like to have the explicit callout of the current boundaries, so that ifMINYEAR
orMAXYEAR
get modified in a way that breaks backwards compatibility, it will raise an error.