KEMBAR78
Adjust async_step_zeroconf signature for strict typing by epenet · Pull Request #59503 · home-assistant/core · GitHub
Skip to content

Conversation

@epenet
Copy link
Contributor

@epenet epenet commented Nov 10, 2021

Proposed change

Adjust async_step_zeroconf type hint, as discussed in #58958 (comment)

cc @frenck, @bdraco

Also see home-assistant/architecture#662

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:

@epenet
Copy link
Contributor Author

epenet commented Nov 10, 2021

@frenck / @bdraco I have created an architecture discussion for this as it seems the more I fix the more underlying issues it creates. Maybe we need to set all DiscoveryInfo to be fully typed before we advance on this.
See home-assistant/architecture#662

@epenet epenet mentioned this pull request Nov 12, 2021
22 tasks

return await self.async_step_confirm()

async_step_zeroconf = async_step_discovery
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line is redundant with the base class, and actually causing mypy to crash, so had to remove it!!! See linked PR (#59576)

async def async_step_zeroconf(
self, discovery_info: DiscoveryInfoType
) -> data_entry_flow.FlowResult:
"""Handle a flow initialized by Zeroconf discovery."""
return await self.async_step_discovery(discovery_info)

Copy link
Member

Choose a reason for hiding this comment

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

Did you open in issue with mypy?

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 have not (yet) opened the issue with mypy, because I thought the line was just redundant. I will try to open an issue with them next week.
@balloob suggests that the line needs to stay there - so maybe we have to keep it and set it to ignore the type?

Or maybe there is another way to set a different signature for async_step_zeroconf (with ZeroconfServiceInfo) and async_step_discovery (DiscoveryInfoType) ?

Copy link
Member

Choose a reason for hiding this comment

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

Ignore is fine here. The alternative would be to write out the function and await the discovery handler which would add another await only to satisfy mypy which doesn't seem like a good trade off to me.

Copy link
Contributor Author

@epenet epenet Nov 13, 2021

Choose a reason for hiding this comment

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

I didn't manage to make #type: ignore work.

If I add the line back in, the I have to remove altogether the type hint in sonos component (it seems to be the only component using DiscoveryFlowHandler with zeroconf and type hints).

Is removing the type hint on async_step_zeroconf in sonos a good (at least temporary) trade off?

Copy link
Contributor Author

@epenet epenet Nov 15, 2021

Choose a reason for hiding this comment

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

If I remove the await keyword, now pylint complains with invalid-overridden-method so I have reverted the change, but I could bring it back in with pylint disable:

    def async_step_zeroconf(  # pylint: disable=invalid-overridden-method
        self,
        discovery_info: zeroconf.ZeroconfServiceInfo,
    ) -> Coroutine[Any, Any, FlowResult]:
        """Handle a flow initialized by Zeroconf discovery."""
        return self.async_step_discovery(cast(dict, discovery_info))

Copy link
Member

@frenck frenck Nov 15, 2021

Choose a reason for hiding this comment

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

last, but also ugly, but probably the best solution is code duplication, which solves the concern raised by @bdraco while shutting up all linters as well, allowing us to move forward:

    async def async_step_zeroconf(
        self, discovery_info: zeroconf.ZeroconfServiceInfo
    ) -> FlowResult:
        """Handle a flow initialized by Zeroconf discovery."""
        if self._async_in_progress() or self._async_current_entries():
            return self.async_abort(reason="single_instance_allowed")

        await self.async_set_unique_id(self._domain)

        return await self.async_step_confirm()

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 implement. @bdraco do you agree?

Copy link
Member

Choose a reason for hiding this comment

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

I agree 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great. I have updated the code. Hopefully the CI passes and we are good to go...

@epenet
Copy link
Contributor Author

epenet commented Nov 12, 2021

Hi @frenck, @bdraco,
This PR is ready for a review now.

The core changes are in homeassistant/config_entries.py and homeassistant/helpers/config_entry_flow.py.
All the other component changes are the compulsory type hint tweaks so CI doesn't break.

@epenet epenet marked this pull request as ready for review November 12, 2021 14:01
@epenet epenet requested a review from a team as a code owner November 12, 2021 14:01
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Nov 14, 2021
@epenet epenet force-pushed the zeroconf-service-info branch from fa39f52 to 4e7d366 Compare November 15, 2021 09:45
@frenck frenck removed the smash Indicator this PR is close to finish for merging or closing label Nov 15, 2021
@epenet
Copy link
Contributor Author

epenet commented Nov 15, 2021

Phew! thanks @frenck , thanks @bdraco

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