Demonstrate init subclass workaround

This commit is contained in:
Hynek Schlawack 2023-06-27 07:12:37 +02:00
parent 4461e2ce47
commit 261d26ed45
No known key found for this signature in database
GPG Key ID: AE2536227F69F181
1 changed files with 24 additions and 1 deletions

View File

@ -1,7 +1,7 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
""" """
Tests for `__init_subclass__` related tests. Tests for `__init_subclass__` related functionality.
""" """
import attr import attr
@ -41,3 +41,26 @@ def test_init_subclass_attrs():
pass pass
assert "foo" == Attrs().param 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())