Skip to content

Commit bfc173a

Browse files
committed
tests/cpydiff: Test for PEP487 __init_subclass__.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
1 parent df05cae commit bfc173a

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
categories: Core,Classes
3+
description: ``__init_subclass__`` isn't automatically called.
4+
cause: MicroPython does not currently implement PEP 487.
5+
workaround: Manually call ``__init_subclass__`` after class creation if needed. e.g.::
6+
7+
class A(Base):
8+
pass
9+
A.__init_subclass__()
10+
11+
"""
12+
13+
14+
class Base:
15+
@classmethod
16+
def __init_subclass__(cls):
17+
print(f"Base.__init_subclass__({cls.__name__})")
18+
19+
20+
class A(Base):
21+
pass
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
categories: Core,Classes
3+
description: ``__init_subclass__`` isn't an implicit classmethod.
4+
cause: MicroPython does not currently implement PEP 487. ``__init_subclass__`` is not currently in the list of special-cased class/static methods.
5+
workaround: Decorate declarations of ``__init_subclass__`` with ``@classmethod``.
6+
"""
7+
8+
9+
def regularize_spelling(text, prefix="bound_"):
10+
# for regularizing across the CPython "method" vs Micropython "bound_method" spelling for the type of a bound classmethod
11+
if text.startswith(prefix):
12+
return text[len(prefix) :]
13+
return text
14+
15+
16+
class A:
17+
def __init_subclass__(cls):
18+
pass
19+
20+
@classmethod
21+
def manual_decorated(cls):
22+
pass
23+
24+
25+
a = type(A.__init_subclass__).__name__
26+
b = type(A.manual_decorated).__name__
27+
28+
print(regularize_spelling(a))
29+
print(regularize_spelling(b))
30+
if a != b:
31+
print("FAIL")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
categories: Core,Classes
3+
description: Micropython doesn't support parameterized ``__init_subclass__`` class customization.
4+
cause: MicroPython does not currently implement PEP 487. The micropython syntax tree does not include a kwargs node after the class inheritance list.
5+
workaround: Use class variables or another mechanism to specify base-class customizations.
6+
"""
7+
8+
9+
class Base:
10+
@classmethod
11+
def __init_subclass__(cls, arg=None, **kwargs):
12+
cls.init_subclass_was_called = True
13+
print(f"Base.__init_subclass__({cls.__name__}, {arg=!r}, {kwargs=!r})")
14+
15+
16+
class A(Base, arg="arg"):
17+
pass
18+
19+
20+
# Regularize across Micropython not automatically calling __init_subclass__ either.
21+
if not getattr(A, "init_subclass_was_called", False):
22+
A.__init_subclass__()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
categories: Core,Classes
3+
description: ``__init_subclass__`` can't be defined a cooperatively-recursive way.
4+
cause: MicroPython does not currently implement PEP 487. The base object type does not have an ``__init_subclass__`` implementation.
5+
workaround: Omit the recursive ``__init_subclass__`` call unless it's known that the grandparent also defines it.
6+
"""
7+
8+
9+
class Base:
10+
@classmethod
11+
def __init_subclass__(cls, **kwargs):
12+
cls.init_subclass_was_called = True
13+
super().__init_subclass__(**kwargs)
14+
15+
16+
class A(Base):
17+
pass
18+
19+
20+
# Regularize across Micropython not automatically calling __init_subclass__ either.
21+
if not getattr(A, "init_subclass_was_called", False):
22+
A.__init_subclass__()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy