Skip to content

Commit 7904a25

Browse files
Initial exploration and issue reproduction
Co-authored-by: Pierre-Sassoulas <[email protected]>
1 parent fed1d3c commit 7904a25

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test_issue.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Playground file. Do not commit"""
2+
3+
from abc import ABC
4+
from collections.abc import Callable
5+
from dataclasses import dataclass, field
6+
from typing import ParamSpec
7+
8+
_P = ParamSpec("_P")
9+
10+
11+
@dataclass
12+
class Foo[T](ABC):
13+
14+
_foo: T | None = field(init=False)
15+
_bar: dict[str, str] = field(init=False)
16+
17+
def __init_subclass__(cls) -> None:
18+
def _wrap(func: Callable[_P, None]) -> Callable[_P, None]:
19+
def _w(*args: _P.args, **kwds: _P.kwargs) -> None:
20+
self = args[0]
21+
func(*args, **kwds)
22+
if not hasattr(self, "_foo"):
23+
object.__setattr__(self, "_foo", None)
24+
if not hasattr(self, "_bar"):
25+
object.__setattr__(self, "_bar", {})
26+
27+
return _w
28+
29+
cls.__init__ = _wrap(cls.__init__) # type: ignore[method-assign]
30+
31+
32+
@dataclass
33+
class Bar(Foo): ...

0 commit comments

Comments
 (0)