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.

Bug: StrictYaml cannot serialize `None` (null value)

See original GitHub issue

Typing the following into the python interpreter

Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import strictyaml
>>> yaml = strictyaml.as_document({'a':None})

results in

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Temp\3\venv3\lib\site-packages\strictyaml\parser.py", line 258, in as_document
    return schema(YAMLChunk(schema.to_yaml(data), label=label))
  File "D:\Temp\3\venv3\lib\site-packages\strictyaml\any_validator.py", line 45, in to_yaml
    return schema_from_data(data).to_yaml(data)
  File "D:\Temp\3\venv3\lib\site-packages\strictyaml\compound.py", line 197, in to_yaml
    for key, value in data.items()
  File "D:\Temp\3\venv3\lib\site-packages\strictyaml\compound.py", line 200, in <listcomp>
    and value != self._defaults[key]
  File "D:\Temp\3\venv3\lib\site-packages\strictyaml\scalar.py", line 148, in to_yaml
    raise YAMLSerializationError("'{}' is not a string".format(data))
strictyaml.exceptions.YAMLSerializationError: 'None' is not a string

This appears to be a bug. (If it is not a bug, this gaping feature-hole is not documented anywhere.)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
crdoconnorcommented, Apr 22, 2020

I can add a NoneValidator which will accept values other than empty (defaulting to null) if that helps?

On Wed, 22 Apr 2020, 14:25 ArneBachmannDLR, notifications@github.com wrote:

Here is my workaround:

from strictyaml.scalar import ScalarValidator class None_(ScalarValidator): ‘’’ Validator for strictyaml schemas expecting a None. ‘’’

def validate_scalar(_, chunk): if chunk.contents == ‘null’: return None chunk.expecting_but_found(“when expecting None”)

def to_yaml(_, data): return ‘null’

Make sure to put None_() before | Str() or other unions.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/57#issuecomment-617775873, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNOU3QRCL5KE7VIEAGLRN3V5JANCNFSM4HOZFTOQ .

2reactions
ArneBachmannDLRcommented, Apr 22, 2020

I think it would be useful to define a value to be optionally None (YAML’s null). The problem is that None is already taken in Python, so that None() is not available, therefore I went with None_(). An alternative might be NoneOr(Type()) or Nullable(Type).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changelog - HitchDev
BUGFIX : Fix for bug caused by revalidation with Optional default values. ... BUGFIX : Fixed the way serialization of empty lists, dicts,...
Read more >
cannot serialize None (type NoneType) in ElementTree ...
I was wondering if there is a way to avoid writing the attribute at all if the value of the attribute is None....
Read more >
The Norway Problem | Hacker News
I mean if the value is imported as a decimal, then a sort by that field will sort as decimal. This might not...
Read more >
YAML: The Missing Battery in Python
You'll also serialize Python objects and create a YAML syntax ... state the lack of a value, but you can also leave the...
Read more >
why StrictYAML refuses to do implicit typing and so should you
It's not "the Norway Problem" it's "the YAML Problem". ... takes yes/no, true/false, and I think on/off as boolean values while encouraging ...
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