pyaml converts integer-like strings to integers
See original GitHub issueFor example:
(ae.venv)bash-3.2$ python
Python 2.7.12 (default, Nov 7 2016, 14:33:03)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyaml
>>> pyaml.dump({"foo": "1"})
u'foo: 1\n'
Is this intended behavior? The problem is that the data we read back from this sort of YAML is different to what was written.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
python 3.x - PyYaml parses '9:00' as int - Stack Overflow
The BaseConstructor that is used by the BaseLoader handles any scalar as string, including integers, booleans and "your" sexagesimals:
Read more >quoting strings composed of digits · Issue #98 · yaml/pyyaml
it seems to convert all strings starting with a zero to int, but other ints stored as a string it keeps the quote...
Read more >Python Convert String to Int – How to Cast a String in Python
To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a...
Read more >PyYAML Documentation
The function yaml.load converts a YAML document to a Python object. ... yaml.safe_load limits this ability to simple Python objects like integers or...
Read more >How to Convert a Python String to int
There are several ways to represent integers in Python. In this quick and practical tutorial, you'll learn how you can store integers using...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Certainly can be, as mentioned, I’m just lazy to do it - quite rarely use the module myself as it is, and certainly not for correct serialization, as also mentioned.
Edit: as a side-note, I think such option can be reasonably implemented via +1 parsing pass of PyYAML over string values serialized through this module, and if value parses back to original one - keep it, otherwise replace with a safe-style variant, or picking style by running extra dump/parse ops. This is because unquoted strings in YAML are notoriously unsafe, and it’s not just
123
that’ll be an issue, but1_000
,1.123
,1e123
,null
,false
,2021-10-10T4:30:00
, etc etc - parser knows about all these, so using it should be a good way to catch all such cases.You’re right from the “output must be correct” perspective, but the way I tend to use this module, I’d prefer it to actually be incorrect but don’t have quotes around every digit-only string in there, and maybe use str() after parser if such output should be parsed and it’s a common-enough case.
Might be easy to add an option to do what you hinted at though, i.e. some init keyword plus a check in represent_stringish.