Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on and off are incorrectly interpreted when they are keys #52

Closed
jason-s opened this issue Feb 16, 2017 · 8 comments
Closed

on and off are incorrectly interpreted when they are keys #52

jason-s opened this issue Feb 16, 2017 · 8 comments
Labels

Comments

@jason-s
Copy link

jason-s commented Feb 16, 2017

on and off are interpreted as True or False when used as values. But they are also interpreted this way as keys:

C:\>python
Python 2.7.12 |Anaconda 4.1.1 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import yaml
>>> yaml.safe_load("""
... on: 1
... off: 2
... """)
{False: 2, True: 1}
>>> yaml.__version__
'3.11'
@jason-s
Copy link
Author

jason-s commented Feb 16, 2017

It doesn't seem to follow the YAML spec (at least not the YAML 1.1 boolean spec) consistently in this regard:

>>> yaml.safe_load("""
... y: 1
... n: 2
... """)
{'y': 1, 'n': 2}
>>> yaml.safe_load("""
... yes: 1
... no: 2
... """)
{False: 2, True: 1}
>>>

@jason-s
Copy link
Author

jason-s commented Feb 16, 2017

>>> 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("%s: 1" % k)
...
y {'y': 1}
Y {'Y': 1}
yes {True: 1}
Yes {True: 1}
YES {True: 1}
n {'n': 1}
N {'N': 1}
no {False: 1}
No {False: 1}
NO {False: 1}
true {True: 1}
True {True: 1}
TRUE {True: 1}
false {False: 1}
False {False: 1}
FALSE {False: 1}
on {True: 1}
On {True: 1}
ON {True: 1}
off {False: 1}
Off {False: 1}
OFF {False: 1}

@mjrk
Copy link

mjrk commented Apr 28, 2017

@jason-s these aliases for booleans are reserved YAML keywords. This counts for keys, too, and other implementation also interpret them as booleans. If you want the string value, you have to wrap them in quotation marks.

>>> import yaml
>>> yaml.safe_load("""
... "no": test
... """)
{'no': 'test'}
>>> yaml.safe_load("""
... no: test
... """)
{False: 'test'}

However, the handling of y/n indeed seems to be inconsistent.

@wirecat
Copy link

wirecat commented Mar 12, 2020

@mjrk Even then, why would anyone want their YAML library to automatically change a key name? That doesn't make any sense to me. I understand it for values, but not keys.

e: I don't see On or Off as keywords in the spec: https://yaml.org/spec/1.2/spec.html#id2803629

@perlpunk
Copy link
Member

e: I don't see On or Off as keywords in the spec: https://yaml.org/spec/1.2/spec.html#id2803629

PyYAML currently only implements YAML 1.1:
https://yaml.org/type/bool.html

@perlpunk
Copy link
Member

Even then, why would anyone want their YAML library to automatically change a key name?

If a language like Python supports other types than strings as dictionary keys, like booleans or None, why not?

true: value 1
false: value 2
null: value 3

YAML makes no difference between keys and values in this regard.

@wirecat
Copy link

wirecat commented Mar 12, 2020

@perlpunk Touche. I personally wouldn't want my keys being transformed like that, but it makes sense that some might.

I didn't realize this library only supported 1.1 and that explains my confusion.

Seems like there isn't an issue here and this could be closed?

@nitzmahone
Copy link
Member

@wirecat it's also trivial to swap in a custom resolver to your own Loader composition to alter or remove that behavior any way you'd like: https://github.com/yaml/pyyaml/blob/master/lib3/yaml/resolver.py

The lack of support for y/n is already tracked on #247, so I'm going to go ahead and close this (since the rest is working as-designed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants