Expectation on encoding -> decoding of `None`
See original GitHub issueHello,
I’m on a good way to make a Ruby implementation of NestedText (erikw/nestedtext-ruby, all official decode tests are passing!) and I’m currently writing unit tests for various edge-case inputs. Using the Python implementation, I’m unsure what the expectation is on encoding python’s None
(and ruby’s nil
in my case). I see that None
is treated in different ways.
Using this base-program and just changing the obj = ...
line in the different examples:
import nestedtext as nt
obj = ...
dumped = nt.dumps(obj)
print("dumped:")
print(repr(dumped))
loaded = nt.loads(dumped)
print("loaded:")
print(repr(loaded))
Just None
obj = None
gives the output:
dumped:
''
loaded:
{}
▶️ None
encodes to empty string, but is decoded back as empty inline dict
None
in list
obj = [None]
gives the output:
dumped:
'-'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "__main__.py", line 82, in main
test_dump()
File "__main__.py", line 73, in test_dump
loaded = nt.loads(dumped)
File "[...]/nestedtext.py", line 1088, in loads
loader = NestedTextLoader(lines, top, source, on_dup, keymap)
File "[...]/nestedtext.py", line 765, in __init__
report('content must start with key or brace ({{).', lines.get_next())
File "[...]/nestedtext.py", line 258, in report
raise NestedTextError(template=message, *args, **kwargs)
File "<string>", line 0
nestedtext.NestedTextError
▶️ None
encodes to empty string, but can’t be decoded back to Python
None
as dict value
obj = {"key": None}
gives the output:
dumped:
'key:'
loaded:
{'key': ''}
▶️ None
encodes to empty string, but is decoded back to empty string and not None
None
as dict key
obj = {None: "value"}
gives the output:
dumped:
'None: value'
loaded:
{'None': 'value'}
▶️ None
encodes to the string “None”, and decodes back to the string “None”
Thus, None
is treated different in all cases above. The encoding from Python is of course not a part of the specification for the NT data format, but it would still be nice to know the rules for None
, or that None
is always treated the same in all cases 😃.
Is the output above the expected? I suspect that in the case with None
- in a list, that it should be possible to decode back to Python
- as a key in a dict, that it should maybe instead render to empty string instead of the string “None”, to be more consistent with the other cases?
I’m happy to hear your thoughts!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Okay, I finally updated the GitHub version with this change.
Thank you for the heads-up. I will do the same in nestedtext-ruby 😃.