bpo-36004: Add date.fromisocalendar (GH-11888)

This commit implements the first version of date.fromisocalendar, the
inverse function for date.isocalendar.
This commit is contained in:
Paul Ganssle 2019-04-29 09:22:03 -04:00 committed by Victor Stinner
parent a86e06433a
commit 88c0937056
6 changed files with 209 additions and 0 deletions

View file

@ -1795,6 +1795,82 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
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,
]
# 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