Skip to content

Commit f3ec4d1

Browse files
committed
pythongh-112451: Make datetime.timezone not acceptable as a base class.
This is consistent with C-extension datetime.timezone.
1 parent ac10947 commit f3ec4d1

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Lib/_pydatetime.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,9 @@ def __new__(cls, offset, name=_Omitted):
23472347
"timedelta(hours=24).")
23482348
return cls._create(offset, name)
23492349

2350+
def __init_subclass__(cls):
2351+
raise TypeError("type 'datetime.timezone' is not an acceptable base type")
2352+
23502353
@classmethod
23512354
def _create(cls, offset, name=None):
23522355
self = tzinfo.__new__(cls)

Lib/test/datetimetester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ def test_inheritance(self):
301301
self.assertIsInstance(timezone.utc, tzinfo)
302302
self.assertIsInstance(self.EST, tzinfo)
303303

304+
def test_cannot_subclass(self):
305+
msg = "type 'datetime.timezone' is not an acceptable base type"
306+
with self.assertRaisesRegex(TypeError, msg):
307+
class MyTimezone(timezone): pass
308+
304309
def test_utcoffset(self):
305310
dummy = self.DT
306311
for h in [0, 1.5, 12]:

0 commit comments

Comments
 (0)