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 type hints to homeassistant.auth #15853

Merged
merged 4 commits into from
Aug 16, 2018
Merged

Add type hints to homeassistant.auth #15853

merged 4 commits into from
Aug 16, 2018

Conversation

scop
Copy link
Member

@scop scop commented Aug 6, 2018

Description:

Adds type hints to homeassistant.auth.

Checklist:

  • The code change is tested and works locally. (partial)
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New or updated dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works. (typing tests extended)

@scop scop requested a review from a team as a code owner August 6, 2018 08:43
@ghost ghost added the in progress label Aug 6, 2018
@awarecan
Copy link
Contributor

awarecan commented Aug 7, 2018

I am not sure we are in a good position to refactor auth since it is still under active development. What is our strategy for typing stuff?

@scop
Copy link
Member Author

scop commented Aug 7, 2018

I'm not sure I understand what you mean by refactoring -- in my opinion the changes this PR don't really do that (with maybe the change to use namedtuple instead of dict for user meta being an exception).

Anywat, I think having typing there while it's in active development would be especially beneficial, as it can prevent some issues before they reach end user systems. That's the reason I picked auth as the first thing to extend type hint coverage to.

But sure, hearing what the overall strategy and desirability for typing stuff in the project would be cool. Personally, I would be willing to help extend it. I actually already have some more work done on that front that I haven't submitted yet, would like to get this one out of the way first.

@awarecan
Copy link
Contributor

awarecan commented Aug 7, 2018

Auth is under "active" development means I have several PRs in the pipe line, and few others haven't open PR yet. Accepting your PR means lots of merging I need to deal with.

For the typing in general, I don't know what other people thinking, but I personally like a parameter has typing when its type is some Class, but not a dict or list. For example

async def auth_manager_from_config(hass, provider_configs)

I like change it to

async def auth_manager_from_config(
        hass: HomeAssistant, provider_configs) -> AuthManager

But not your version

async def auth_manager_from_config(
        hass: HomeAssistant,
        provider_configs: List[Dict[str, Any]]) -> AuthManager

Anyway, I suggest you open an issue in https://github.com/home-assistant/architecture/issues to collect more input on this topic.

@balloob
Copy link
Member

balloob commented Aug 8, 2018

I think that right now because we have so many PRs open, adding types to it would be a pain for merge conflicts.

Once the dust has settled a bit, I do want to have types on the auth code.

@scop
Copy link
Member Author

scop commented Aug 8, 2018 via email

@scop
Copy link
Member Author

scop commented Aug 8, 2018

Having type hints there already now would be beneficial for the ongoing work. At the extreme other end, for code that is ready and not being developed and there are no new things are interfacing with it, it doesn't matter much at all whether there are type hints or not. The earlier they are introduced, the more useful they are.

@@ -47,22 +47,24 @@
class AuthManager:
"""Manage the authentication for Home Assistant."""

def __init__(self, hass, store, providers):
def __init__(self, hass: HomeAssistant, store: auth_store.AuthStore,
providers: Dict[Tuple[str, Optional[str]], AuthProvider]) \
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe create a type alias for Dict[Tuple[str, Optional[str]], AuthProvider]?

id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex))
is_owner = attr.ib(type=bool, default=False)
is_active = attr.ib(type=bool, default=False)
system_generated = attr.ib(type=bool, default=False)

# List of credentials of a user.
credentials = attr.ib(type=list, default=attr.Factory(list), cmp=False)
credentials = attr.ib(type=list, default=attr.Factory(list), cmp=False) # type: List[Credentials] # noqa: E501 pylint: disable=line-too-long
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid line-too-long where possible.
Here I think you can break after (

@scop scop mentioned this pull request Aug 9, 2018
2 tasks
@balloob
Copy link
Member

balloob commented Aug 14, 2018

We're now at a point where I don't expect any major changes to happen anymore.

@scop
Copy link
Member Author

scop commented Aug 16, 2018

Rebased against dev.

@@ -173,11 +198,17 @@ def __init__(self, hass):
if 'jwt_key' not in rt_dict:
continue

created_at = dt_util.parse_datetime(rt_dict['created_at'])
if created_at is None:
Copy link
Member

Choose a reason for hiding this comment

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

When can this happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

When homeassistant.util.dt.DATETIME_RE does not match the input. Probably not that all often in practice, but parse_datetime can return None and is declared to do so by using Optional, so we need to appease type checks and prevent assigning the possible None to (the non-Optional) RefreshToken.created_at.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably change it to raise value error. Fail loudly

@balloob balloob merged commit 649f17f into home-assistant:dev Aug 16, 2018
@ghost ghost removed the in progress label Aug 16, 2018
@scop scop deleted the typing-auth branch August 17, 2018 15:18
@balloob balloob mentioned this pull request Aug 29, 2018
girlpunk pushed a commit to girlpunk/home-assistant that referenced this pull request Sep 4, 2018
* Always load users in auth store before use

* Use namedtuple instead of dict for user meta

* Ignore auth store tokens with invalid created_at

* Add type hints to homeassistant.auth
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
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.

5 participants