Skip to content

Versioned Documents

Document types for each EnergyPlus release, the check that narrows a document to one, and the types of the release arguments the loaders take. See Typed for a Release for how to use them.

idfkit.versioned

Release document types

Every supported release has a document type, a range type from it onward and a range type before it. They are generated, one per release, and differ only in the release they name, so they are summarised here rather than rendered one by one.

Name A document of Example
IDFDocumentV<major>_<minor> exactly that release IDFDocumentV9_4
IDFDocumentSince<major>_<minor> that release or any later one IDFDocumentSince22_1
IDFDocumentBefore<major>_<minor> any release earlier than that one IDFDocumentBefore24_1

The releases run from 8.9 to 26.2, the ones in Versions. IDFDocumentV26_2, the latest, is IDFDocument itself. IDFDocumentSince8_9 covers every release, and there is no IDFDocumentBefore8_9.

For a type checker each IDFDocumentV<major>_<minor> is a class carrying that release's object types and fields; at run time it is a stand-in that as_version checks against. A range type is the union of the releases it covers.

idfkit.versioned.AnyIDFDocument = IDFDocumentAllVersions[Strict] module-attribute

A document of any supported EnergyPlus version.

IDFDocument, typed for the latest release, and every older release's type in idfkit.versioned derive from it, so annotate with it to accept a document whatever its version. Through it, a document offers what every supported version has: the object types present in all of them, each typed as all of them declare it, so a field whose choices changed between releases reads as the union of them, accepts only their intersection, and a field some release lacks is not offered.

"Space" in doc does not change this type: it is true when the document holds a Space, and false for a release without the type and for one that has it but holds none, so it says nothing about the release. Narrow with idfkit.versioned.as_version to reach what only some releases have.

idfkit.versioned.as_version(doc, document_type=None, *, since=None, before=None)

as_version(
    doc: AnyIDFDocument, document_type: type[_D]
) -> _D
as_version(
    doc: AnyIDFDocument, document_type: tuple[type[_D], ...]
) -> _D
as_version(
    doc: AnyIDFDocument,
    *,
    since: ReleaseBound | None = None,
    before: ReleaseBound | None = None,
) -> AnyIDFDocument

Type doc as a document of the EnergyPlus version document_type stands for, or one of several.

Or, given since and/or before instead, as a document of any supported release from since on and before before.

The stub in versioned.pyi documents this function; this is its run-time check.

Source code in idfkit/_document_types.py
def as_version(
    doc: AnyIDFDocument,
    document_type: type[_D] | tuple[type[_D], ...] | None = None,
    *,
    since: ReleaseBound | None = None,
    before: ReleaseBound | None = None,
) -> _D | AnyIDFDocument:
    """Type *doc* as a document of the EnergyPlus version *document_type* stands for, or one of several.

    Or, given *since* and/or *before* instead, as a document of any supported release from *since*
    on and before *before*.

    The stub in ``versioned.pyi`` documents this function; this is its run-time check.
    """
    ranged = since is not None or before is not None
    if document_type is not None and ranged:
        msg = "as_version takes a document type or since=/before=, not both"
        raise TypeError(msg)
    if ranged:
        _check_range(doc, since, before)
        return doc
    if document_type is None:
        msg = "as_version needs a document type, or since= or before="
        raise TypeError(msg)
    candidates = _candidates(document_type)
    if not candidates:
        msg = "as_version needs at least one document type"
        raise TypeError(msg)
    resolved = [_resolve(candidate) for candidate in candidates]
    # A type argument states the document's strictness (``IDFDocumentV9_4[Literal[False]]``), which
    # the type then promises through ``doc.strict``: refuse a document that has the other one, as the
    # loaders and ``new_document`` refuse to create it. Release and strictness are matched against
    # the same member, so a member's strictness never vouches for another member's release.
    stated = [_stated_strictness(candidate) for candidate in candidates]
    release = find_closest_version(doc.version)
    release_matches = [required is None or required == release for _, required in resolved]
    if any(
        matches and (value is None or value == doc.strict)
        for matches, value in zip(release_matches, stated, strict=True)
    ):
        return cast("_D", doc)
    if any(release_matches):
        msg = f"The document has strict={doc.strict}, so it cannot be typed as {document_type!r}"
        raise TypeError(msg)
    if len(resolved) == 1:
        origin, required = resolved[0]
        expected = f"not {version_string(cast('tuple[int, int, int]', required))}"
        typed_as = origin.__name__
    else:
        releases = ", ".join(version_string(cast("tuple[int, int, int]", required)) for _, required in resolved)
        expected = f"not one of {releases}"
        typed_as = " | ".join(origin.__name__ for origin, _ in resolved)
    msg = f"The document is EnergyPlus {version_string(doc.version)}, {expected}, so it cannot be typed as {typed_as}"
    raise TypeError(msg)

idfkit.versioned.DocumentTypeTarget = type[AnyIDFDocument] | tuple[type[AnyIDFDocument], Unpack[tuple[type[AnyIDFDocument], ...]]] module-attribute

idfkit.versioned.ReleaseBound = type[AnyIDFDocument] | str | tuple[int, int] | tuple[int, int, int] module-attribute

One end of a range of releases, as since= and before= take it.

A release's document type (IDFDocumentV9_6), its number ("9.6") or its version tuple ((9, 6) or (9, 6, 0)). since= includes the release it names and before= excludes it.

idfkit.versioned.ReleaseDocumentT = release_type_var('ReleaseDocumentT', IDFDocumentV8_9, IDFDocumentV9_0, IDFDocumentV9_1, IDFDocumentV9_2, IDFDocumentV9_3, IDFDocumentV9_4, IDFDocumentV9_5, IDFDocumentV9_6, IDFDocumentV22_1, IDFDocumentV22_2, IDFDocumentV23_1, IDFDocumentV23_2, IDFDocumentV24_1, IDFDocumentV24_2, IDFDocumentV25_1, IDFDocumentV25_2, IDFDocumentV26_1, IDFDocumentV26_2) module-attribute

A type variable that is exactly one release's document type.

Constrained to IDFDocumentV8_9 ... IDFDocument, so type[ReleaseDocumentT] accepts one release's type and refuses AnyIDFDocument, which names no single release. A union of release types names none either: mypy refuses it, but pyright tries each member against the constraints and accepts it, so check at run time too. idfkit.migration.migrate takes its target this way, and raises TypeError for a union.