Community Plugins
Community Plugins¶
The Community Plugins API lets you define custom YAML node types that integrate with pyrs-yaml's serialization and deserialization. A custom type can convert between YAML tagged scalars and arbitrary Python objects.
Built-in Plugins¶
pyrs-yaml ships with built-in plugins registered at import time:
| Tag | Python type | Description |
|---|---|---|
!!timestamp |
datetime / datetime.date |
ISO 8601 timestamp; the text decides which — a date-only text is a date |
!time |
datetime.time |
ISO 8601 time (no date); a local extension, YAML defines no tag for a bare time |
!date |
datetime.date |
the local spelling !!timestamp replaced when writing — still read |
!timestamp |
datetime |
the local spelling !!timestamp replaced when writing — still read |
:material-binary: !uuid |
uuid.UUID |
UUID string ↔ object |
!decimal |
decimal.Decimal |
Arbitrary-precision decimal |
:material-binary: !!binary |
bytes |
Base64-encoded binary data; the local !binary spelling is still read |
!regex |
re.Pattern |
Compiled regex pattern |
!set |
str |
YAML set (unkeyed mapping) — experimental, no round-trip serialization |
tag:yaml.org,2002:timestamp is the only temporal tag YAML defines, and it covers both a date and a
datetime, so safe_dump() writes it for both. The two local spellings above are what this library
wrote before that: a local tag has no constructor outside it, so PyYAML and ruamel.yaml both answered
such a document with ConstructorError and the value could not be read back at all. They stay
registered because documents this library already wrote carry them.
Creating a Custom Type¶
Subclass CustomType and implement from_yaml() and to_yaml():
import pyrs_yaml
from datetime import datetime
class TimestampType(pyrs_yaml.CustomType):
python_type = datetime
def from_yaml(self, value: str):
return datetime.fromisoformat(value)
def to_yaml(self, obj) -> str:
return obj.isoformat()
python_type — Optional attribute used during serialization. When
safe_dump() encounters a Python object, it checks registered types via
isinstance(obj, type.python_type) and calls to_yaml() if matched.
Registering¶
Imperative form:
pyrs_yaml.register_type("!timestamp", TimestampType())
Decorator form:
@pyrs_yaml.register_type("!timestamp")
class TimestampType(pyrs_yaml.CustomType): ...
Usage¶
Loading a tagged scalar:
doc = pyrs_yaml.parse("when: !timestamp 2026-08-11T10:30:00")
val = doc.get("when")
assert isinstance(val, datetime) # val is a datetime object
The tag !timestamp triggers from_yaml() which returns a datetime.
Dumping a Python object:
data = {"ts": datetime(2026, 8, 11, 10, 30)}
out = pyrs_yaml.safe_dump(data)
# out contains: !!timestamp 2026-08-11T10:30:00
safe_dump() checks each value against registered types. When a datetime
object is found, the standard tag is written — !!timestamp, the one other YAML
readers construct — and the text decides on the read side whether it names a
date or a datetime.
API Reference¶
| Method | Description |
|---|---|
can_parse(value) |
Whether this type handles a given scalar value (string) |
from_yaml(value) |
Convert YAML string → Python object |
to_yaml(obj) |
Convert Python object → YAML string |
validate(obj) |
Validate a Python object (returns bool) |
Third-Party Plugins¶
Third-party packages can register custom types by calling register_type()
at import time. Automatic discovery via importlib.metadata.entry_points
(group pyrs_yaml.plugins) is supported. Plugins are discovered at module
import time; errors are logged and do not block startup.
Example: UUID Type¶
import uuid
import pyrs_yaml
class UUIDType(pyrs_yaml.CustomType):
python_type = uuid.UUID
def from_yaml(self, value):
return uuid.UUID(value)
def to_yaml(self, obj):
return str(obj)
pyrs_yaml.register_type("!uuid", UUIDType())
doc = pyrs_yaml.parse("id: !uuid 550e8400-e29b-41d4-a716-446655440000")
assert isinstance(doc.get("id"), uuid.UUID)
See Also¶
- Plugin Development — Build your own custom types
- Custom Schemas — Control scalar type resolution