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 generic classes BaseFlowHandler and BaseFlowManager #111814

Merged
merged 5 commits into from
Feb 29, 2024

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Feb 29, 2024

Proposed change

This is the first of a set of PRs which splits up #105727 in managable pieces:

Rationale: FlowResult is currently a union of the results needed by all classes derived from FlowHandler, and in some cases, derived classes knowingly violate the typing of FlowResult

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
  • 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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@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 (auth) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of auth can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign auth Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@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 (repairs) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of repairs can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign repairs Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@@ -241,6 +241,9 @@ class OperationNotAllowed(ConfigError):
}


ConfigFlowResult = FlowResult
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When all config flows are migrated, this will be changed to a sub class of FlowResult which adds minor_version and version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When all config flows are updated, this will be updated so it's only allowed to return ConfigFlowResult from ConfigFlow and OptionsFlow methods

@emontnemery emontnemery requested a review from a team as a code owner February 29, 2024 08:15
@@ -707,7 +717,7 @@ def _async_update_entry(self, data: dict[str, Any]) -> None:

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a different result for options flows?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we should probably have that, but there are a handful of integrations which use the same base class for config and option flows and I couldn't work out a way to make the type checker accept it.

Should we give it another try before merging this PR or do you think it's OK to either let config and option flow share the return type or try to work it out 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.

We can do it in a follow up.

self._handler_progress_index: dict[str, set[BaseFlowHandler]] = {}
self._init_data_process_index: dict[type, set[BaseFlowHandler]] = {}

_flow_result: Callable[..., _FlowResultT]
Copy link
Member

Choose a reason for hiding this comment

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

Where did Marc's comment go?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_flow_result: Callable[..., _FlowResultT]
_flow_result: type[_FlowResultT]

Copy link
Member

Choose a reason for hiding this comment

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

And move it above the init method.

Copy link
Member

Choose a reason for hiding this comment

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

Where did Marc's comment go?

type[_FlowResultT] would be ideal / the correct annotation, however mypy does appear to have an issue with it. Will need to investigate that one. That's why I deleted my initial comment (also thought I had only staged it in the first place - well guess I pressed the wrong button 😅)

Moving it above the __init__ method does still make sense though.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! 👍

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 already tried the type version, but mypy does not like it like Marc suggests:

homeassistant/data_entry_flow.py:495: error: Cannot instantiate type "Type[FlowResult]"  [misc]

We could add a comment explaining type would be preferred but is not allowed by mypy? Another alternative would be to type it as type and override the resulting errors. That's somewhat annoying though because child classes would then also need to silence errors if instantiating.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW, this is the only related mypy issue I could find when I first ran into the problem: python/mypy#12425, but that's about a nested type. In our case the type is from a typevar, not sure if it's the same problem or?

Copy link
Member

Choose a reason for hiding this comment

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

type[X] and Callable[..., X] are basically equivalent in the return type. The only difference being that type[X] also checks the init args.

I think it's fine to leave Callable for now. Feel free to add a comment though that type would be preferred. We can adjust it if / when mypy is fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Found the mypy issue for it: python/mypy#11644
And here is the fix: python/mypy#16963. Should be in the release after the next one, i.e. 1.10.

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.

3 participants