-
Notifications
You must be signed in to change notification settings - Fork 1
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
Refactor BaseModel._deserialize
#16
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks like it reduces the complexity of _deserialize()
method quite a bit!
one downside i see is that the new _get_included_item()
is still tightly coupled to _deserialize()
. this means it isn't easy to implement an isolated unit test for the new method.
relationships = item.get("relationships") | ||
if not relationships: | ||
|
||
if not (relationships := item.get("relationships")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for relationship_data in data | ||
if ( | ||
included_item := self._get_included_item( | ||
relationship_data["type"], relationship_data["id"], included |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason why it's not using relationship_data.get("type")
you seem to primarily use get()
up until here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about this?
@@ -43,6 +43,21 @@ def __init__(self, client) -> None: | |||
relationships: list[tuple] | |||
filters: list[tuple] | |||
|
|||
def _get_included_item(self, item_type: str, item_id, included: dict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good job, this method doesn't contain a single walrus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That can be arranged
def _get_included_item(self, item_type: str, item_id, included: dict):
if not (included_item:= next(
(
included_item
for included_item in included
if included_item.get("type") == item_type
and included_item.get("id") == item_id
),
None,
)):
return None
included_model = self.client._type_map[item_type](self.client)
return included_model._deserialize(included_item, included)
if not included_item: | ||
return None | ||
included_model = self.client._type_map[item_type](self.client) | ||
return included_model._deserialize(included_item, included) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_deserialize()
used to fully contain the recursion logic, so the stack looked something like so:
_deserialize()
_deserialize()
_deserialize()
- ...
with this change the stack will look more like this:
_deserialize()
_get_included_item()
_deserialize()
_get_included_item()
_deserialize()
_get_included_item()
- ...
is this a trade off, you're willing to make?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
Alex Recommends ReportAlex recommends the following language changes, but Alex is a regular expression based algorithm, so take them with a grain of salt. ✨ 🚀 ✨ Nothing to Report ✨ 🚀 ✨ |
bd9fe1d
to
7586f6c
Compare
7586f6c
to
dd9f8de
Compare
No description provided.