I have to covert a project from pyyaml to ruamel.yaml. One function is using pyyaml.FullLoader and i cannot find a ruamel.yaml alternative to this loader.
I do not know exactly why FullLoader was initially chosen but it is a loading trusted files only.
is it ruamel.YAML(typ="unsafe")
?
If not, how do these two yaml loading methods differ?
edit* The file this function loads can contain unix environment variables such as $PROJ_PATH incase that makes a difference.
I am not completely familiar with the FullLoader
, it was added after ruamel.yaml
was forked from PyYAML 3.14. But it is probably what used to be the default (unsafe) loader from PyYAML and you would replace that with:
from pathlib import Path
import ruamel.yaml
yaml = ruamel.yaml.YAML(typ='unsafe', pure=True)
data = yaml.load(Path('/some/path/to/a/file.yaml'))
The primary difference (apart from bugfixes) is that ruamel.yaml
used in this way supports YAML 1.2 (the default) and YAML 1.1, and PyYAML only a subset of YAML 1.1. So if you rely on YAML 1.1 specifics ( e.g. Yes being a boolean and 12:34:56 being a sexagesimal ) you should also specify:
yaml.version((1, 1))
if the files don't have a %YAML 1.1
directive.
If you leave out pure=True
you'll also will be using a C based scanner that tokenizes the data, which is quite a
bit faster, but also currently not fully YAML 1.2 compatible.
There are valid YAML constructs that ruamel.yaml
supports but PyYAML
doesn't, but if your document currently works with PyYAML, you are unlikely to use these :-)
The main difference between using the "unsafe" loader and the (default) round-trip loader is how tags that start with !!python/object
are loaded. The unsafe loader executes the code specified with that tag (usually creating a Python object or calling a Python function during loading, and that is what makes this unsafe). The round-trip mode can handle YAML documents with these tags as well. But it will safely create a normal ruamel.yaml
object and put the tag on an attribute for further inspection (and dumping), without executing any code specified in the tag. This makes the round-trip loader safe but also able to deal with tags in YAML without necessarily having objects registered.
The object created most often is a CommentedMap
, but CommentedList
or a primitive can also be created with a tag attribute, depening on the node the tag in YAML applies to.
If possible to implement I recommend searching for the actual tags used, loading with the default round-trip loader, and walking over the data tree checking for tags that need evaluation, replacing data.