From aafa50d49283709eaa6cb4b3b74b073e8306cfd6 Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Sat, 18 Nov 2023 10:56:26 +0100 Subject: [PATCH 1/2] fix: ValueError: mutable default for field x is not allowed: use default_factory --- src/autofaker/generator.py | 11 +++++------ tests/test_create_anonymous_data_classes.py | 4 ++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/autofaker/generator.py b/src/autofaker/generator.py index 399d6ef..cb0bb5e 100644 --- a/src/autofaker/generator.py +++ b/src/autofaker/generator.py @@ -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 diff --git a/tests/test_create_anonymous_data_classes.py b/tests/test_create_anonymous_data_classes.py index f6133e7..fedb90e 100644 --- a/tests/test_create_anonymous_data_classes.py +++ b/tests/test_create_anonymous_data_classes.py @@ -9,6 +9,8 @@ class DataClass: id: int text: str + list_of_str: list[str] + list_of_int: list[int] class AnonymousDataClassTestCase(unittest.TestCase): @@ -28,6 +30,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 From 932ee6ddc43e689dae78ada7cd3f26131830bedf Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Sat, 18 Nov 2023 15:55:39 +0100 Subject: [PATCH 2/2] fix: compatibility with python3.7 --- tests/test_create_anonymous_data_classes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_create_anonymous_data_classes.py b/tests/test_create_anonymous_data_classes.py index fedb90e..5421fae 100644 --- a/tests/test_create_anonymous_data_classes.py +++ b/tests/test_create_anonymous_data_classes.py @@ -1,6 +1,7 @@ import dataclasses import unittest from dataclasses import dataclass +from typing import List from autofaker import Autodata @@ -9,8 +10,8 @@ class DataClass: id: int text: str - list_of_str: list[str] - list_of_int: list[int] + list_of_str: List[str] + list_of_int: List[int] class AnonymousDataClassTestCase(unittest.TestCase):