File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 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 ): ...
You can’t perform that action at this time.
0 commit comments