Python SDK type hints and coders
Python3 can provide type hints on functions, as shown in the following code:
def toKv(s: str) -> beam.typehints.KV[bytes, bytes]: return ("".encode("utf-8"), s.encode("utf-8"))
The previous code defines a method called toKv
that takes a string (str
) as input and outputs an object that's compatible with beam.typehints.KV[bytes, bytes]
. When we use such a function in a simple transform such as beam.Map(toKv)
, Beam can infer the type of the resulting PCollection
and can automatically use a special-purpose coder instead of pickle. In the case of bytes, this would be ByteArrayCoder
.
Besides declaring type hints to mapping functions, we can use a decorator for DoFns
, which will declare the (input or output) type hint explicitly for the whole transform:
@beam.typehints.with_input_types( beam.typehints.Tuple[ str, beam.typehints...