[](https://www.npmjs.com/package/soia-python-gen) [](https://github.com/gepheum/soia-python-gen/actions)
npm install soia-python-gen

Official plugin for generating Python code from .soia files.
Targets Python 3.10 and higher.
From your project's root directory, run npm i --save-dev soia-python-gen.
In your soia.yml file, add the following snippet under generators:
``yaml`
- mod: soia-python-gen
config: {}
The npm run soiac command will now generate .py files within the soiagen directory.
For more information, see this Python project example.
The examples below are for the code generated from this .soia file.
`python`Import the given symbols from the Python module generated from "user.soia"
from soiagen.user_soia import TARZAN, User, UserHistory, UserRegistry
For every struct S in the .soia file, soia generates a frozen/deeply immutable class S and a mutable class S.Mutable.
#### Frozen struct classes
`pythonTo consruct a frozen/deeply immutable User, either call the partial() static
factory method or the constructor.
john = User.partial(
user_id=42,
name="John Doe",
)
assert john.name == "John Doe"
assert john.user_id == 42
assert User.DEFAULT == User.partial()
`
#### Mutable struct classes
`pythonUser.Mutable is a mutable version of User.
lyla_mut = User.Mutable()
lyla_mut.user_id = 44
lyla_mut.name = "Lyla Doe"
joly_history_mut = UserHistory.Mutable()
joly_history_mut.user = joly_mut
#### Converting between frozen and mutable
`python
to_mutable() does a shallow copy of the frozen struct, so it's cheap. All the
properties of the copy hold a frozen value.
evil_jane_mut = jane.to_mutable()
evil_jane_mut.name = "Evil Jane"to_frozen() recursively copies the mutable values held by properties of the
object. It's cheap if all the values are frozen, like in this example.
evil_jane: User = evil_jane_mut.to_frozen()You can also call replace() on the frozen struct.
evil_jane = evil_jane.replace(name="Evil Jane")
Same as:
evil_jane_mut = evil_jane.to_mutable()
evil_jane_mut.name = "Evil Jane"
evil_jane = evil_jane_mut.to_frozen()
assert evil_jane.user_id == 43
`#### Writing logic agnostic of mutability
`python
'User.OrMutable' is a type alias for 'User | User.Mutable'.
def greet(user: User.OrMutable):
print(f"Hello, ${user.name}")
greet(jane)
Hello, Jane Doe
greet(lyla_mut)
Hello, Lyla Doe
`$3
The definition of the
SubscriptionStatus enum in the .soia file is:
`rust
enum SubscriptionStatus {
FREE;
trial: Trial;
PREMIUM;
}
`#### Making enum values
`python
john_status = User.SubscriptionStatus.FREE
jane_status = User.SubscriptionStatus.PREMIUMjoly_status = User.SubscriptionStatus.UNKNOWN
Use wrap_*() for data variants.
roni_status = User.SubscriptionStatus.wrap_trial(
User.Trial(start_time=soia.Timestamp.from_unix_millis(1744974198000))
)
`#### Conditions on enums
`python
Use e.kind == "CONSTANT_NAME" to check if the enum value is a constant.
assert john_status.kind == "FREE"
assert john_status.value is NoneStatic type checkers will complain: "RED" not in the enum definition.
assert jane_status.kind == "RED"
Use "?" for UNKNOWN.
assert joly_status.kind == "?"assert roni_status.kind == "trial"
assert isinstance(roni_status.value, User.Trial)
def get_subscription_info_text(status: User.SubscriptionStatus) -> str:
# Use the union() getter for typesafe switches on enums.
if status.union.kind == "?":
return "Unknown subscription status"
elif status.union.kind == "FREE":
return "Free user"
elif status.union.kind == "trial":
# Here the compiler knows that the type of union.value is 'User.Trial'
trial: User.Trial = status.union.value
return f"On trial since {trial.start_time}"
elif status.union.kind == "PREMIUM":
return "Premium user"
# Static type checkers will complain here if you missed a case.
_: Never = status.union.kind
raise AssertionError("Unreachable code")
`$3
Every frozen struct class and enum class has a static readonly
serializer property which can be used for serializing and deserializing instances of the class.`python
Serialize 'john' to dense JSON.
serializer = User.serializer
print(serializer.to_json(john))
[42, 'John Doe']
assert isinstance(serializer.to_json(john), list)
to_json_code() returns a string containing the JSON code.
Same as calling json.dumps() on the result of to_json()
print(serializer.to_json_code(john))
[42,"John Doe"]
Serialize 'john' to readable JSON.
print(serializer.to_json_code(john, readable=True))
{
"user_id": 42,
"name": "John Doe"
}
The dense JSON flavor is the flavor you should pick if you intend to
deserialize the value in the future. Soia allows fields to be renamed, and
because fields names are not part of the dense JSON, renaming a field does
not prevent you from deserializing the value.
You should pick the readable flavor mostly for debugging purposes.
`$3
`python
Use from_json() and from_json_code() to deserialize.
assert john == serializer.from_json(serializer.to_json(john))
assert john == serializer.from_json_code(serializer.to_json_code(john))
Also works with readable JSON.
assert john == serializer.from_json_code( #
serializer.to_json_code(john, readable=True)
)
`$3
`python
user_registry = UserRegistry(users=[john, jane, lyla_mut])'user_registry.users' is an instance of a subclass of tuple[User, ...] which
has methods for finding items by key.
assert user_registry.users.find(42) == john
assert user_registry.users.find(100) is None
assert user_registry.users.find_or_default(42).name == "John Doe"
assert user_registry.users.find_or_default(100).name == ""
find() and find_or_default() run in O(1) time.
`$3
`python
print(TARZAN)
User(
user_id=123,
name='Tarzan',
quote='AAAAaAaAaAyAAAAaAaAaAyAAAAaAaAaA',
pets=[
User.Pet(
name='Cheeta',
height_in_meters=1.67,
picture='🐒',
),
],
subscription_status=User.SubscriptionStatus.wrap_trial(
User.Trial(
start_time=Timestamp(
unix_millis=1743592409000,
_formatted='2025-04-02T11:13:29Z',
),
)
),
)
`$3
#### Starting a soia service on an HTTP server
Full example here.
#### Sending RPCs to a soia service
Full example here.
$3
Reflection allows you to inspect a soia type at runtime.
`python
field_names: list[str] = []user_type_descriptor = User.serializer.type_descriptor
'user_type_descriptor' has information about User and all the types it
depends on.
print(user_type_descriptor.as_json_code())
{
"type": {
"kind": "record",
"value": "user.soia:User"
},
"records": [
{
"kind": "struct",
"id": "user.soia:User",
"fields": [
{
"name": "user_id",
"type": {
"kind": "primitive",
"value": "int64"
},
"number": 0
},
...
{
"name": "pets",
"type": {
"kind": "array",
"value": {
"item": {
"kind": "record",
"value": "user.soia:User.Pet"
}
}
},
"number": 3
},
...
]
},
{
"kind": "struct",
"id": "user.soia:User.Pet",
...
},
...
]
}
A TypeDescriptor can be serialized and deserialized.
assert user_type_descriptor == soia.reflection.TypeDescriptor.from_json_code(
user_type_descriptor.as_json_code()
)
``