M_Jones
M_Jones

Reputation: 31

Why is dataclass is not working correctly?

The dataclass-function is not working. For example,

from dataclasses import dataclass

@dataclass
class Person:
    name: str
    age: int

p = Person("Jonas", 27)
print(p)

The error message occurs when I try to define p:

TypeError: Person() takes no arguments

Hence, dataclass is not handling the __init__ function appropriately. The dataclasses.py file is in C:\Users\Jonas\Desktop\Tut_Dataclass\test_env\Lib\site-packages (1), where I also find a folder dataclasses-0.6.dist-info. Typing where python in comand prompt (in VSCode) gives the correct location C:\Users\Jonas\Desktop\Tut_Dataclass\test_env\Scripts\python.exe. Checking sys.pathshows that (1) is listed and therefore python should find the module dataclasses. I checked numpy (installed via pip) and it works properly. As a novice in VSCode and python, I appreciate any idea to solve this issue.

Upvotes: 3

Views: 3826

Answers (1)

nozem
nozem

Reputation: 497

If restarting / reinstalling didn't work, the problem might be in the order of decorators. e.g:

@dataclass_json
@dataclass
class Person:
    name: str
    age: int

works. While:

@dataclass
@dataclass_json
class Person:
    name: str
    age: int

throws TypeError: Person() takes no arguments

Upvotes: 4

Related Questions