Borislav Hadzhiev
Sun Apr 24 2022·2 min read
Photo by Jeremy Bishop
The Python "TypeError: load() missing 1 required positional argument:
'Loader'" occurs when we use the yaml.load()
method without specifying the
Loader
keyword argument. To solve the error, use the yaml.full_load()
method
instead or explicitly set the Loader
keyword arg.
Here is an example of how the error occurs.
import yaml document = """ a: 1 b: c: 3 d: 4 """ # ⛔️ TypeError: load() missing 1 required positional argument: 'Loader' print(yaml.dump(yaml.load(document)))
The yaml.load
method now
requires
us to explicitly specify the Loader
keyword arguments because of some security
implications around the default behavior of the method.
The easiest way to solve the error is to use one of the yaml.full_load()
or
yaml.safe_load()
methods.
import yaml document = """ a: 1 b: c: 3 d: 4 """ print(yaml.dump(yaml.safe_load(document))) print(yaml.dump(yaml.full_load(document)))
The examples above achieve the same result as explicitly passing the Loader
keyword argument in a call to the yaml.load()
method.
import yaml document = """ a: 1 b: c: 3 d: 4 """ print(yaml.dump(yaml.load(document, Loader=yaml.SafeLoader))) print(yaml.dump(yaml.load(document, Loader=yaml.FullLoader)))
The SafeLoader
which is used by the yaml.safe_load()
method loads a subset
of the YAML language. This is recommended for loading untrusted input.
yaml.safe_load
method should be used unless you need to serialize or deserialize arbitrary objects, because the method cannot execute arbitrary code from the YAML file.The FullLoader
which is used by the yaml.full_load
method loads the full
YAML language. Using this with untrusted input is not recommended.
You can read more about why the default behavior of the yaml.load
method was
deprecated in
this Github wiki.