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)
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:
I think it would be useful to define a value to be optionally None (YAML’s
null
). The problem is thatNone
is already taken in Python, so thatNone()
is not available, therefore I went withNone_()
. An alternative might beNoneOr(Type())
orNullable(Type)
.I don’t think
NotNull
makes sense. Regarding theEmptyDict
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 schemeNullNone
makes sense. Either naming decision is valid.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 extendScalarValidator
as Arne has shown? (thanks btw). MaybeNull
is a meaningful name as well, since it is not protected?