File tree Expand file tree Collapse file tree 2 files changed +47
-4
lines changed
Lib/test/test_dataclasses Expand file tree Collapse file tree 2 files changed +47
-4
lines changed Original file line number Diff line number Diff line change @@ -185,10 +185,21 @@ Module contents
185185 - *slots *: If true (the default is ``False ``), :attr: `~object.__slots__ ` attribute
186186 will be generated and new class will be returned instead of the original one.
187187 If :attr: `!__slots__ ` is already defined in the class, then :exc: `TypeError `
188- is raised. Calling no-arg :func: `super ` in dataclasses using ``slots=True `` will result in
189- the following exception being raised:
190- ``TypeError: super(type, obj): obj must be an instance or subtype of type ``.
191- The two-arg :func: `super ` is a valid workaround. See :gh: `90562 ` for full details.
188+ is raised.
189+
190+ .. warning ::
191+ Calling no-arg :func: `super ` in dataclasses using ``slots=True ``
192+ will result in the following exception being raised:
193+ ``TypeError: super(type, obj): obj must be an instance or subtype of type ``.
194+ The two-arg :func: `super ` is a valid workaround.
195+ See :gh: `90562 ` for full details.
196+
197+ .. warning ::
198+ Passing parameters to a base class :meth: `~object.__init_subclass__ `
199+ when using ``slots=True `` will result in a :exc: `TypeError `.
200+ Either use ``__init_subclass__ `` with no parameters
201+ or use default values as a workaround.
202+ See :gh: `91126 ` for full details.
192203
193204 .. versionadded :: 3.10
194205
Original file line number Diff line number Diff line change @@ -3560,6 +3560,38 @@ class A(WithDictSlot): ...
35603560 self .assertEqual (A ().__dict__ , {})
35613561 A ()
35623562
3563+ @support .cpython_only
3564+ def test_slots_with_wrong_init_subclass (self ):
3565+ # TODO: This test is for a kinda-buggy behavior.
3566+ # Ideally, it should be fixed and `__init_subclass__`
3567+ # should be fully supported in the future versions.
3568+ # See https://github.com/python/cpython/issues/91126
3569+ class WrongSuper :
3570+ def __init_subclass__ (cls , arg ):
3571+ pass
3572+
3573+ with self .assertRaisesRegex (
3574+ TypeError ,
3575+ "missing 1 required positional argument: 'arg'" ,
3576+ ):
3577+ @dataclass (slots = True )
3578+ class WithWrongSuper (WrongSuper , arg = 1 ):
3579+ pass
3580+
3581+ class CorrectSuper :
3582+ args = []
3583+ def __init_subclass__ (cls , arg = "default" ):
3584+ cls .args .append (arg )
3585+
3586+ @dataclass (slots = True )
3587+ class WithCorrectSuper (CorrectSuper ):
3588+ pass
3589+
3590+ # __init_subclass__ is called twice: once for `WithCorrectSuper`
3591+ # and once for `WithCorrectSuper__slots__` new class
3592+ # that we create internally.
3593+ self .assertEqual (CorrectSuper .args , ["default" , "default" ])
3594+
35633595
35643596class TestDescriptors (unittest .TestCase ):
35653597 def test_set_name (self ):
You can’t perform that action at this time.
0 commit comments