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

Enable error-code truthy-iterable [mypy 1.0] #87596

Merged
merged 1 commit into from Feb 7, 2023

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Feb 7, 2023

Proposed change

Followup to #87586

Enable truthy-iterable to detect boolean checks with Iterable. It's valid to pass a Generator which would always be true.

def transform(items: Iterable[int]) -> list[int]:
    # Error: "items" has type "Iterable[int]" which can always be true in boolean context. Consider using "Collection[int]" instead.  [truthy-iterable]
    if not items:
        return [42]
    return [x + 1 for x in items]

https://mypy.readthedocs.io/en/stable/error_code_list2.html#check-that-iterable-is-not-implicitly-true-in-boolean-context-truthy-iterable

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:

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.

To help with the load of incoming pull requests:

@cdce8p cdce8p requested a review from a team as a code owner February 7, 2023 02:10
@home-assistant home-assistant bot added cla-signed code-quality core small-pr PRs with less than 30 lines. labels Feb 7, 2023
Comment on lines 420 to 422
# handle empty list from component/platform
if not new_entities:
if not new_entities: # type: ignore[truthy-iterable]
return
Copy link
Member Author

Choose a reason for hiding this comment

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

I considered changing new_entities to Collections[Entity]. As it turns out though, quite a few integrations actually call it with a Generator. Those would need to be changed first.

In the end, it's probably not worth it here. This is an early out anyway. All cases would also be caught a few lines below with

tasks = [
self._async_add_entity(
entity, update_before_add, entity_registry, device_registry
)
for entity in new_entities
]
# No entities for processing
if not tasks:
return

Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @cdce8p 👍

../Frenck

@frenck frenck merged commit 42008c5 into home-assistant:dev Feb 7, 2023
25 checks passed
@cdce8p cdce8p deleted the mypy-truthy-iterable branch February 7, 2023 11:10
@github-actions github-actions bot locked and limited conversation to collaborators Feb 8, 2023
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.

None yet

2 participants