I've been having an issue with dataclasses, despite using the decorator and passing the arguments to the dataclass, I will get a TypeError
stating that the object does not take any arguments. This seems quite temperamental and doesn't seem to be triggered by code changes, something which is working for a colleague is not working for me (but sometimes does). We're both using Python 3.9.7 and we both code with PyCharm.
I made the switch from Windows to Ubuntu in an attempt to stop this issue but after a week or so, it is happening again. Here is my last stack trace error:
Traceback (most recent call last):
File "/home/pedro/Documents/datacollect/Python-Shape-Game/main.py", line 188, in <module>
shapes = load_shapes()
File "/home/pedro/Documents/datacollect/Python-Shape-Game/main.py", line 139, in load_shapes
return [factory.create(item) for item in data["shapes"]]
File "/home/pedro/Documents/datacollect/Python-Shape-Game/main.py", line 139, in <listcomp>
return [factory.create(item) for item in data["shapes"]]
File "/home/pedro/Documents/datacollect/Python-Shape-Game/factory.py", line 28, in create
return creation_function(**arguments)
TypeError: Rectangle() takes no arguments
pygame 2.1.0 (SDL 2.0.16, Python 3.9.7)
It is using the factory / plugin pattern to register shapes from JSON, the most notable lines in factory.py being:
shape_creation_functions: dict[str, Callable[..., Shape]] = {}
# ...
creation_function = shape_creation_functions[shape_type]
return creation_function(**arguments)
Here is the object which is failing:
class Shape(Protocol):
"""Represents a shape"""
type: str
rgb: list
colour: tuple
method: str
positions: dict
radius: int
def map(self, position: str) -> Union[list[tuple[Any, Any]], tuple]:
"""Map the shape to the screen"""
@dataclass()
class Rectangle(Shape):
"""Represents a rectangle"""
type: str
rgb: list
colour: tuple
method: str
positions: dict
def map(self, position: str) -> Union[list[tuple[Any, Any]], tuple]:
"""Draw the shape on the screen"""
rect = (
self.positions[position][0],
self.positions[position][1],
self.positions[position][2],
self.positions[position][3]
)
return rect