apache_beam.typehints.row_type module

class apache_beam.typehints.row_type.RowTypeConstraint(fields: Sequence[Tuple[str, type]], user_type, schema_options: Sequence[Tuple[str, Any]] | None = None, field_options: Dict[str, Sequence[Tuple[str, Any]]] | None = None)[source]

Bases: TypeConstraint

For internal use only, no backwards comatibility guaratees. See https://beam.apache.org/documentation/programming-guide/#schemas-for-pl-types for guidance on creating PCollections with inferred schemas.

Note RowTypeConstraint does not currently store arbitrary functions for converting to/from the user type. Instead, we only support NamedTuple user types and make the follow assumptions:

  • The user type can be constructed with field values as arguments in order (i.e. constructor(*field_values)).

  • Field values can be accessed from instances of the user type by attribute (i.e. with getattr(obj, field_name)).

In the future we will add support for dataclasses ([#22085](https://github.com/apache/beam/issues/22085)) which also satisfy these assumptions.

The RowTypeConstraint constructor should not be called directly (even internally to Beam). Prefer static methods from_user_type or from_fields.

Parameters:
  • fields – a list of (name, type) tuples, representing the schema inferred from user_type.

  • user_type – constructor for a user type (e.g. NamedTuple class) that is used to represent this schema in user code.

  • schema_options – A list of (key, value) tuples representing schema-level options.

  • field_options – A dictionary representing field-level options. Dictionary keys are field names, and dictionary values are lists of (key, value) tuples representing field-level options for that field.

static from_user_type(user_type: type, schema_options: Sequence[Tuple[str, Any]] | None = None, field_options: Dict[str, Sequence[Tuple[str, Any]]] | None = None) RowTypeConstraint | None[source]
static from_fields(fields: Sequence[Tuple[str, type]], schema_id: str | None = None, schema_options: Sequence[Tuple[str, Any]] | None = None, field_options: Dict[str, Sequence[Tuple[str, Any]]] | None = None, schema_registry: SchemaTypeRegistry | None = None) RowTypeConstraint[source]
property user_type
set_schema_id(schema_id)[source]
property schema_id
property schema_options
field_options(field_name)[source]
type_check(instance)[source]
get_type_for(name)[source]
class apache_beam.typehints.row_type.GeneratedClassRowTypeConstraint(fields, schema_id: str | None = None, schema_options: Sequence[Tuple[str, Any]] | None = None, field_options: Dict[str, Sequence[Tuple[str, Any]]] | None = None, schema_registry: SchemaTypeRegistry | None = None)[source]

Bases: RowTypeConstraint

Specialization of RowTypeConstraint which relies on a generated user_type.

Since the generated user_type cannot be pickled, we supply a custom __reduce__ function that will regenerate the user_type.