Skip to content

Commit

Permalink
pythongh-112451: Prohibit subclassing of datetime.timezone.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixxm committed Jan 24, 2024
1 parent 7b25e5e commit 00b10e6
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 26 deletions.
11 changes: 1 addition & 10 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2348,16 +2348,7 @@ def __new__(cls, offset, name=_Omitted):
return cls._create(offset, name)

def __init_subclass__(cls):
# When deprecation ends replace with:
# raise TypeError("type 'datetime.timezone' is not an acceptable base type")
import warnings
warnings.warn(
"Subclassing 'datetime.timezone' is deprecated and scheduled for removal "
"in Python 3.15.",
DeprecationWarning,
stacklevel=2,
)
super().__init_subclass__()
raise TypeError("type 'datetime.timezone' is not an acceptable base type")

@classmethod
def _create(cls, offset, name=None):
Expand Down
14 changes: 0 additions & 14 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,7 @@ def test_inheritance(self):
self.assertIsInstance(timezone.utc, tzinfo)
self.assertIsInstance(self.EST, tzinfo)

def test_cannot_subclass_deprecation_warning(self):
if '_Fast' in self.__class__.__name__:
self.skipTest('Only run for Pure Python implementation')

msg = (
"Subclassing 'datetime.timezone' is deprecated and scheduled for removal "
"in Python 3.15."
)
with self.assertWarnsRegex(DeprecationWarning, msg):
class MyTimezone(timezone): pass

def test_cannot_subclass(self):
if '_Pure' in self.__class__.__name__:
self.skipTest('Only run for Fast C implementation')

msg = "type 'datetime.timezone' is not an acceptable base type"
with self.assertRaisesRegex(TypeError, msg):
class MyTimezone(timezone): pass
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Prohibit subclassing pure-Python ``datetime.timezone``. This is consistent
with C-extension implementation. Patch by Mariusz Felisiak.

0 comments on commit 00b10e6

Please sign in to comment.