Giter VIP home page Giter VIP logo

Comments (8)

jason-s avatar jason-s commented on May 18, 2024

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}
>>>

from pyyaml.

jason-s avatar jason-s commented on May 18, 2024
>>> 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}

from pyyaml.

mjrk avatar mjrk commented on May 18, 2024

@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.

from pyyaml.

wirecat avatar wirecat commented on May 18, 2024

@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

from pyyaml.

perlpunk avatar perlpunk commented on May 18, 2024

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

from pyyaml.

perlpunk avatar perlpunk commented on May 18, 2024

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.

from pyyaml.

wirecat avatar wirecat commented on May 18, 2024

@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?

from pyyaml.

nitzmahone avatar nitzmahone commented on May 18, 2024

@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).

from pyyaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.