If one specifies an attribute to be of type str, they cannot then enter a valid string that looks like an integer to that attribute in their stream.
For example:
In [1]: from yamlize import Object, Attribute
In [2]: class Pet(Object):
...: name = Attribute(type=str)
...: age = Attribute(type=str)
...:
In [3]: lucy = Pet.load(u'''
...: name: Lucy
...: age: 8
...: ''')
YamlizingError: Coerced `<class 'int'>` to `<class 'str'>`, but the new value `8` is not equal to old `8`.
start: in "<unicode string>", line 3, column 6:
age: 8
^ (line: 3)
end: in "<unicode string>", line 3, column 7:
age: 8
^ (line: 3)
Instead, if the user wants to supply (what is ostensibly) an int to a string attribute, they have to use quotes around the value. But this is strange, because quotes are not needed in other cases. For instance, if a user supplies something that obviously looks like a string to a string attribute, quotes are not needed:
In [1]: from yamlize import Object, Attribute
In [2]: class Pet(Object):
...: name = Attribute(type=str)
...: age = Attribute(type=str)
...:
In [3]: lucy = Pet.load(u'''
...: name: Lucy is my name and this is obviously a string
...: age: "8"
...: ''')
In [4]: lucy.name
Out[4]: 'Lucy is my name and this is obviously a string'
It would be nice if Yamlize would fully recognize that the type=str parameter being passed to Attribute means that whatever the user provides, even if it looks like an int or anything else, it should be coerced to a str.
At the least, the printed error could be changed to be more meaningful. Right now it says:
but the new value `8` is not equal to old `8`
and this is confusing because the two printed values look exactly the same.
If one specifies an attribute to be of type
str, they cannot then enter a valid string that looks like an integer to that attribute in their stream.For example:
Instead, if the user wants to supply (what is ostensibly) an
intto a string attribute, they have to use quotes around the value. But this is strange, because quotes are not needed in other cases. For instance, if a user supplies something that obviously looks like a string to a string attribute, quotes are not needed:It would be nice if Yamlize would fully recognize that the
type=strparameter being passed toAttributemeans that whatever the user provides, even if it looks like anintor anything else, it should be coerced to astr.At the least, the printed error could be changed to be more meaningful. Right now it says:
and this is confusing because the two printed values look exactly the same.