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

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.)

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 20 (7 by maintainers)

Most upvoted comments

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 .

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).

I wanted it to to mirror the existing EmptyNone / EmptyDict / EmptyList validates. I don’t plan on having a NotNull type. Should I?

I don’t think NotNull makes sense. Regarding the EmptyDict I always found it hard to grasp (if empty then dict? accept empty or dict? if not defined then interpret as dict?). But following that scheme NullNone makes sense. Either naming decision is valid.

I don’t plan on having a NotNull type. Should I?

I think only NotNullStr is needed due to default Str behavior. IMO in most cases it is crucial to know when a str field is expected to not be null

I’ve added NullNone as a new validator that parses “null” to None and serializes None to “null” and release tomorrow. Apologies I dropped the ball on this one.

Hi! Any updates on this? Do we have a built-in validator for "null" -> None or should we extend ScalarValidator as Arne has shown? (thanks btw). Maybe Null is a meaningful name as well, since it is not protected?