Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ValueError: mutable default <class 'list'> for field x is not alowed: use default_factory #18

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/autofaker/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ def __init__(self, cls, use_fake_data: bool = False):
self.cls = cls

def generate(self):
args = []
fields = dataclasses.fields(self.cls)
for field in fields:
params = {}
for dataclass_field in fields:
generator = TypeDataGenerator.create(
field.type, field.name, use_fake_data=self.use_fake_data
dataclass_field.type, dataclass_field.name, use_fake_data=self.use_fake_data
)
args.append((field.name, field.type, generator.generate()))
name = self.cls.__module__ + "." + self.cls.__qualname__
instance = dataclasses.make_dataclass(name, args)
params[dataclass_field.name] = generator.generate()
instance = self.cls(**params)
return instance


Expand Down
5 changes: 5 additions & 0 deletions tests/test_create_anonymous_data_classes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
import unittest
from dataclasses import dataclass
from typing import List

from autofaker import Autodata

Expand All @@ -9,6 +10,8 @@
class DataClass:
id: int
text: str
list_of_str: List[str]
list_of_int: List[int]


class AnonymousDataClassTestCase(unittest.TestCase):
Expand All @@ -28,6 +31,8 @@ def test_create_many_data_class_returns_dataclass(self):
data = Autodata.create(DataClass)
self.assertNotEqual(0, data.id)
self.assertNotEqual("", data.text)
self.assertNotEqual("", data.list_of_str[0])
self.assertNotEqual(0, data.list_of_int[0])


@dataclass
Expand Down
Loading