Skip to content

Dataclass takes no arguments in a subinterpreter in 3.12 #110279

Closed
@peterebden

Description

@peterebden

Bug report

Bug description:

In 3.12, when I construct an instance of a dataclass in a subinterpreter, I get an "X takes no arguments" error, for cases that work fine in the main interpreter.

A fairly minimal code example:

#include <stdio.h>

#include <Python.h>

const char* code = "\n\
from dataclasses import dataclass\n\
\n\
@dataclass\n\
class MyClass:\n\
  id: str\n\
\n\
c = MyClass(id='abc')\n\
print(c)\n\
";

int main(int argc, char** argv) {
  Py_InitializeEx(0);
  printf("Python version: ");
  fflush(stdout);
  PyRun_SimpleString("import sys; print(sys.version)");
  PyThreadState* main_thread = PyThreadState_Get();

  printf("In main interpreter:\n");
  PyRun_SimpleString(code);

  PyThreadState* interpreter_thread = Py_NewInterpreter();
  printf("\nIn subinterpreter:\n");
  PyRun_SimpleString(code);
  Py_EndInterpreter(interpreter_thread);

  PyThreadState_Swap(main_thread);
  Py_Finalize();
  return 0;
}

Compiled with gcc -o py_dataclass py_dataclass.c -I /usr/include/python3.11 -lpython3.11 I get the expected:

Python version: 3.11.5 (main, Aug 25 2023, 13:19:50) [GCC 11.4.0]
In main interpreter:
MyClass(id='abc')

In subinterpreter:
MyClass(id='abc')

Compiled with gcc -o py_dataclass py_dataclass.c -I /usr/include/python3.12 -lpython3.12 I get:

Python version: 3.12.0 (main, Oct  2 2023, 15:04:50) [GCC 11.4.0]
In main interpreter:
MyClass(id='abc')

In subinterpreter:
Traceback (most recent call last):
  File "<string>", line 8, in <module>
TypeError: MyClass() takes no arguments

This is using the python3.11-dev and python3.12-dev packages from the deadsnakes PPA.

I'm a little lost about what the interaction between the subinterpreter and the dataclass is that would cause this. The non-mangled Python code is just

from dataclasses import dataclass

@dataclass
class MyClass:
  id: str

c = MyClass(id='abc')
print(c)

Let me know if there's any more info I can provide!

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Linux

Metadata

Metadata

Labels

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions