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.

Expectation on encoding -> decoding of `None`

See original GitHub issue

Hello,

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

github_iconTop GitHub Comments

1reaction
KenKundertcommented, May 20, 2022

Okay, I finally updated the GitHub version with this change.

0reactions
erikwcommented, Jan 21, 2022

Thank you for the heads-up. I will do the same in nestedtext-ruby 😃.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Encoding/decoding model of communication - Wikipedia
The Encoding/Decoding model of communication was first developed by cultural studies scholar ... Decoding has both verbal and non-verbal forms of communication: Decoding...
Read more >
Encoding, Decoding and Understanding - The Literacy Bug
One needs to encode, decode and understand diverse texts rapidly and accurately in order to read with enough fluency so as to make...
Read more >
Encoding/decoding `Option[Option[A]]` loses data (is not ...
The problem is only that Codec is no longer injective in the presence of nested Options ( decode(encode(a)) != a ) which isn't...
Read more >
An Overview of Stuart Hall's Encoding and Decoding Theory ...
Encoding refers to that the information disseminator transforming the conveyed message, meaning, intention or viewpoint, through verbal or non-verbal form, into ...
Read more >
4.3 Nonverbal Communication Competence - Lumen Learning
In this section we will discuss some strategies for effectively encoding and decoding nonverbal messages. As we've already learned, we receive little, ...
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