Skip to content

Replace barrel imports in hub/__init__.py with dynamic registry-based resolution #1423

@debu-sinha

Description

@debu-sinha

Context

Discussion #1412 and PR #1394 (shipped in v0.9.1) introduced the project-level
.guardrails/hub_registry.json registry. Currently guardrails hub install dual-writes:
it appends an import line to hub/__init__.py in site-packages AND writes to the JSON
registry.

The __init__.py mutation approach is fragile: it breaks on virtualenv changes, pip
upgrades, and concurrent installs. The registry was built to replace it.

@CalebCourier noted on Discussion #1412 (Mar 2): "If you have the bandwidth to open a
PR for Phase 2 it would be much appreciated."

Proposed Changes

  1. Replace guardrails/hub/__init__.py barrel imports with PEP 562 __getattr__ that
    reads from hub_registry.json, caches the export map, and resolves validators lazily
  2. Remove the add_to_hub_inits() call from the install path (registry becomes sole
    source of truth)
  3. Add unregister_validator() for clean removal on uninstall
  4. Rewrite guardrails hub list to read from registry instead of regex-parsing
    __init__.py

Backward Compatibility

  • from guardrails.hub import DetectPII works identically via __getattr__
  • Existing registry entries from v0.9.1 installs are used as-is
  • add_to_hub_inits() and remove_from_hub_inits() stay in the codebase but are no
    longer called
  • No migration command needed: reinstalling validators auto-populates the registry

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions