diff --git a/dateutil/parser/_parser.py b/dateutil/parser/_parser.py index 0da0f3e6b..7df6a2916 100644 --- a/dateutil/parser/_parser.py +++ b/dateutil/parser/_parser.py @@ -1166,6 +1166,9 @@ def _build_tzinfo(self, tzinfos, tzname, tzoffset): tzinfo = tz.tzstr(tzdata) elif isinstance(tzdata, integer_types): tzinfo = tz.tzoffset(tzname, tzdata) + else: + raise TypeError("Offset must be tzinfo subclass, tz string, " + "or int offset.") return tzinfo def _build_tzaware(self, naive, res, tzinfos): diff --git a/dateutil/test/test_parser.py b/dateutil/test/test_parser.py index 21584c611..c95631ceb 100644 --- a/dateutil/test/test_parser.py +++ b/dateutil/test/test_parser.py @@ -238,6 +238,60 @@ def test_parse_bytearray(self): assert res == expected +class TestTzinfoInputTypes(object): + def test_tzinfo_dict_could_return_none(self): + dstr = "2017-02-03 12:40 BRST" + result = parse(dstr, tzinfos={"BRST": None}) + expected = datetime(2017, 2, 3, 12, 40) + assert result == expected + + def test_tzinfos_callable_could_return_none(self): + dstr = "2017-02-03 12:40 BRST" + result = parse(dstr, tzinfos=lambda *args: None) + expected = datetime(2017, 2, 3, 12, 40) + assert result == expected + + def test_invalid_tzinfo_input(self): + dstr = "2014 January 19 09:00 UTC" + # Pass an absurd tzinfos object + tzinfos = {"UTC": ValueError} + with pytest.raises(TypeError): + parse(dstr, tzinfos=tzinfos) + + def test_valid_tzinfo_tzinfo_input(self): + dstr = "2014 January 19 09:00 UTC" + tzinfos = {"UTC": tz.tzutc()} + expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzutc()) + res = parse(dstr, tzinfos=tzinfos) + assert res == expected + + def test_valid_tzinfo_unicode_input(self): + dstr = "2014 January 19 09:00 UTC" + tzinfos = {u"UTC": u"UTC+0"} + expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzutc()) + res = parse(dstr, tzinfos=tzinfos) + assert res == expected + + def test_valid_tzinfo_callable_input(self): + dstr = "2014 January 19 09:00 UTC" + + def tzinfos(*args, **kwargs): + return u"UTC+0" + + expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzutc()) + res = parse(dstr, tzinfos=tzinfos) + assert res == expected + + @pytest.mark.xfail(reason="Doesn't check for UTC in tzinfos dict, " + "not sure it *should*.") + def test_valid_tzinfo_int_input(self): + dstr = "2014 January 19 09:00 UTC" + tzinfos = {u"UTC": -28800} + expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzutc()) + res = parse(dstr, tzinfos=tzinfos) + assert res == expected + + class ParserTest(unittest.TestCase): @classmethod @@ -508,14 +562,6 @@ def testUnspecifiedDayFallbackFebLeapYear(self): self.assertEqual(parse("Feb 2008", default=datetime(2010, 1, 31)), datetime(2008, 2, 29)) - def testTzinfoDictionaryCouldReturnNone(self): - self.assertEqual(parse('2017-02-03 12:40 BRST', tzinfos={"BRST": None}), - datetime(2017, 2, 3, 12, 40)) - - def testTzinfosCallableCouldReturnNone(self): - self.assertEqual(parse('2017-02-03 12:40 BRST', tzinfos=lambda *args: None), - datetime(2017, 2, 3, 12, 40)) - def testErrorType01(self): with pytest.raises(ValueError): parse('shouldfail')