Skip to content

Commit 9820918

Browse files
Only the regression test
1 parent e843e53 commit 9820918

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Test for issue #10519: Crash with generic dataclass that has __init_subclass__"""
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+
"""A generic dataclass with __init_subclass__ that modifies __init__"""
14+
15+
_foo: T | None = field(init=False)
16+
_bar: dict[str, str] = field(init=False)
17+
18+
def __init_subclass__(cls) -> None:
19+
def _wrap(func: Callable[_P, None]) -> Callable[_P, None]:
20+
def _w(*args: _P.args, **kwds: _P.kwargs) -> None:
21+
self = args[0]
22+
func(*args, **kwds)
23+
if not hasattr(self, "_foo"):
24+
object.__setattr__(self, "_foo", None)
25+
if not hasattr(self, "_bar"):
26+
object.__setattr__(self, "_bar", {})
27+
28+
return _w
29+
30+
cls.__init__ = _wrap(cls.__init__) # type: ignore[method-assign]
31+
32+
33+
@dataclass
34+
class Bar(Foo):
35+
"""A subclass of the generic dataclass without type parameter"""
36+
37+
38+
@dataclass
39+
class Baz(Foo[str]):
40+
"""A subclass of the generic dataclass with type parameter"""

tests/functional/d/dataclass/dataclass_generic_init_subclass.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)