Skip to content

Load CLI subcommands from entry points#449

Open
Seth Fitzsimmons (mojodna) wants to merge 9 commits intodevfrom
cli-entry-points
Open

Load CLI subcommands from entry points#449
Seth Fitzsimmons (mojodna) wants to merge 9 commits intodevfrom
cli-entry-points

Conversation

@mojodna
Copy link
Collaborator

@mojodna Seth Fitzsimmons (mojodna) commented Feb 23, 2026

Summary

Refactor the CLI so installed packages contribute subcommands via setuptools entry points, then extract validate into a standalone overture-schema-validation package to prove the mechanism.

  • The cli() group discovers subcommands from the overture.schema.cli entry point group at import time
  • overture-schema-validation registers the validate subcommand through an entry point -- no CLI code changes needed
  • Shared model-filtering and union-construction logic moves to overture-schema-core to avoid cross-dependencies

Motivation

The CLI package was accumulating domain-specific command implementations as direct dependencies. Entry-point-based plugin loading inverts this: command packages depend on core or system, and the CLI discovers them at runtime. Adding a new subcommand (e.g. generate) means adding a package with an entry point -- no changes to the CLI.

Entry point registration

Packages register subcommands by declaring an entry point in pyproject.toml:

[project.entry-points."overture.schema.cli"]
validate = "overture.schema.validation.commands:validate"

The value points to a Click command object. Built-in commands (json-schema, list-types) take precedence on name collision; broken plugins warn to stderr without crashing.

Squash commit message

Load CLI subcommands from entry points

The cli() group discovers subcommands from the
"overture.schema.cli" entry point group at import time.
Built-in commands take precedence on name collision;
broken plugins warn to stderr without crashing.

Extract validate into overture-schema-validation, which
registers via entry point. Move filter_models(),
resolve_types(), and union-building helpers to
overture-schema-core so both packages share them
without cross-dependency.

Test plan

  • make check passes (pytest + ruff + mypy across workspace)
  • overture-schema --help lists validate when validation package is installed
  • overture-schema validate works end-to-end
  • Built-in commands (json-schema, list-types) work independently of validation package
  • Plugin loading integration tests cover discovery, broken-plugin resilience, and name collision

Every other package had its tests/ directory in
pythonpath; cli was the only one missing, making
its conftest imports rely on rootdir discovery.
Extracts filter_models() and resolve_types() into core/discovery.py.
Union-building helpers (create_union_type_from_models, _discriminated_union,
_can_discriminate, _type_literal) go to a new core/union.py module.

ModelDict and UnionType type aliases live alongside the functions that
produce them (discovery.py and union.py respectively).

Both CLI and the upcoming validation package import these from core,
avoiding a cross-dependency.
The cli() group discovers and loads subcommands from the
"overture.schema.cli" entry point group at import time.
Built-in commands (json-schema, list-types, validate) are
registered first via decorators; plugins that collide with
built-in names are skipped.

Broken plugins emit a warning to stderr and don't prevent
the CLI from starting.
Empty package with entry point registration for the
"validate" subcommand in the "overture.schema.cli" group. Code
will be moved from overture-schema-cli in subsequent commits.
Moves validate command, type_analysis, error_formatting,
data_display to the new package. types.py retains only
ValidationErrorDict and ErrorLocation (ModelDict and UnionType
are in core).

output.py (rewrap) is deliberately duplicated -- both packages
keep a copy. A future CLI helper package will consolidate
shared utilities.

The validate command is now loaded by the CLI via entry point.
CLI package is stripped down to built-in commands (list-types,
json-schema).
Validation test files (type_analysis, error_formatting, data_display,
heterogeneous_collections) move to the validation package. Validate
command and function tests extracted from CLI test modules.

CLI retains list-types, json-schema, and plugin loading tests.
Drop pyyaml and yamlcore (validation-only). Declare
overture-schema-system explicitly (used by json-schema command).
Verify validate appears in --help and works end-to-end
when the validation package is installed.
Remove dead code, extract duplicated patterns, and tighten
types across cli, core, and validation packages.

Deduplication:
- Merge _format_nested_path / format_path into one formatter
- Extract _filter_union_markers for repeated structural
  analysis pattern in error_formatting
- Extract get_effective_message for repeated ctx["error"]
  preference pattern
- Replace 5 manual dict-building sites with defaultdict

API cleanup:
- Remove use_overture_types bool from filter_models /
  resolve_types; callers pass namespace="overture" directly
- Remove unused get_registered_model
- Remove dead show_feature_data parameter and its branch

Type correctness:
- handle_generic_error -> NoReturn (was -> None)
- Union type truthiness checks -> explicit is not None
- cast(str, _type_literal(f)) -> assertion
- hasattr(metadata, "discriminator") -> getattr check
  (FieldInfo always has the attr, defaulting to None)
- Nested union detection narrowed from get_origin is not
  None to explicit Union type check
- Hardcoded context_size replaced with DEFAULT_CONTEXT_SIZE

filter_models ValueError now includes the filter parameters
in the message for easier diagnosis of typos.
description = "Validation command for Overture Maps schema CLI"
dynamic = ["version"]
license = "MIT"
name = "overture-schema-validation"

Choose a reason for hiding this comment

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

Can we have a slight restructuring of the package names?

Current:

  • overture-schema-addresses-theme
  • overture-schema-addresses-theme
  • overture-schema-base-theme
  • overture-schema-cli
  • overture-schema-validation
  • ...

Proposed:

  • overture-schema-theme-addresses
  • overture-schema-theme-base
  • overture-schema-cli
  • overture-schema-cli-validation
  • ...

This will keep the related packages visually grouped

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I intentionally named this -validation; code generation has a minor validation requirement (we want to ensure that the TOML examples are valid) and I can imagine pulling in the Parquet file validation from #437. I also intend this to be a library for validation, not just the CLI.

For the theme packages, let's stick that on the WG agenda for broader discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants