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

Various type hint improvements #37952

Merged
merged 7 commits into from
Jul 22, 2020
Merged

Various type hint improvements #37952

merged 7 commits into from
Jul 22, 2020

Conversation

scop
Copy link
Member

@scop scop commented Jul 18, 2020

Proposed change

Various type hint improvements.

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

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@scop scop changed the title Typing misc Various type hint improvements Jul 18, 2020
"set_default_executor",
warn_use(
loop.set_default_executor, "sets default executor on the event loop"
),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed? Looks really ugly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ugliness is subjective, but it's because of python/mypy#2427, better than # type: ignore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, guess betterness is subjective too. type ignore is a big gun, but easier to grep later and may actually be flagged automatically later as no longer needed.

Will have a look at this once the rest here has been cleared.

Copy link
Member

Choose a reason for hiding this comment

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

🤷 I'm fine either way.

Copy link
Member

Choose a reason for hiding this comment

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

setattr not triggering the same errors as assigning to a method might be fixed one day in mypy though 😉

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it could be fixed either way in mypy, or perhaps there are several related things to fix. Anyway it's good to construct our workarounds in away that they start to trigger errors when they're no longer needed, so we get to clean them up as soon as possible. And at least we know unneeded type ignores result in that, so from that POV that instead of a setattr would be preferable after all -- and at least it's more prominent. Will have a look.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed, passes locally, let's see if CI passes. Some headscratching here because the above discussed item already had a type ignore, not sure why I ended touching it in the first place.

@@ -134,7 +134,7 @@ def __init__(
self,
hass: HomeAssistant,
script: "Script",
variables: Optional[Sequence],
variables: TemplateVarsType,
Copy link
Member

Choose a reason for hiding this comment

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

This should still have Optional ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Already defined as such,

TemplateVarsType = Optional[Dict[str, Any]]

Copy link
Member

Choose a reason for hiding this comment

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

Isn't that weird to have optional be part of the "main" type. I feel like optional should be context specific.

Copy link
Member Author

@scop scop Jul 22, 2020

Choose a reason for hiding this comment

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

Gut feeling says yes, but in helpers.template where the variables by that type end up being used it is truly always optional. OTOH if there are some places where passing some variables is actually a must (can't tell offhand), then it would be good to remove optionality from the type itself.

@balloob balloob merged commit aa1c5fc into home-assistant:dev Jul 22, 2020
@scop scop deleted the typing-misc branch July 23, 2020 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants