Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
YamlAble
✓ from yamlable import YamlAble
The base class for making objects YAML-serializable.
yaml_info
✓ from yamlable import yaml_info
Decorator for marking class YAML tag info.
Defines a YAML-serializable class with custom tag, then serializes and deserializes.
from yamlable import YamlAble, yaml_info
@yaml_info(yaml_tag='!person')
class Person(YamlAble):
def __init__(self, name, age):
self.name = name
self.age = age
p = Person('Alice', 30)
yaml_str = YamlAble.to_yaml(p)
print(yaml_str)
# Output:
# !person {age: 30, name: Alice}
obj = YamlAble.from_yaml(yaml_str)
assert isinstance(obj, Person)
print(obj.name) # Alice
Errors
Common errors & fixes
TypeError: Cannot dump type <class '__main__.MyClass'>
The class does not inherit from YamlAble or lacks @yaml_info decorator.
fixInherit from YamlAble and decorate with @yaml_info(yaml_tag='!mytag').
yaml.constructor.ConstructorError: could not determine a constructor for the tag '!mytag'
The tag is used in YAML but no corresponding class with that tag has been registered (or the class wasn't imported before loading).
fixEnsure the class with that @yaml_info tag is imported before calling `YamlAble.from_yaml`.
AttributeError: 'str' object has no attribute 'name'
Trying to use YamlAble.from_yaml on a plain YAML string that doesn't contain an object with the expected tag (e.g., a scalar or mapping without tag). It returns a dict/str instead of the expected class instance.
fixAnnotate your YAML with appropriate tags or use `yaml.load` with `Loader=yaml.SafeLoader` for non-tagged data.
Upgrade
Version history
1.1.1latest on PyPI · released Jul 6, 2022
Audit
Dependencies
PyYAMLrequiredRequired for YAML parsing and emitting.