Skip to content

UpstreamSchema

🛈 DocStrings

The parts of an upstream schema the checks compare.

Source code in src/aclientp/schema_reference.py
@dataclass(frozen=True)
class UpstreamSchema:
    """The parts of an upstream schema the checks compare."""

    shapes: dict[str, Shape]
    enums: dict[str, EnumDef]
    union_members: dict[str, frozenset[str]]
    """Union name to the keys of its members (variants and referenced definitions)."""

    methods: dict[str, frozenset[str]]
    """``meta.unstable.json`` method group to method names."""

    @classmethod
    def parse(cls, schema: JSON, meta: JSON) -> UpstreamSchema:
        """Extract shapes, enums, unions and methods from the upstream files."""
        defs: dict[str, JSON] = schema["$defs"]
        shapes: dict[str, Shape] = {}
        enums: dict[str, EnumDef] = {}
        unions: dict[str, frozenset[str]] = {}
        for name, node in defs.items():
            variants = node.get("oneOf") or node.get("anyOf")
            if variants is not None and "properties" not in node:
                if enum := _parse_enum(name, variants):
                    enums[name] = enum
                    continue
                members: set[str] = set()
                for variant in variants:
                    if ref := variant.get("$ref"):
                        members.add(_ref_name(ref))
                    elif shape := _parse_variant(name, variant, defs):
                        shapes[shape.key] = shape
                        members.add(shape.key)
                    elif (parts := variant.get("allOf")) and len(parts) == 1 and "$ref" in parts[0]:
                        # Untagged variant, e.g. ``{"title": "stdio", "allOf": [{"$ref": ...}]}``
                        members.add(_ref_name(parts[0]["$ref"]))
                unions[name] = frozenset(members)
            elif "enum" in node:
                enums[name] = EnumDef(name, frozenset(node["enum"]), open=False)
            elif _is_object(node, defs):
                collected = _collect(node, defs)
                shapes[name] = Shape(
                    name,
                    collected.properties,
                    collected.required,
                    bases=collected.bases,
                    nullable=collected.nullable,
                    constraints=collected.constraints,
                )
                branches = [
                    branch
                    for part in node.get("oneOf", []) + node.get("anyOf", [])
                    if (branch := _parse_branch(name, node, part, defs))
                ]
                for branch in branches:
                    shapes[branch.key] = branch
                if branches:
                    unions[name] = frozenset(branch.key for branch in branches)
        methods = {
            group: frozenset(meta[group].values()) for group in METHOD_GROUPS if group in meta
        }
        return cls(shapes, enums, unions, methods)

    @property
    def inlined_bases(self) -> frozenset[str]:
        """Definitions that only serve as ``allOf`` bases of union variants."""
        used_as_base = {
            base for shape in self.shapes.values() if shape.union for base in shape.bases
        }
        referenced = {member for members in self.union_members.values() for member in members}
        return frozenset(used_as_base - referenced)

inlined_bases property

inlined_bases: frozenset[str]

Definitions that only serve as allOf bases of union variants.

methods instance-attribute

methods: dict[str, frozenset[str]]

meta.unstable.json method group to method names.

union_members instance-attribute

union_members: dict[str, frozenset[str]]

Union name to the keys of its members (variants and referenced definitions).

parse classmethod

parse(schema: JSON, meta: JSON) -> UpstreamSchema

Extract shapes, enums, unions and methods from the upstream files.

Source code in src/aclientp/schema_reference.py
@classmethod
def parse(cls, schema: JSON, meta: JSON) -> UpstreamSchema:
    """Extract shapes, enums, unions and methods from the upstream files."""
    defs: dict[str, JSON] = schema["$defs"]
    shapes: dict[str, Shape] = {}
    enums: dict[str, EnumDef] = {}
    unions: dict[str, frozenset[str]] = {}
    for name, node in defs.items():
        variants = node.get("oneOf") or node.get("anyOf")
        if variants is not None and "properties" not in node:
            if enum := _parse_enum(name, variants):
                enums[name] = enum
                continue
            members: set[str] = set()
            for variant in variants:
                if ref := variant.get("$ref"):
                    members.add(_ref_name(ref))
                elif shape := _parse_variant(name, variant, defs):
                    shapes[shape.key] = shape
                    members.add(shape.key)
                elif (parts := variant.get("allOf")) and len(parts) == 1 and "$ref" in parts[0]:
                    # Untagged variant, e.g. ``{"title": "stdio", "allOf": [{"$ref": ...}]}``
                    members.add(_ref_name(parts[0]["$ref"]))
            unions[name] = frozenset(members)
        elif "enum" in node:
            enums[name] = EnumDef(name, frozenset(node["enum"]), open=False)
        elif _is_object(node, defs):
            collected = _collect(node, defs)
            shapes[name] = Shape(
                name,
                collected.properties,
                collected.required,
                bases=collected.bases,
                nullable=collected.nullable,
                constraints=collected.constraints,
            )
            branches = [
                branch
                for part in node.get("oneOf", []) + node.get("anyOf", [])
                if (branch := _parse_branch(name, node, part, defs))
            ]
            for branch in branches:
                shapes[branch.key] = branch
            if branches:
                unions[name] = frozenset(branch.key for branch in branches)
    methods = {
        group: frozenset(meta[group].values()) for group in METHOD_GROUPS if group in meta
    }
    return cls(shapes, enums, unions, methods)

:fa-brands-github: Show source on GitHub