Update hints about type hints (#899)

This commit is contained in:
Franck Nijhof 2021-04-26 14:22:44 +02:00 committed by GitHub
parent 01f0246787
commit 247e9ba0ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View File

@ -9,6 +9,7 @@ Before you commit any changes, check your work against these requirements:
- New dependencies are added to `requirements_all.txt` (if applicable), using `python3 -m script.gen_requirements_all`
- New codeowners are added to `CODEOWNERS` (if applicable), using `python3 -m script.hassfest`
- The `.coveragerc` file is updated to exclude your platform if there are no tests available or your new code uses a third-party library for communication with the device, service, or sensor. `config_flow.py` can't be excluded as it must be fully tested (100% coverage).
- The `.no-strict-typing` file is updated to exclude your code if it doesn't provide a fully type hinted source.
- The code is formatted using Black, as per these [guidelines](https://developers.home-assistant.io/blog/2019/07/31/black.html). This can be done running the command `black --fast homeassistant tests`.
- Documentation is developed for [home-assistant.io](https://home-assistant.io/)
- Visit the [website documentation](/documenting.md) for more information about contributing to [home-assistant.io](https://github.com/home-assistant/home-assistant.io).

View File

@ -68,6 +68,11 @@ One exception is for logging which uses the percentage formatting. This is to av
_LOGGER.info("Can't connect to the webservice %s at %s", string1, string2)
```
### Typing of functions
### Typing
Either completely type a function or do not type a function at all.
We encourage the use of fully typing your code. This helps with finding/preventing issues and bugs in our codebase,
but also helps fellow contributors making adjustments to your code in the future as well.
By default, Home Assistant will assume everything has type hints and our automated CI process will also check for this.
Python modules can be excluded from this behavior by adding an entry to the `.no-strict-typing` file in the
root of the Home Assistant Core project.

View File

@ -4,7 +4,8 @@ title: "Adding type hints to your code"
Type hints in Python are static annotations of variables and functions, to let humans more easily understand the code. See the standard library [docs](https://docs.python.org/3/library/typing.html) and this PyCascades 2018 [talk](https://youtu.be/zKre4DKAB30).
Type hints are not required for all modules at the moment in Home Assistant, but we aim to have complete coverage of the core modules.
Type hints are not required for all modules at the moment in Home Assistant, but we aim to have a complete as possible coverage.
To improve and encourage this, all code is type checked in our continuous integration process and assumes everything is type checked, unless explicitly excluded from type checking.
Adding type hints to an existing codebase can be a daunting task. To speed this up and help developers doing this, Instagram made the [`monkeytype`](https://pypi.org/project/MonkeyType/) program. It will analyze calls during runtime and try to assign the correct type hints to the code.
@ -22,3 +23,9 @@ We've added a script to start a run of our test suite or a test module and tell
**Note:**
Applying a monkeytyped stub to a module that has existing typing annotations might error and not work. This tool is most useful for totally untyped modules.
### Excluding modules from type checking
While we encourage the use of type hints, we currently do not require them for our integrations.
By default, our CI assumes files do have type hints. In case the added module doesn't have this, it can be excluded by adding the module to the `.no-strict-typing` file
that is located at the root of the Home Assistant Core project.