gh-112451: Prohibit subclassing of datetime.timezone. (#114190)

This is consistent with C-extension datetime.timezone.
This commit is contained in:
Mariusz Felisiak 2024-01-26 09:33:13 +01:00 committed by GitHub
parent b69548a0f5
commit 456e274578
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 0 deletions

View File

@ -2347,6 +2347,9 @@ class timezone(tzinfo):
"timedelta(hours=24).")
return cls._create(offset, name)
def __init_subclass__(cls):
raise TypeError("type 'datetime.timezone' is not an acceptable base type")
@classmethod
def _create(cls, offset, name=None):
self = tzinfo.__new__(cls)

View File

@ -301,6 +301,10 @@ class TestTimeZone(unittest.TestCase):
self.assertIsInstance(timezone.utc, tzinfo)
self.assertIsInstance(self.EST, tzinfo)
def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class MyTimezone(timezone): pass
def test_utcoffset(self):
dummy = self.DT
for h in [0, 1.5, 12]:

View File

@ -0,0 +1,2 @@
Prohibit subclassing pure-Python :class:`datetime.timezone`. This is consistent
with C-extension implementation. Patch by Mariusz Felisiak.