Skip to content

Cannot enter a plain integer into an attribute specified as type=str  #15

@keckler

Description

@keckler

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions