From 261d26ed4536b8a6593f82aa6a2c3a1d7c9664d4 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Tue, 27 Jun 2023 07:12:37 +0200 Subject: [PATCH] Demonstrate init subclass workaround --- tests/test_init_subclass.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/test_init_subclass.py b/tests/test_init_subclass.py index ffd36108..cff4e948 100644 --- a/tests/test_init_subclass.py +++ b/tests/test_init_subclass.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: MIT """ -Tests for `__init_subclass__` related tests. +Tests for `__init_subclass__` related functionality. """ import attr @@ -41,3 +41,26 @@ def test_init_subclass_attrs(): pass assert "foo" == Attrs().param + + +def test_init_subclass_slots_workaround(): + """ + `__init_subclass__` works with modern APIs if care is taken around classes + existing twice. + """ + subs = {} + + @attr.define + class Base: + def __init_subclass__(cls): + subs[cls.__qualname__] = cls + + @attr.define + class Sub1(Base): + x: int + + @attr.define + class Sub2(Base): + y: int + + assert (Sub1, Sub2) == tuple(subs.values())