Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

c0rydoras
Copy link
Collaborator

No description provided.

@c0rydoras c0rydoras self-assigned this Aug 22, 2023
Copy link

@hairmare hairmare left a 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")):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

for relationship_data in data
if (
included_item := self._get_included_item(
relationship_data["type"], relationship_data["id"], included

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

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

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 :trollface:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be arranged :trollface:

    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)

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@c0rydoras c0rydoras closed this Oct 11, 2023
@c0rydoras c0rydoras reopened this Oct 11, 2023
@github-actions
Copy link
Contributor

Alex Recommends Report

Alex recommends the following language changes, but Alex is a regular expression based algorithm, so take them with a grain of salt.

✨ 🚀 ✨ Nothing to Report ✨ 🚀 ✨

@c0rydoras c0rydoras added the refactor refactor request / implementation label Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor refactor request / implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants