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

Make FlowResult a generic type #111952

Merged
merged 5 commits into from
Mar 7, 2024
Merged

Make FlowResult a generic type #111952

merged 5 commits into from
Mar 7, 2024

Conversation

emontnemery
Copy link
Contributor

@emontnemery emontnemery commented Mar 1, 2024

Proposed change

Add generic type BaseFlowResult

Rationale:
Without this PR, FlowResult.handler is typed as str, although the handler in auth flows is a tuple[str, str]

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

home-assistant bot commented Mar 1, 2024

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.

@home-assistant
Copy link

home-assistant bot commented Mar 1, 2024

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.

Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

Left a few comments. Could you also check this case here? Should handler still accept vol.Any(str, list)?

vol.Required("handler"): vol.Any(str, list),

homeassistant/data_entry_flow.py Outdated Show resolved Hide resolved
homeassistant/components/auth/login_flow.py Outdated Show resolved Hide resolved
homeassistant/auth/models.py Outdated Show resolved Hide resolved
homeassistant/auth/__init__.py Outdated Show resolved Hide resolved
"""Create a login flow."""
auth_provider = self.auth_manager.get_auth_provider(*handler_key)
if not auth_provider:
raise KeyError(f"Unknown auth provider {handler_key}")
return await auth_provider.async_login_flow(context)

async def async_finish_flow(
self, flow: data_entry_flow.BaseFlowHandler, result: FlowResult
) -> FlowResult:
self, flow: data_entry_flow.BaseFlowHandler, result: AuthFlowResult
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
self, flow: data_entry_flow.BaseFlowHandler, result: AuthFlowResult
self, flow: LoginFlow, result: AuthFlowResult

Does this work? It would make the cast unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't work because:

homeassistant/auth/__init__.py:118: error: Argument 1 of "async_finish_flow" is incompatible with supertype "FlowManager"; supertype defines the argument type as "FlowHandler[AuthFlowResult, tuple[str, str]]"  [override]
homeassistant/auth/__init__.py:118: note: This violates the Liskov substitution principle
homeassistant/auth/__init__.py:118: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

I think mypy is being a bit unreasonable here though.
One option could be to provide the type of FlowHandler as a generic parameter to FlowManager.

Copy link
Member

Choose a reason for hiding this comment

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

One option could be to provide the type of FlowHandler as a generic parameter to FlowManager.

Wouldn't that also require higher-kind TypeVars as FlowHandler itself is generic?
I'd just leave it as is. Although nice, the current typing doesn't hurt and it's just one cast.

homeassistant/data_entry_flow.py Outdated Show resolved Hide resolved
homeassistant/data_entry_flow.py Outdated Show resolved Hide resolved
homeassistant/data_entry_flow.py Outdated Show resolved Hide resolved
homeassistant/data_entry_flow.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft March 1, 2024 16:53
@home-assistant
Copy link

home-assistant bot commented Mar 1, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@emontnemery
Copy link
Contributor Author

Left a few comments. Could you also check this case here? Should handler still accept vol.Any(str, list)?

vol.Required("handler"): vol.Any(str, list),

I missed that case, it should indeed only accept str 👍

@cdce8p
Copy link
Member

cdce8p commented Mar 5, 2024

@emontnemery Could you rebase this one?

@emontnemery emontnemery marked this pull request as ready for review March 6, 2024 13:01
@emontnemery emontnemery requested a review from a team as a code owner March 6, 2024 13:01
@home-assistant home-assistant bot requested a review from cdce8p March 6, 2024 13:01
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

This is coming together quite nicely 👍🏻
Just a few minor comments.

--
Regarding the TypeError. That looks like an issue with runtime typing and TypeVar defaults. More precisely one in typing_extensions as it doesn't overwrite the _check_generics function it ignore TypeVars with defaults when counting the generic arguments.

As we don't need runtime typing, it's easy enough to work around it. Just wrap the bound type in quotes here.

_FlowManagerT = TypeVar(
"_FlowManagerT",
bound=data_entry_flow.FlowManager[Any],
default=data_entry_flow.FlowManager,
)

I'll see if I can open an issue in typing_extensions to fix that. Should be easy enough as the fix will also be required for 3.13. So it might already be implemented upstream in which case it only needs to be ported typing_extensions.

Edit:

homeassistant/data_entry_flow.py Outdated Show resolved Hide resolved
homeassistant/auth/models.py Show resolved Hide resolved
@@ -182,7 +182,7 @@ def __init__(self) -> None:

@property
@abstractmethod
def flow_manager(self) -> FlowManager[ConfigFlowResult]:
def flow_manager(self) -> FlowManager[ConfigFlowResult, str]:
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
def flow_manager(self) -> FlowManager[ConfigFlowResult, str]:
def flow_manager(self) -> FlowManager[ConfigFlowResult]:

str is the default type so it isn't necessary.

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 don't really like this. Can the generic type be passed by name, then it's clearer what is changed from the default?

Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately not. That would have required PEP-637 (Indexing with keyword arguments).

I mentioned it somewhere else already, this is also one of the reasons why TypeVars with defaults must be after ones without. Just so it's more clear what the type argument is referring to.

As it looks now, str is necessary after all. This is also a runtime use so the typing_extension bug hits here too. At the moment, it only works if either all or no type arguments are passed to the generic.

homeassistant/config_entries.py Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft March 6, 2024 13:55
@emontnemery emontnemery marked this pull request as ready for review March 7, 2024 10:02
@home-assistant home-assistant bot requested a review from cdce8p March 7, 2024 10:02
@cdce8p cdce8p merged commit 82efb3d into dev Mar 7, 2024
53 checks passed
@cdce8p cdce8p deleted the flow-result-generic-type branch March 7, 2024 11:41
@github-actions github-actions bot locked and limited conversation to collaborators Mar 8, 2024
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.

None yet

2 participants