Skip to content

Protect state.as_dict from mutation#65693

Merged
balloob merged 11 commits intodevfrom
protect-state-as-dict-mutation
Feb 4, 2022
Merged

Protect state.as_dict from mutation#65693
balloob merged 11 commits intodevfrom
protect-state-as-dict-mutation

Conversation

@balloob
Copy link
Copy Markdown
Member

@balloob balloob commented Feb 4, 2022

Breaking change

Proposed change

State objects cache their as_dict output. @olle69 noticed that the Tuya diagnostics was mutating this, impacting the cache.

This PR makes it immutable and fixes Tuya. Homekit Controller also mutated the dict, but first passed it through redact_data which makes a copy.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@balloob balloob added this to the 2022.2.2 milestone Feb 4, 2022
@balloob balloob requested review from a team and frenck as code owners February 4, 2022 17:34
@probot-home-assistant
Copy link
Copy Markdown

Hey there @tuya, @zlinoliver, @METISU, @frenck, mind taking a look at this pull request as it has been labeled with an integration (tuya) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@balloob balloob mentioned this pull request Feb 4, 2022
22 tasks
@balloob balloob requested review from Jc2k and bdraco as code owners February 4, 2022 17:59
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Feb 4, 2022

Will do some performance testing

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Feb 4, 2022

Hmm

Not starting up

2022-02-04 18:15:23 ERROR (MainThread) [homeassistant.components.websocket_api.messages] Unable to serialize to JSON. Bad data found at $.result[0](State: person.j_nick_koston)={'entity_id': 'person.j_nick_koston', 'state': 'not_home', 'attributes': .....'context': mappingproxy({'id': '7f293c20e52d8cf128043ac9d695e37f', 

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Feb 4, 2022

The websocket can't serialize the states with the mappingproxytype

Copy link
Copy Markdown
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

The frontend can't connect (setup) with this

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Feb 4, 2022

minor issue 🤣 I will be very surprised if the tests pass (they don't).

state_dict = None
if state:
state_dict = async_redact_data(state.as_dict(), REDACTED_STATE)
state_dict = cast(dict, async_redact_data(state.as_dict(), REDACTED_STATE))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the typing changes in cc6b0cc are wrong. async_redact_data is returning a dict, not a MappingProxyType, so the cast here is papering over a bug in async_redact_data IMO.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

async_redact_data will convert any type inheriting from Mapping to a dict. It's typed to have input = output type.

Let me see if I can fix that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Initial attempt gives me an error.

@overload
def async_redact_data(data: Mapping, to_redact: Iterable[Any]) -> dict:
    ...


@overload
def async_redact_data(data: T, to_redact: Iterable[Any]) -> T:
    ...

homeassistant/components/diagnostics/util.py:15: error: Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]
Found 1 error in 1 file (checked 2 source files)

Copy link
Copy Markdown
Member Author

@balloob balloob Feb 4, 2022

Choose a reason for hiding this comment

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

ignored that error ec868f3 🤷

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Feb 4, 2022

Argh so this is not a trivial issue to fix because the JSONEncoder in Python is hard coded to check isinstance(obj, dict). The only way to go seems to be to subclass dict such that it looks like a dict but blows up when mutated. That would probably silence typing 😬

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Feb 4, 2022

Pulling out the Tuya change into a new PR for the hot fix as this change will be a bit more elaborate.

@balloob balloob removed this from the 2022.2.2 milestone Feb 4, 2022
@balloob
Copy link
Copy Markdown
Member Author

balloob commented Feb 4, 2022

Tuya PR #65708

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Feb 4, 2022

That looks like it will be more performant as well 👍

Testing now....

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Feb 4, 2022

So far so good.. 👍

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Feb 4, 2022

Functional and performance testing looks good. Its effectively imperceptible from a performance standpoint.
Screen Shot 2022-02-04 at 13 33 05

Copy link
Copy Markdown
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

LGTM sans the typing issues

Copy link
Copy Markdown
Contributor

@marvin-w marvin-w left a comment

Choose a reason for hiding this comment

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

KNX looks good!

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Feb 4, 2022

I suggest we adopt ReadOnlyDict in future PRs in all places where we use MappingProxy

@balloob balloob merged commit 5da923c into dev Feb 4, 2022
@balloob balloob deleted the protect-state-as-dict-mutation branch February 4, 2022 22:45
@github-actions github-actions bot locked and limited conversation to collaborators Feb 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants