question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pyaml converts integer-like strings to integers

See original GitHub issue

For 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:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mk-fgcommented, Oct 9, 2021

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, but 1_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.

1reaction
mk-fgcommented, Dec 11, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found