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

Add additional type hints for calendar integration #68660

Merged
merged 2 commits into from Mar 27, 2022

Conversation

allenporter
Copy link
Contributor

@allenporter allenporter commented Mar 25, 2022

Proposed change

Add additional type hints for calendar integration as part of pre-work to implement event triggers (home-assistant/architecture#700)

Overall set of PRs for triggers:

  • Typing cleanup
  • Add initial trigger for start events (getting scaffolding all setup)
  • Add trigger for end events
  • Add offset support
  • Frontend

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:

@probot-home-assistant
Copy link

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (calendar) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

Dev automation moved this from Needs review to Reviewer approved Mar 26, 2022
@@ -97,7 +101,7 @@ def normalize_event(event):
return normalized_event


def calculate_offset(event, offset):
def calculate_offset(event: dict[str, Any], offset: str) -> dict[str, Any]:
Copy link
Member

@frenck frenck Mar 26, 2022

Choose a reason for hiding this comment

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

We should type event correctly.

Although its technically an dict[str, Any], we do define its contents and should thus should type it.

IMHO, using loose typing like this is harmful (compared to no typing at all).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to update. I have some questions to help me understand what correct looks like here.

The set of fields used by calendar is hard to follow, so that makes sense.

  1. To be a little more explicit: is that like TypedDict? Breaking change? Or feel free to point me at an example you like.

  2. One issue: integrations today add whatever fields they like to this for example, the entire Google Calendar API response is in this. Is that an issue or do we want to specify every field in the API? Or break that?

  3. Can you elaborate on why it's harmful? I'm not experienced enough with the down sides of this to have that context.

Thanks, I appreciate just a little more assistance then happy to resolve.

Copy link
Member

@frenck frenck Mar 26, 2022

Choose a reason for hiding this comment

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

The set of fields used by calendar is hard to follow, so that makes sense.

Hehe yeah, been playing with Calendar lately myself as well (added an implementation), but those fields are magical at this point (not typed, not documented). Which makes it hard to use and figure out.

To be a little more explicit: is that like TypedDict? Breaking change? Or feel free to point me at an example you like.

We should go for the best fit for this. Considering the low/limited use of this integration, I would not mind hard-breaking changes myself (to be honest). The impact would be low anyway.

I'm not entirely sure. TypeDicts could work, but they have quite a few disadvantages (especially around required/optional fields). Hence we mostly use dataclasses in Home Assistant right now.

One issue: integrations today add whatever fields they like to this for example, the entire Google Calendar API response is in this. Is that an issue or do we want to specify every field in the API? Or break that?

I don't this it's correct to have this event-free-for-all format. We could allow extra_attributes-a-like in an event (like we do with entities), but the base should be defined IMHO.

Can you elaborate on why it's harmful?

Not typing upstream, doesn't break when adding typing or making typing more strict. Adding typing everywhere and later change it, does require everything to be updated. Hence I think we should correctly type it from the get go (besides making it easier to use calendar stuff in the first place).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks that was helpful.

I agree the breaking change impact will be small as I think nothing or very little should be depending on this field. My impression is there are about 5 fields we actually would want to use in practice.

One consideration from the arch event trigger discussion: we were thinking about allowing integrations to pass arbitrary API fields in the event trigger payloads, to reduce what we need to put in the trigger schema. However maybe just having a fully specified Event schema is worth it.

Should this rise to the level of arch discussion to align on the event fields for calendar or do we just consider this cleanup given the low integration numbers for calendar?

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 need to do this change in this PR. It's a bigger change and better to do in a follow up.

Copy link
Member

Choose a reason for hiding this comment

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

The status quo would be a typeddict I would say?

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 the status quo includes arbitrary data from the Google API though, right? e.g. https://developers.google.com/calendar/api/v3/reference/events#resource via

self._event = copy.deepcopy(next(valid_items, None))
-- unless it can ignore arbitrary extra fields. It looks like most of the other integrations are more uniform and use roughly the same fields.

I'm happy to drive this cleanup as i've been cleaning up google 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.

The other related problem we have is that event, async_get_events, and the state attributes are all a little bit different in ways that are not particularly helpful at the moment.

Copy link
Member

Choose a reason for hiding this comment

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

Slept over it, I think Martin and you are right, this is a step forward. And it still needs to be handled differently IMHO, but that might be not for now. All other things in this PR as still valuable. Making steps is important too

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'll follow through on the clean up of stripping out unnecessary fields and adding in the typing. (So you won't regret the approval 🙂)

Dev automation moved this from Reviewer approved to Review in progress Mar 26, 2022
@allenporter
Copy link
Contributor Author

Sent #68724 to remove some dependencies on the event dict structure that currently add unnecessary fields.

Dev automation moved this from Review in progress to Reviewer approved Mar 27, 2022
@frenck frenck merged commit f05a682 into home-assistant:dev Mar 27, 2022
Dev automation moved this from Reviewer approved to Done Mar 27, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants