Skip to content

Commit

Permalink
Merge pull request #138 from microsoft/bugfix/backed-model-init-kwargs
Browse files Browse the repository at this point in the history
Bugfix/backed model initialization with arguments
  • Loading branch information
samwelkanda authored Sep 14, 2023
2 parents 95d61d4 + 8b17e43 commit af95450
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.8.4] - 2023-09-14

### Added

### Changed
- Fix error when instantiating BackedModel using positional and keyword arguments

## [0.8.3] - 2023-09-13

### Added
Expand Down
2 changes: 1 addition & 1 deletion kiota_abstractions/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION: str = "0.8.3"
VERSION: str = "0.8.4"
21 changes: 7 additions & 14 deletions kiota_abstractions/store/backed_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See License in the project root for license information.
# ------------------------------------------------------------------------------

from dataclasses import dataclass
from dataclasses import dataclass, fields

from .backing_store import BackingStore

Expand All @@ -18,22 +18,15 @@ class BackedModel:

def __post_init__(self):
self.backing_store.is_initialization_completed = True
for field in fields(self):
if field.name != "backing_store":
field_val = getattr(self, field.name)
if field_val is not field.default:
self.backing_store.set(field.name, field_val)

def __setattr__(self, prop, val):
if prop == "backing_store":
super().__setattr__(prop, val)
else:
self.backing_store.set(prop, val)

def __getattribute__(self, prop):
if prop == "backing_store":
return super().__getattribute__(prop)
if self.backing_store.get(prop) is not None:
return self.backing_store.get(prop)
try:
attr = super().__getattribute__(prop)
if callable(attr): # methods such as serialize and get_field_deserializers
return attr
return None
except AttributeError:
return None
super().__setattr__(prop, self.backing_store.get(prop))
9 changes: 4 additions & 5 deletions tests/store/test_backed_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ def test_backed_model_return_only_changed_values_false(mock_user):
def test_backed_model_return_only_changed_values_true(mock_user):
# Set the backing store to only return changed values
mock_user.backing_store.return_only_changed_values = True
# No changes have been made to the backing store, getters should return None
assert mock_user.id is None
assert mock_user.odata_type is None
assert mock_user.business_phones is None
# No changes have been made to the backing store
# enumerate should return an empty list
assert mock_user.backing_store.enumerate_() == []
# change a property value
mock_user.business_phones = ["+1 234 567 8901"]
# returns the changed value
assert mock_user.business_phones == ["+1 234 567 8901"]
assert mock_user.backing_store.enumerate_() == [('business_phones', (["+1 234 567 8901"], 1))]

0 comments on commit af95450

Please sign in to comment.