Skip to content

gds.spaces

Bases: BaseModel

A typed product space — defines the shape of data flowing between blocks.

Each field in the schema maps a name to a TypeDef. validate_data() checks a data dict against the schema, returning a list of error strings.

Source code in packages/gds-framework/gds/spaces.py
class Space(BaseModel):
    """A typed product space — defines the shape of data flowing between blocks.

    Each field in the schema maps a name to a TypeDef. ``validate_data()`` checks
    a data dict against the schema, returning a list of error strings.
    """

    model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True)

    name: str
    fields: dict[str, TypeDef] = Field(default_factory=dict)
    description: str = ""

    def validate_data(self, data: dict[str, Any]) -> list[str]:
        """Validate a data dict against this space's field schema.

        Returns a list of error strings (empty means valid).
        """
        errors: list[str] = []
        for field_name, typedef in self.fields.items():
            if field_name not in data:
                errors.append(f"Missing field: {field_name}")
            elif not typedef.check_value(data[field_name]):
                errors.append(
                    f"{field_name}: expected {typedef.name}, "
                    f"got {type(data[field_name]).__name__} "
                    f"with value {data[field_name]!r}"
                )
        extra_fields = set(data.keys()) - set(self.fields.keys())
        if extra_fields:
            errors.append(f"Unexpected fields: {extra_fields}")
        return errors

    def is_compatible(self, other: Space) -> bool:
        """Check if another space has the same structure (field names and types)."""
        if set(self.fields.keys()) != set(other.fields.keys()):
            return False
        return all(self.fields[k] == other.fields[k] for k in self.fields)

validate_data(data)

Validate a data dict against this space's field schema.

Returns a list of error strings (empty means valid).

Source code in packages/gds-framework/gds/spaces.py
def validate_data(self, data: dict[str, Any]) -> list[str]:
    """Validate a data dict against this space's field schema.

    Returns a list of error strings (empty means valid).
    """
    errors: list[str] = []
    for field_name, typedef in self.fields.items():
        if field_name not in data:
            errors.append(f"Missing field: {field_name}")
        elif not typedef.check_value(data[field_name]):
            errors.append(
                f"{field_name}: expected {typedef.name}, "
                f"got {type(data[field_name]).__name__} "
                f"with value {data[field_name]!r}"
            )
    extra_fields = set(data.keys()) - set(self.fields.keys())
    if extra_fields:
        errors.append(f"Unexpected fields: {extra_fields}")
    return errors

is_compatible(other)

Check if another space has the same structure (field names and types).

Source code in packages/gds-framework/gds/spaces.py
def is_compatible(self, other: Space) -> bool:
    """Check if another space has the same structure (field names and types)."""
    if set(self.fields.keys()) != set(other.fields.keys()):
        return False
    return all(self.fields[k] == other.fields[k] for k in self.fields)