Skip to content

Commit

Permalink
Drop unused 'exclude_unset' argument from BaseModel.dict() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dmach committed Jan 4, 2024
1 parent 16cdc06 commit 05dad91
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 2 additions & 5 deletions osc/util/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,13 @@ def __init__(self, **kwargs):

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()

Check warning on line 333 in osc/util/models.py

View check run for this annotation

Codecov / codecov/patch

osc/util/models.py#L333

Added line #L333 was not covered by tests
else:
result[name] = getattr(self, name)
return result
14 changes: 10 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ def test_bool(self):


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"}]

Check warning

Code scanning / CodeQL

Duplicate key in dict literal Warning test

Dictionary key 'text' is subsequently
overwritten
.
self.assertEqual(m.dict(), {"a": "default", "b": "B", "sub": [{"text": "one", "text": "two"}]})

Check warning

Code scanning / CodeQL

Duplicate key in dict literal Warning test

Dictionary key 'text' is subsequently
overwritten
.

def test_unknown_fields(self):
class TestModel(BaseModel):
Expand Down

0 comments on commit 05dad91

Please sign in to comment.