cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

cattr.structure converting None

Open fwannmacher opened this issue 6 years ago • 1 comments

  • cattrs version: 0.9.0
  • Python version: 3.6
  • Operating System: Ubuntu 18.04

Description

structure is converting None to 'None' when it is set to a str field.

What I Did

dataclass = partial(attrs, auto_attribs=True, frozen=True)

@dataclass
class BaseData:
    def to_dict(self):
        return asdict(self)

@dataclass
class A(BaseData):
    a: str
    b: str

print(structure({'a': 'asd', 'b': None}, A))
>>> A(a='asd', b='None')

fwannmacher avatar Dec 05 '18 11:12 fwannmacher

I can reproduce in 0.9.1.dev0, the latest master, using a simpler example:

>>> import attr, cattr
>>> @attr.s
>>> class Model:
...     a: str = attr.ib()
...
>>> cattr.structure({"a": None}, Model)
Model(a='None')

The issue is related to #26, and arise due to the type coercion of str

madsmtm avatar Jan 24 '19 20:01 madsmtm