IFC-3676: Leave out empty keys in schema files, keep null in schema format, parse the new schema warnings

Draft
OG
1d ago
+393-13

Why

A schema author who loads a schema file cannot remove a value stored on an attribute or relationship, such as parameters.regex. IFC-3337 reports the problem. The server change in opsmill/infrahub#11795 makes an explicit null clear the value. A key left out keeps the stored value.

This pull request makes the SDK and infrahubctl work with that server change. It is tracked in IFC-3676.

What changed

  • SchemaWarningType has two new values, ignored_value and kept_value, so the client can parse a schema load response that contains these warnings.
  • When infrahubctl or a Git repository sync reads a schema file, a key with no value, such as parameters:, is left out, as if the file did not contain it. Only a written null or ~ clears a value.
    • infrahub_sdk.yaml.SchemaLoader is the loader of SchemaFile. It drops each mapping key whose value is a plain empty scalar, at every depth, including keys copied in by a YAML merge key (<<:).
    • Object and menu files still read a key with no value as null.
    • A key tagged !!null with no value, such as regex: !!null, is read in the same way as an empty key, and is left out.
  • infrahubctl schema format:
    • keeps a written null, ~ or NULL as written, and keeps an empty value empty;
    • compares the file before and after formatting with SchemaLoader, so it stops with an error instead of turning an empty value into null or back. It stops on a flow-style mapping with an empty value, such as {name: serial, kind: Text, description: }, because it cannot write that value back as empty;
    • with --strip-defaults, keeps optional: false on attributes, because a schema load keeps the stored optional value when the file leaves out the key.

How to review

  1. infrahub_sdk/yaml.py: SchemaLoader and _is_empty_value.
  2. infrahub_sdk/ctl/schema_format.py: the null-preserving constructor and representer, and the check that uses SchemaLoader.
  3. Tests: tests/unit/sdk/test_yaml.py, tests/unit/ctl/test_schema_format.py, and tests/unit/ctl/test_schema_app.py::test_schema_load_leaves_out_keys_with_no_value, which checks the request that infrahubctl schema load sends.

How to test

uv run pytest tests/unit/sdk/test_yaml.py tests/unit/ctl/test_schema_format.py tests/unit/ctl/test_schema_app.py tests/unit/sdk/test_schema.py

Impact and rollout

  • Backward compatibility: a schema file with an empty key, such as parameters:, is now sent without that key. Against a server without the change in opsmill/infrahub#11795 the result is the same as before, because that server ignores a null.
  • Deployment notes: this pull request merges before the Infrahub feature branch in opsmill/infrahub#11795 merges into develop. That branch then moves the python_sdk submodule to the head of infrahub-develop.

Checklist

šŸ¤– Generated with Claude Code