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

Seemingly random quoting of keys #826

Closed
DavidMertz opened this issue Aug 23, 2024 · 2 comments
Closed

Seemingly random quoting of keys #826

DavidMertz opened this issue Aug 23, 2024 · 2 comments

Comments

@DavidMertz
Copy link

I have a configuration file that I read and write exclusively with pyyaml. As various state changes in the overall system, keys are added or removed, or their content modified; this is always by reading the file with pyyaml, tweaking the Python dictionary, and writing it back with pyyaml.

The structure defined is a dictionary with short keys. Often the keys consist entirely of digits, but sometimes they contain letters, or are even exclusively letters. Usually the number-like names have one or more leading zeros.

At first brush, it seemed like perhaps keys that might be interpreted as numbers were quoted, but that is only sometimes true. For some other examples, I thought that there was a difference in whether there were an even versus odd number of leading zeros, but that is violated in the example shown. E.g. sometimes an all-digits code with one leading zero is quoted, sometimes not. Same with two, three, or four leading zeros.

We also use another configuration file that is similar, but contains different information about a subset of the keys in the mentioned file. It appears that the decision to quote or not quote is consistent per key, but the latter file only contains a fairly small subset of the keys in the exhibited one (what's shown is just a few of the keys that illustrate this ticket).

This behavior isn't really a bug since what is produced is valid YAML that can easily be read by pyyaml. It's more like a quirk (at least in the uses I've had so far). In what I've seen, only keys that contain only digits are ever quoted.

[...]
'00015':
  preferred_source: Ada
00021LA:
  preferred_source: Ada
'00026':
  preferred_source: Ada
00048:
  preferred_source: Ada
'01107':
  preferred_source: Ada
01199NE:
  preferred_source: Ada
01948:
  preferred_source: Ada
X298:
  preferred_source: Ada
[...]
@perlpunk
Copy link
Member

perlpunk commented Aug 23, 2024

strings need to be quoted if they resolve to something special, e.g. a number or a boolean.
Decimal numbers can't have zeroes at the beginning, octal numbers must.
00015 would be a (octal) number because it starts with a zero, 00021LA wouldn't be a number. 01948 looks like a number, but it has a 9, which can't be in an octal, so it doesn't need to be quoted.
For more details please check the pinned issue with lots of duplicate issues linked: #486

@perlpunk
Copy link
Member

Feel free to try out my https://pypi.org/project/yamlcore/ package which works just on top of PyYAML and supports the YAML 1.2 Core schema.
You will get a much more reasonably looking result here.

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

No branches or pull requests

2 participants