8000 gh-112451: Prohibit subclassing of datetime.timezone. · felixxm/cpython@00b10e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00b10e6

Browse files
committed
pythongh-112451: Prohibit subclassing of datetime.timezone.
1 parent 7b25e5e commit 00b10e6

File tree

4 files changed

+3
-26
lines changed

4 files changed

+3
-26
lines changed

Lib/_pydatetime.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,16 +2348,7 @@ def __new__(cls, offset, name=_Omitted):
23482348
return cls._create(offset, name)
23492349

23502350
def __init_subclass__(cls):
2351-
# When deprecation ends replace with:
2352-
# raise TypeError("type 'datetime.timezone' is not an acceptable base type")
2353-
import warnings
2354-
warnings.warn(
2355-
"Subclassing 'datetime.timezone' is deprecated and scheduled for removal "
2356-
"in Python 3.15.",
2357-
DeprecationWarning,
2358-
stacklevel=2,
2359-
)
2360-
super().__init_subclass__()
2351+
raise TypeError("type 'datetime.timezone' is not an acceptable base type")
23612352

23622353
@classmethod
23632354
def _create(cls, offset, name=None):

Lib/test/datetimetester.py

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

304-
def test_cannot_subclass_deprecation_warning(self):
305-
if '_Fast' in self.__class__.__name__:
306-
self.skipTest('Only run for Pure Python implementation')
307-
308-
msg = (
309-
"Subclassing 'datetime.timezone' is deprecated and scheduled for removal "
310-
"in Python 3.15."
311-
)
312-
with self.assertWarnsRegex(DeprecationWarning, msg):
313-
class MyTimezone(timezone): pass
314-
315304
def test_cannot_subclass(self):
316-
if '_Pure' in self.__class__.__name__:
317-
self.skipTest('Only run for Fast C implementation')
318-
319305
msg = "type 'datetime.timezone' is not an acceptable base type"
320306
with self.assertRaisesRegex(TypeError, msg):
321307
class MyTimezone(timezone): pass

Misc/NEWS.d/next/Library/2024-01-17-16-34-00.gh-issue-112451.5y9enV.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prohibit subclassing pure-Python ``datetime.timezone``. This is consistent
2+
with C-extension implementation. Patch by Mariusz Felisiak.

0 commit comments

Comments
 (0)
292D
0