From b8d6c949c914bae4da64116a9bd144312432da2e Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Thu, 4 Jan 2024 08:39:06 +0100 Subject: [PATCH] Drop unused 'exclude_unset' argument from BaseModel.dict() method --- osc/util/models.py | 7 ++----- tests/test_models.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/osc/util/models.py b/osc/util/models.py index 69337cfb..125557ba 100644 --- a/osc/util/models.py +++ b/osc/util/models.py @@ -324,16 +324,13 @@ class BaseModel(metaclass=ModelMeta): self._allow_new_attributes = False - def dict(self, exclude_unset=False): + def dict(self): result = {} for name, field in self.__fields__.items(): if field.exclude: continue - if exclude_unset and field.name not in self._values and field.is_optional: - # include only mandatory fields and optional fields that were set to an actual value - continue if field.is_model: - result[name] = getattr(self, name).dict(exclude_unset=exclude_unset) + result[name] = getattr(self, name).dict() else: result[name] = getattr(self, name) return result diff --git a/tests/test_models.py b/tests/test_models.py index db1cae97..ccde8a4b 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -24,16 +24,21 @@ class TestNotSet(unittest.TestCase): class Test(unittest.TestCase): - def test_modified(self): + def test_dict(self): + class TestSubmodel(BaseModel): + text: str = Field(default="default") + class TestModel(BaseModel): a: str = Field(default="default") b: Optional[str] = Field(default=None) + sub: Optional[List[TestSubmodel]] = Field(default=None) m = TestModel() - self.assertEqual(m.dict(exclude_unset=True), {"a": "default"}) + self.assertEqual(m.dict(), {"a": "default", "b": None, "sub": None}) - m = TestModel(b=None) - self.assertEqual(m.dict(exclude_unset=True), {"a": "default", "b": None}) + m.b = "B" + m.sub = [{"text": "one"}, {"text": "two"}] + self.assertEqual(m.dict(), {"a": "default", "b": "B", "sub": [{"text": "one"}, {"text": "two"}]}) def test_unknown_fields(self): class TestModel(BaseModel):