FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.13] gh-138010: Fix `__init_subclass__` forwarding by `warnings.deprecated` (GH-138210) by brianschubert · Pull Request #138564 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 19 additions & 0 deletions Lib/test/test_warnings/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,25 @@ class D(C, x=3):

self.assertEqual(D.inited, 3)

def test_existing_init_subclass_in_sibling_base(self):
@deprecated("A will go away soon")
class A:
pass
class B:
def __init_subclass__(cls, x):
super().__init_subclass__()
cls.inited = x

with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"):
class C(A, B, x=42):
pass
self.assertEqual(C.inited, 42)

with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"):
class D(B, A, x=42):
pass
self.assertEqual(D.inited, 42)

def test_init_subclass_has_correct_cls(self):
init_subclass_saw = None

Expand Down
26 changes: 13 additions & 13 deletions Lib/warnings.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -602,27 +602,27 @@ def __new__(cls, /, *args, **kwargs):

arg.__new__ = staticmethod(__new__)

original_init_subclass = arg.__init_subclass__
# We need slightly different behavior if __init_subclass__
# is a bound method (likely if it was implemented in Python)
if isinstance(original_init_subclass, MethodType):
original_init_subclass = original_init_subclass.__func__
if "__init_subclass__" in arg.__dict__:
# __init_subclass__ is directly present on the decorated class.
# Synthesize a wrapper that calls this method directly.
original_init_subclass = arg.__init_subclass__
# We need slightly different behavior if __init_subclass__
# is a bound method (likely if it was implemented in Python).
# Otherwise, it likely means it's a builtin such as
# object's implementation of __init_subclass__.
if isinstance(original_init_subclass, MethodType):
original_init_subclass = original_init_subclass.__func__

@functools.wraps(original_init_subclass)
def __init_subclass__(*args, **kwargs):
warn(msg, category=category, stacklevel=stacklevel + 1)
return original_init_subclass(*args, **kwargs)

arg.__init_subclass__ = classmethod(__init_subclass__)
# Or otherwise, which likely means it's a builtin such as
# object's implementation of __init_subclass__.
else:
@functools.wraps(original_init_subclass)
def __init_subclass__(*args, **kwargs):
def __init_subclass__(cls, *args, **kwargs):
warn(msg, category=category, stacklevel=stacklevel + 1)
return original_init_subclass(*args, **kwargs)
return super(arg, cls).__init_subclass__(*args, **kwargs)

arg.__init_subclass__ = __init_subclass__
arg.__init_subclass__ = classmethod(__init_subclass__)

arg.__deprecated__ = __new__.__deprecated__ = msg
__init_subclass__.__deprecated__ = msg
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix an issue where defining a class with an :func:`@warnings.deprecated
<warnings.deprecated>`-decorated base class may not invoke the correct
:meth:`~object.__init_subclass__` method in cases involving multiple
inheritance. Patch by Brian Schubert.
Loading

Back | FazBrowse Home | New Git URL