Skip to content

Generated-model helpers

Version selection + module loading for the codegen-produced modules (dhis2w_client.generated.v{N}). The hand-written client + plugin trees use these helpers when they need typed access to a specific DHIS2 major's wire shape.

When to reach for it

  • Picking the right schema module at runtime when you don't know which DHIS2 version the connected instance is running.
  • Importing a v43-only model directly (e.g. when the v42-typed accessor doesn't surface a v43-only field — see Schema diff: v41 -> v42 -> v43).
  • Checking which generated versions a particular dhis2w-client install carries (codegen is per-version + opt-in; not every build has every tree).

Worked example — pick a generated module by version

from dhis2w_client import Dhis2
from dhis2w_client.generated import available_versions, load

# Which versions has this install committed?
print(available_versions())  # e.g. ['v41', 'v42', 'v43']

# Load one by enum.
gen_v43 = load(Dhis2.V43)
print(gen_v43.__name__)  # 'dhis2w_client.generated.v43'

# Read a specific v43 schema typed.
from dhis2w_client.generated.v43.schemas.program import Program as ProgramV43

# `Program` here is the v43-typed model with `enableChangeLog`,
# `enrollmentsLabel`, etc. — fields that don't exist on the v42 model.

Pairs with the version-aware client

The top-level Dhis2Client auto-detects the connected DHIS2 version on connect() and binds the matching generated module to client.resources, client.models, etc. Read Version-aware clients for the runtime dispatch story; the helpers here are the low-level building blocks the dispatcher uses.

generated

Version-scoped generated clients. Populated by d2w codegen.

Classes

Dhis2

Bases: StrEnum

DHIS2 major version that dhis2w-client has a generated client for.

Members mirror the directory names under dhis2w_client/generated/. Use as a typed hint when you want to pin the client to a specific version instead of letting it auto-detect via /api/system/info:

from dhis2w_client import Dhis2, Dhis2Client
async with Dhis2Client(url, auth=auth, pin_version=Dhis2.V42) as client:
    ...
Source code in packages/dhis2w-client/src/dhis2w_client/generated/__init__.py
class Dhis2(StrEnum):
    """DHIS2 major version that `dhis2w-client` has a generated client for.

    Members mirror the directory names under `dhis2w_client/generated/`. Use as a
    typed hint when you want to pin the client to a specific version instead of
    letting it auto-detect via `/api/system/info`:

        from dhis2w_client import Dhis2, Dhis2Client
        async with Dhis2Client(url, auth=auth, pin_version=Dhis2.V42) as client:
            ...
    """

    V41 = "v41"
    V42 = "v42"
    V43 = "v43"

Functions

available_versions()

Return the version keys that have been populated by codegen.

Walks the generated/ folder, imports each v\d+ subpackage, and returns only the ones whose __init__.py set GENERATED = True.

Source code in packages/dhis2w-client/src/dhis2w_client/generated/__init__.py
def available_versions() -> tuple[str, ...]:
    r"""Return the version keys that have been populated by codegen.

    Walks the `generated/` folder, imports each `v\d+` subpackage, and returns
    only the ones whose `__init__.py` set `GENERATED = True`.
    """
    here = Path(__file__).parent
    ready: list[str] = []
    for child in sorted(here.iterdir()):
        if not child.is_dir() or not _VERSION_KEY_RE.match(child.name):
            continue
        try:
            module = importlib.import_module(f"dhis2w_client.generated.{child.name}")
        except ImportError:
            continue
        if getattr(module, "GENERATED", False):
            ready.append(child.name)
    return tuple(ready)

load(version_key)

Import a specific generated version module.

Source code in packages/dhis2w-client/src/dhis2w_client/generated/__init__.py
def load(version_key: str) -> ModuleType:
    """Import a specific generated version module."""
    return importlib.import_module(f"dhis2w_client.generated.{version_key}")