yaml / pyyaml

Canonical source repository for PyYAML
MIT License
2.54k stars 515 forks source link

bug? some string constants are getting printed as ints #773

Closed brigadier closed 7 months ago

brigadier commented 9 months ago
l = range(0, 10)
for i in l: yaml.dump(['0' + str(i)])

Result:

... 
"- '00'\n"
"- '01'\n"
"- '02'\n"
"- '03'\n"
"- '04'\n"
"- '05'\n"
"- '06'\n"
"- '07'\n"
'- 08\n'
'- 09\n'

Is this supposed to be so? I'm on python 3.8, pyyaml v. 5.3.1

haojiezhe12345 commented 7 months ago

I'm using pyyaml 6.0.1 on python 3.12, still encountering the bug

nitzmahone commented 7 months ago

This isn't really a bug- it's consistent and correct behavior for roundtripping values in YAML 1.1 (which is all PyYAML currently supports). The default emitter config won't quote string values unless not quoting them would cause a similarly-configured loader to treat them as a non-string. The default 1.1 type configuration recognizes strings matching 0[0-7]+ as octal ints, so strings matching that need to be quoted to prevent the loader seeing them as octals, but 0[89] do not.