Skip to content

[enhancement]: mypy - validate jsonschema against type annotations  #5398

@holmanb

Description

@holmanb

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:

# 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 sbrunner/jsonschema-gentypes#998 is resolved, cloud-init should be able to make use of this. A proposal has been made to fix these issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions