Comments (8)
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.
>>> 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.
@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.
@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.
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.
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.
@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.
@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)
- Failure to import HOT 2
- Support `str` subclasses in libyaml extension
- CSafeDumper doesn't appear to respect `allow_unicode=True`
- failed installing dependencies on WSL2 HOT 1
- bug? some string constants are getting printed as ints HOT 2
- yaml dump outputs not-to-spec keys HOT 2
- yaml-6.0.1 ==> cython-3.0.7
- How to install via pip using `--without-libyaml` when `--install-option` is deprecated? HOT 2
- PyYaml missing AArch64 wheel for Python 3.12 only HOT 1
- Result of safe_load_all is not writable with safe_dump_all HOT 4
- Install PyYAML 6.0 from source results into failure: ERROR: No matching distribution found for wheel HOT 1
- Unable to install in Python 3.12 environment HOT 1
- Yaml on dumping giving different result
- RFE: port to `cython` 3.x HOT 2
- Depth first firing of generators HOT 1
- Unable to install due to dependency on setuptools - help please? HOT 2
- Provide wheel for `musllinux` `arm64`
- [Joke Issue] Attempt to write the most complex YAML file in human history
- Can't seem to read emoji HOT 1
- "Y" does not round-trip in practice HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyyaml.