]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112451: Prohibit subclassing of datetime.timezone. (#114190)
authorMariusz Felisiak <felisiak.mariusz@gmail.com>
Fri, 26 Jan 2024 08:33:13 +0000 (09:33 +0100)
committerGitHub <noreply@github.com>
Fri, 26 Jan 2024 08:33:13 +0000 (09:33 +0100)
This is consistent with C-extension datetime.timezone.

Lib/_pydatetime.py
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2024-01-24-20-11-46.gh-issue-112451.7YrG4p.rst [new file with mode: 0644]

index bca2acf1fc88cf95d29229b60d245a9d089a944d..355145387e355bbb749dd052e3d8f092ebfdc400 100644 (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)
index 8bda17358db87f93831288d3a01087ff630b974b..53ad5e57ada0178649b5c5c5d8e0feb159f79fa0 100644 (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]:
diff --git a/Misc/NEWS.d/next/Library/2024-01-24-20-11-46.gh-issue-112451.7YrG4p.rst b/Misc/NEWS.d/next/Library/2024-01-24-20-11-46.gh-issue-112451.7YrG4p.rst
new file mode 100644 (file)
index 0000000..126ca36
--- /dev/null
@@ -0,0 +1,2 @@
+Prohibit subclassing pure-Python :class:`datetime.timezone`. This is consistent
+with C-extension implementation. Patch by Mariusz Felisiak.