-
-
Notifications
You must be signed in to change notification settings - Fork 776
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
Parsing numbers according to YAML 1.2 #627
Comments
Does it break anything? Are there any strings with numbers and underscores that should stay strings? found this issue, kinda similar symfony/symfony#27120
YAML 1.2 doesn't define how numbers are parsed. As far as YAML is concerned, there're just 3 types: maps, collections and scalars. How they are represented in memory is up to parser/schema. Reference parser wouldn't tell you if it's a number or not. What you linked to is a recommended default schema. I would be very hesitant to make a subset of it, but superset might be fine for historical/interoperability reasons. We used to strictly implement https://yaml.org/type/int.html schema (the one 1.2 spec refers to in section 10.4.). Unfortunately, it has problems (people confused sexagesimals with port numbers, and octals didn't align well with ecmascript changes), so it was reduced a bit. But underscores remained from there. So the questions are:
I'm looking at ruamel.yaml (that was one of the first yaml 1.2 parsers I remember), it casts digits with underscores to numbers as well: $ python3
>>> from ruamel.yaml import YAML
>>> YAML().load("foo: 1_2_3_4")
ordereddict([('foo', 1234)]) |
Other YAML 1.2 libraries use the YAML 1.2 Core schema as the default, e.g. https://github.com/eemeli/yaml, https://github.com/perlpunk/YAML-PP-p5 Note that a "parser" doesn't do anything here, it's the constructor. A YAML parser only cares about the syntax.
Well, it depends on what you do by default. |
I've checked, there are 2 differences with YAML 1.2 core schema left:
change for next major version maybe, not sure @perlpunk do you have any idea why YAML 1.2 spec removed those? |
I don't know why binary was dropped. |
We use I'm happy to update the PR to make underscores "opt out". I would just need some guidance on what you want to call the property or how to make a new schema. |
I have a concrete case where this breaks things. This OpenAPI spec uses The > yaml = require('yaml')
> jsYaml = require('js-yaml')
> yaml.parse('18_24: test')
{ '18_24': 'test' }
> jsYaml.load('18_24: test')
{ '1824': 'test' } |
Based on the spec for YAML 1.2, underscores shouldn't be allowed in numbers:
https://yaml.org/spec/1.2/2009-07-21/spec.html#id2604185
However the latest
js-yaml
still allows them:package.json
test.js
Is this intended behaviour? Thanks in advance.
The text was updated successfully, but these errors were encountered: