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

[App] Increased DeepDiff's verbose level to properly handle dict changes #13960

Merged
merged 4 commits into from
Aug 5, 2022

Conversation

adam-lightning
Copy link
Contributor

@adam-lightning adam-lightning commented Aug 1, 2022

What does this PR do?

Problem

While building a streamlit UI we found that if we do the following:

state.my_dict["some_key"] = "some_value"

And later trigger the status update the App will fail with something of the following error:

  File "/opt/miniconda3/envs/lai-pip/lib/python3.8/site-packages/lightning/core/lightning_app.py", line 306, in maybe_apply_changes
    state += delta
  File "/opt/miniconda3/envs/lai-pip/lib/python3.8/site-packages/deepdiff/delta.py", line 131, in __add__
    self._do_dictionary_item_added()
  File "/opt/miniconda3/envs/lai-pip/lib/python3.8/site-packages/deepdiff/delta.py", line 276, in _do_dictionary_item_added
    self._do_item_added(dictionary_item_added, sort=False)
  File "/opt/miniconda3/envs/lai-pip/lib/python3.8/site-packages/deepdiff/delta.py", line 297, in _do_item_added
    items = items.items()
AttributeError: 'list' object has no attribute 'items'

Solution

It turns out that DeepDiff on current verbose_level = 1 (default) while creating diff only records what keys were added or changed, and not recording the new value. This diff is not applicable to the state with += operation (because it's missing a new value to set) so it fails with the error above. Increasing verbose_level=2 now includes the new item value -> solves the issue.

Disclaimer: We know that just setting state.my_dict["some_key"] = "some_value" does not trigger a state update by itself (the value will not be visible outside). We need other assignment operation to do that (any use of = on state variable would do that). This PR does not address this issue at all.

Does your PR introduce any breaking changes? If yes, please list them.

Nothing I know of. But would be great if someone chimes in and verifies that changing verbose_level in all places is actually safe ;)

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

cc @Borda

@github-actions github-actions bot added the app (removed) Generic label for Lightning App package label Aug 1, 2022
@lantiga
Copy link
Collaborator

lantiga commented Aug 1, 2022

Thank you @adam-lightning, this is indeed the intended use of verbose_level (https://zepworks.com/deepdiff/current/diff.html) so I think it's safe to do this.

Can you please open a separate issue related to the assignment?

@adam-lightning adam-lightning changed the title Increased DeepDiff's verbose level to properly handle dict changes [App] Increased DeepDiff's verbose level to properly handle dict changes Aug 1, 2022
@adam-lightning adam-lightning self-assigned this Aug 1, 2022
@manskx
Copy link
Contributor

manskx commented Aug 2, 2022

Disclaimer: We know that just setting state.my_dict["some_key"] = "some_value" does not trigger a state update by itself (the value will not be visible outside). We need other assignment operation to do that (any use of = on state variable would do that). This PR does not address this issue at all.

This issue is been partially solved in https://github.com/Lightning-AI/lightning/blob/4c35867b618b4c36bfac5428756b95223f7f526a/src/lightning_app/utilities/proxies.py#L173

Copy link
Contributor

@manskx manskx left a comment

Choose a reason for hiding this comment

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

LGTM

@mergify mergify bot added the ready PRs ready to be merged label Aug 3, 2022
@mergify mergify bot requested a review from a team August 3, 2022 05:06
@carmocca carmocca added this to the app:0.6 milestone Aug 3, 2022
@mergify mergify bot added has conflicts and removed ready PRs ready to be merged labels Aug 3, 2022
@adam-lightning
Copy link
Contributor Author

Disclaimer: We know that just setting state.my_dict["some_key"] = "some_value" does not trigger a state update by itself (the value will not be visible outside). We need other assignment operation to do that (any use of = on state variable would do that). This PR does not address this issue at all.

This issue is been partially solved in

https://github.com/Lightning-AI/lightning/blob/4c35867b618b4c36bfac5428756b95223f7f526a/src/lightning_app/utilities/proxies.py#L173

Do you know if anyone have tested that observer with streamlit ? Just curious.

@mergify mergify bot added ready PRs ready to be merged and removed has conflicts ready PRs ready to be merged labels Aug 4, 2022
@adam-lightning adam-lightning enabled auto-merge (squash) August 4, 2022 15:07
Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

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

LGTM !

@adam-lightning adam-lightning merged commit 12a061f into master Aug 5, 2022
@adam-lightning adam-lightning deleted the increasing-deepdiff-verbosity branch August 5, 2022 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app (removed) Generic label for Lightning App package ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants