Skip to content

Protect mutable allowed_context_keys attribute in Selector classes - #181449

Merged
MartinHjelmare merged 5 commits into
devfrom
protect-selector-allowed_context_keys
Sep 7, 2026
Merged

Protect mutable allowed_context_keys attribute in Selector classes#181449
MartinHjelmare merged 5 commits into
devfrom
protect-selector-allowed_context_keys

Conversation

@jbouwh

@jbouwh jbouwh commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Proposed change

Protect mutable allowed_context_keys attribute in Selector based classes

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)
  • Deprecation (breaking change to happen in the future)
  • 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:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI balanced review requested due to automatic review settings September 6, 2026 12:31
@jbouwh
jbouwh requested a review from a team as a code owner September 6, 2026 12:31
@home-assistant home-assistant Bot added cla-signed code-quality core small-pr PRs with less than 30 lines. labels Sep 6, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The base initializer clears all specialized mappings, causing valid selector contexts to fail validation.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Moves selector context-key mappings from shared class attributes to per-instance state.

Changes:

  • Initializes default context keys per selector instance.
  • Relocates specialized mappings into three selector constructors.
File summaries
File Description
homeassistant/helpers/selector.py Refactors allowed_context_keys initialization.
Review details

Suppressed comments (1)

homeassistant/helpers/selector.py:445

  • Add a regression test that verifies each affected selector retains its expected allowed_context_keys and that mutating one instance does not affect another; the existing schema tests do not exercise this new behavior.
        self.allowed_context_keys = {
            # Filters the available attributes based on the selected entity
            "filter_entity": {"entity"}
        }
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread homeassistant/helpers/selector.py Outdated
Copilot AI review requested due to automatic review settings September 6, 2026 12:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new isolation behavior lacks regression coverage.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread homeassistant/helpers/selector.py
Copilot AI review requested due to automatic review settings September 6, 2026 13:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation is correct; the only feedback concerns non-blocking test naming clarity.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread tests/helpers/test_selector.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 6, 2026 18:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The test permanently pollutes the process-wide selector registry with a fake entry.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread tests/helpers/test_selector.py Outdated
Copilot AI review requested due to automatic review settings September 6, 2026 18:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused implementation preserves existing behavior while covering the shared-mutable-state regression.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@EetuPelkonen EetuPelkonen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot has the order backwards: all three constructors call super().init() before assigning, so nothing gets wiped. I applied this and hassfest is 0 invalid over 1515 integrations, test_selector 234 passed. The other three comments are stale.

@jbouwh

jbouwh commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Copilot has the order backwards: all three constructors call super().init() before assigning, so nothing gets wiped. I applied this and hassfest is 0 invalid over 1515 integrations, test_selector 234 passed. The other three comments are stale.

The issue that is solved here is the previous initialization of a mutable object. As it might be set in the subclass this mifht cause issues when a subclass changes the property.

@MartinHjelmare MartinHjelmare left a comment

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.

Thanks!

@MartinHjelmare
MartinHjelmare merged commit 71e6810 into dev Sep 7, 2026
53 checks passed
@MartinHjelmare
MartinHjelmare deleted the protect-selector-allowed_context_keys branch September 7, 2026 18:27
raman325 added a commit to raman325/home-assistant that referenced this pull request Sep 7, 2026
* upstream/dev: (792 commits)
  Add missing LitterHopper statuses to Whisker hopper status sensor (home-assistant#181342)
  Add configurable EnergyZero electricity price interval (home-assistant#181530)
  Bump nhc to 0.8.1 for Niko Home Control (home-assistant#181584)
  Bump aioskybell to 23.12.0 (home-assistant#181587)
  Protect mutable allowed_context_keys attribute in Selector classes (home-assistant#181449)
  Set easyEnergy quality scale to platinum (home-assistant#181378)
  Bump pyblackbird to 0.10 (home-assistant#181565)
  Bump vizaio to 0.7.0 (home-assistant#181568)
  Describe Z-Wave JS triggers for the automation editor (home-assistant#181129)
  Add ScorpionTrack last reported sensor (home-assistant#178755)
  Fix llama.cpp streaming capability reporting (home-assistant#179886)
  Report the Weheat cooling start conditions (home-assistant#181287)
  Fix swallowed exceptions in action handlers for Huawei LTE (home-assistant#181538)
  Fix swallowed exceptions in action handlers for ColorExtractor (home-assistant#181537)
  Add binary_sensor platform to NeoPool (home-assistant#180393)
  Bump vizaio to 0.6.2 (home-assistant#181562)
  Handle reauth_successful centrally (home-assistant#181154)
  Fix Vizio soundbars requiring authentication (home-assistant#181556)
  Update zizmor (home-assistant#181494)
  Fix lingering tasks in insteon properties and config tests (home-assistant#181555)
  ...

# Conflicts:
#	homeassistant/components/vizio/manifest.json
raman325 added a commit to raman325/home-assistant that referenced this pull request Sep 7, 2026
* zwave-js-conditions: (1009 commits)
  Target Z-Wave JS condition nodes with a device selector
  Import the Z-Wave JS bypass helper from its new home in the trigger tests
  Add missing LitterHopper statuses to Whisker hopper status sensor (home-assistant#181342)
  Add configurable EnergyZero electricity price interval (home-assistant#181530)
  Bump nhc to 0.8.1 for Niko Home Control (home-assistant#181584)
  Bump aioskybell to 23.12.0 (home-assistant#181587)
  Protect mutable allowed_context_keys attribute in Selector classes (home-assistant#181449)
  Set easyEnergy quality scale to platinum (home-assistant#181378)
  Bump pyblackbird to 0.10 (home-assistant#181565)
  Bump vizaio to 0.7.0 (home-assistant#181568)
  Describe Z-Wave JS triggers for the automation editor (home-assistant#181129)
  Add ScorpionTrack last reported sensor (home-assistant#178755)
  Fix llama.cpp streaming capability reporting (home-assistant#179886)
  Report the Weheat cooling start conditions (home-assistant#181287)
  Fix swallowed exceptions in action handlers for Huawei LTE (home-assistant#181538)
  Fix swallowed exceptions in action handlers for ColorExtractor (home-assistant#181537)
  Add binary_sensor platform to NeoPool (home-assistant#180393)
  Bump vizaio to 0.6.2 (home-assistant#181562)
  Handle reauth_successful centrally (home-assistant#181154)
  Fix Vizio soundbars requiring authentication (home-assistant#181556)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants