bpo-29581: Make ABCMeta.new pass **kwargs to type.new (#527) · python/cpython@bd583ef (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

Commit bd583ef

Nateambv

authored and

committed

bpo-29581: Make ABCMeta.__new__ pass **kwargs to type.__new__ (#527)

Many metaclasses in the standard library don't play nice with __init_subclass__. This bug makes ABCMeta in particular with __init_subclass__, which is an 80/20 solution for me personally. AFAICT, a general solution to this problem requires updating all metaclasses in the standard library to make sure they pass **kwargs to type.__new__, whereas this PR only fixes ABCMeta. For context, seehttps://bugs.python.org/issue29581. * added a test combining ABCMeta and __init_subclass__ * Added NEWS item

File tree

3 files changed

lines changed

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -129,8 +129,8 @@ class ABCMeta(type):
129 129 # external code.
130 130 _abc_invalidation_counter = 0
131 131
132 -def __new__(mcls, name, bases, namespace):
133 -cls = super().__new__(mcls, name, bases, namespace)
132 +def __new__(mcls, name, bases, namespace, **kwargs):
133 +cls = super().__new__(mcls, name, bases, namespace, **kwargs)
134 134 # Compute set of abstract method names
135 135 abstracts = {name
136 136 for name, value in namespace.items()
Original file line number Diff line number Diff line change
@@ -404,5 +404,17 @@ class C(A, B):
404 404 self.assertEqual(B.counter, 1)
405 405
406 406
407 +class TestABCWithInitSubclass(unittest.TestCase):
408 +def test_works_with_init_subclass(self):
409 +saved_kwargs = {}
410 +class ReceivesClassKwargs:
411 +def __init_subclass__(cls, **kwargs):
412 +super().__init_subclass__()
413 +saved_kwargs.update(kwargs)
414 +class Receiver(ReceivesClassKwargs, abc.ABC, x=1, y=2, z=3):
415 +pass
416 +self.assertEqual(saved_kwargs, dict(x=1, y=2, z=3))
417 +
418 +
407 419 if __name__ == "__main__":
408 420 unittest.main()
Original file line number Diff line number Diff line change
@@ -761,6 +761,9 @@ Library
761 761 - Issue #24142: Reading a corrupt config file left configparser in an
762 762 invalid state. Original patch by Florian Höch.
763 763
764 +- Issue #29581: ABCMeta.__new__ now accepts **kwargs, allowing abstract base
765 + classes to use keyword parameters in __init_subclass__. Patch by Nate Soares.
766 +
764 767 Windows
765 768 -------
766 769