Is there any way to override pyyaml's liberal handling of boolean values?

279
February 17, 2017, at 03:33 AM

It looks like pyyaml implements part of the YAML 1.1 spec, which allows a lot of different values to be interpreted as true or false:

Canonical:

y|n

Regexp:

y|Y|yes|Yes|YES|n|N|no|No|NO
 |true|True|TRUE|false|False|FALSE
 |on|On|ON|off|Off|OFF`
>>> for k in "y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF".split("|"):
...   print k, yaml.safe_load(k)
...
y y
Y Y
yes True
Yes True
YES True
n n
N N
no False
No False
NO False
true True
True True
TRUE True
false False
False False
FALSE False
on True
On True
ON True
off False
Off False
OFF False

and yet it serializes them to true or false (in contrast to with YAML 1.1's canonical y and n):

>>> yaml.safe_dump(dict(x=True, y=False))
'{x: true, y: false}\n'

Is there any way to override it so that only the canonical true and false values are interpreted as booleans?

Answer 1

You should do the following to make PyYAML not to recognise those strings as booleans:

  • replace the added implicit resolver that was added in resolver.py, and that by default reads:

    Resolver.add_implicit_resolver(
            u'tag:yaml.org,2002:bool',
            re.compile(ur'''^(?:yes|Yes|YES|no|No|NO
                        |true|True|TRUE|false|False|FALSE
                        |on|On|ON|off|Off|OFF)$''', re.X),
            list(u'yYnNtTfFoO'))
    

    you will have to trace the add_implicit_resolver to see where it adds what as there is no remove_implicit_resolver. Alternatively define a complete new ResolverBase subclass, that does everything Resolver does, but doesn't have the definition for 2002:bool as above, but the restricted (1.2) version.

  • update the bool_values class attribute of SafeConstructor in constructor.py

That doesn't make PyYAML 1.2 compatible, as there is more to that and PyYAML doesn't fully implement the YAML 1.1 specification to start with.

The easiest way to load YAML into python and not have Yes be interpreted as a boolean is using ruamel.yaml instead of PyYAML (disclaimer: I am the author of that package). In its sources you can also find how to keep the parser 1.1 compatible if the file is tagged as YAML 1.1 (if there is no tag ruamel.yaml assumes version 1.2).

Rent Charter Buses Company
READ ALSO
pycharm debugger not working, throwing error on Queue object has no attribute put

pycharm debugger not working, throwing error on Queue object has no attribute put

I am using a windows machine with PyCharm Community Edition 20163

739
Contao CMS Query a 'checkboxWizard' BLOB field

Contao CMS Query a 'checkboxWizard' BLOB field

I have a question about how to query a 'checkboxWizard' BLOB fieldIn have added a such field to tl_member

391
Matlab file into database table

Matlab file into database table

First question : I have a Matlab file withmat extension and I want to load it into data base table using Mysql

394