Skip to content

Improve comment for helpers.entity.entity_sources #146529

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

Merged
merged 1 commit into from
Jun 27, 2025

Conversation

emontnemery
Copy link
Contributor

Proposed change

Improve comment for helpers.entity.entity_sources, the function simply returns an empty dictionary and it's not clear where that dictionary is mutated.

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

  • 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.

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.

To help with the load of incoming pull requests:

@emontnemery emontnemery requested a review from a team as a code owner June 11, 2025 10:43
"""Get the entity sources."""
"""Get the entity sources.

Items are added to this dict by Entity.async_internal_added_to_hass and
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should have comments on place A saying that it is changed by B or C.
It usually makes it too easy for the comment to be stale and misleading in the future if the code changes. Without this, the developer just has to search for it or "find references", without getting mislead.

Why do you feel like this is needed? It seems easy too lookup already

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's a really weird pattern to have callers mutate a dict returned to them from a function, and it was not obvious to me that's how this is meant to be used. I would expect a bare dict in global scope instead.

You're right the comment is likely to grow stale though, maybe it can just mention callers of helpers.entity.entity_sources are expected to mutate the dict as needed?

Copy link
Member

Choose a reason for hiding this comment

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

I think this is ok. It's at least all in the same module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But now a wild PR appeared: #146549 🙈

Copy link
Member

Choose a reason for hiding this comment

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

I think it's a really weird pattern to have callers mutate a dict returned to them from a function, and it was not obvious to me that's how this is meant to be used. I would expect a bare dict in global scope instead.

True, it is weird indeed.

You're right the comment is likely to grow stale though, maybe it can just mention callers of helpers.entity.entity_sources are expected to mutate the dict as needed?

Yeah, I think describing what/how it should be done instead of where it is done would make it better.

Copy link
Member

Choose a reason for hiding this comment

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

Something like "The returned dictionary can be mutated by callers"

Copy link
Member

Choose a reason for hiding this comment

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

This is merged now, but I still believe that it should be updated to something that will not get stale and be misleading too easily. It will become stale as soon as #146549 is merged

@frenck frenck merged commit 91c3b43 into dev Jun 27, 2025
48 checks passed
@frenck frenck deleted the entity_sources_improve_comment branch June 27, 2025 18:54
@github-actions github-actions bot locked and limited conversation to collaborators Jun 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants