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 return type to json_loads #85672

Merged
merged 18 commits into from
Feb 7, 2023
Merged

Add return type to json_loads #85672

merged 18 commits into from
Feb 7, 2023

Conversation

epenet
Copy link
Contributor

@epenet epenet commented Jan 11, 2023

Proposed change

I have read this thread around "Define a JSON type" python/typing#182 and I think that now that mypy supports recursive type, it might make sense to introduce this type to the Home Assistant code base.

Linked to #85603

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
  • 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.

To help with the load of incoming pull requests:

@epenet epenet requested a review from a team as a code owner January 11, 2023 08:35
@home-assistant home-assistant bot added cla-signed core new-feature small-pr PRs with less than 30 lines. labels Jan 11, 2023
@epenet
Copy link
Contributor Author

epenet commented Jan 11, 2023

Note: it might not fit every situation.
It might force the user to add instance checks assert isinstance(json_data["value"], dict) before calling json_data["value"]["sub_value"]

This comment python/typing#182 (comment) has a nice example of when using it will be awkward.

If it returns JSONType then the first thing any code needs to do is to run an isinstance check for the returned value, as due to being a union, most operations won't be valid on it. However, in some cases it's arguably okay to assume that the returned value is a dict, for example. If this an internal data file, we can be reasonably sure that the format is what we expect. I didn't think about object_hook but that might be another thing to consider.

If a user wants to do the type check they can add an annotation if the return type is Any:

data = json.load(...)  # type: JSONType
if isinstance(data, dict): 
    ...

This, for example, would be rejected if the return type is a union, but would be fine if the return type is Any:

data = json.load(...)  # type: Dict[str, Any]   # error if load() return type is union
...

@epenet epenet closed this Jan 31, 2023
@epenet epenet reopened this Jan 31, 2023
@epenet
Copy link
Contributor Author

epenet commented Jan 31, 2023

Rebased over #86419

@epenet epenet changed the title Add JSON type definitions Add JSON type definition Feb 7, 2023
@epenet epenet changed the title Add JSON type definition Add return type to json_loads Feb 7, 2023
jbouwh
jbouwh previously approved these changes Feb 7, 2023
Copy link
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

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

Looks good to me,
Thnx @epenet !

@epenet epenet dismissed jbouwh’s stale review February 7, 2023 10:40

Stale following json_loads_object

@epenet epenet requested a review from jbouwh February 7, 2023 11:41
Copy link
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

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

Looks good to me,
Thnx @epenet !

Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@epenet epenet merged commit a202588 into home-assistant:dev Feb 7, 2023
@epenet
Copy link
Contributor Author

epenet commented Feb 7, 2023

Thanks @emontnemery , thanks @jbouwh 👍

@epenet epenet deleted the Json-Type branch February 7, 2023 16:22
@github-actions github-actions bot locked and limited conversation to collaborators Feb 8, 2023
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.

3 participants