gh-112451: Prohibit subclassing of datetime.timezone. (#114190)
This is consistent with C-extension datetime.timezone.
This commit is contained in:
parent
b69548a0f5
commit
456e274578
@ -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)
|
||||
|
@ -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]:
|
||||
|
@ -0,0 +1,2 @@
|
||||
Prohibit subclassing pure-Python :class:`datetime.timezone`. This is consistent
|
||||
with C-extension implementation. Patch by Mariusz Felisiak.
|
Loading…
x
Reference in New Issue
Block a user