canonical / canonical/cloud-init

[enhancement]: mypy - validate jsonschema against type annotations

Open
#5,398 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
3.8k
Forks
1.1k
Avg merge
2d 23h
Merged PRs (30d)
18

Description

# Enhancement

https://github.com/sbrunner/jsonschema-gentypes has the capability to generate Python types from jsonschema.

### Current state

Currently mypy is able to see that configurations are type `dict`, but is unable to know what it should expect. Cloud-init has a jsonschema which it uses for validating user inputs.

### Proposal

The proposal is to use this expected schema to validate internal usage of the objects that it has received to make sure that cloud-init's usage of configuration objects matches valid access patterns by type system.

To test out this tool:
```bash
# install
python3 -m venv .venv
. .venv/bin/activate
pip3 install jsonschema-gentypes tox -r test-requirements.txt

# generate types
jsonschema-gentypes --json-schema=./cloudinit/config/schemas/schema-cloud-config-v1.json --python=cloudinit/config/config_type.py
jsonschema-gentypes --json-schema=./cloudinit/config/schemas/schema-network-config-v1.json --python=cloudinit/config/net_one.py
jsonschema-gentypes --json-schema=./cloudinit/config/schemas/schema-network-config-v2.json --python=cloudinit/config/net_two.py

# override current "dumb" types (the v1 and v2 imports depend on a commit from this pr: https://github.com/canonical/cloud-init/pull/5350)
echo "from cloudinit.config.config_type import _Root as Config\nfrom cloudinit.config.net_one import _Root as Netv1\nfrom cloudinit.config.net_two import _Root as Netv2" > cloudinit/config/__init__.py
```

~~Once the reported issues are resolved, this would make a useful addition to CI.~~

~~[1] This tool currently has a couple of bugs that prevent its use in CI. Once https://github.com/sbrunner/jsonschema-gentypes/issues/998 is resolved, cloud-init should be able to make use of this. A [proposal](https://github.com/sbrunner/jsonschema-gentypes/pull/1046) has been made to fix these issues.~~

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.