Skip to content

Default value is being ignored while structuring the attr class #14

@shwetas1205

Description

@shwetas1205
  • cattrs version: 0.5.0
  • Python version: 3.6.2
  • Operating System: Debian GNU/Linux 8.9 (jessie)

Description

When I am trying use structure on an attr class having default values set for few optional attributes, those default values are being ignored and not returning to returned object

What I Did

Here is the sample code:

import attr
from cattr import structure, typed
from typing import Optional


@attr.s
class A(object):
    a = typed(Optional[int], default=0)

@attr.s
class B(object):
    b = attr.ib(validator=optional(instance_of(int)), default=0)

When I tried structure({}, A) I expected A1(a=0) but I get A1(a=None)
Similarly for structure({}, B) I get following error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/xxx/cattr/converters.py", line 171, in structure
    return self.structure_func.dispatch(cl)(obj, cl)
  File "/xxx/cattr/converters.py", line 86, in <lambda>
    lambda *args, **kwargs: self.structure_attrs(*args, **kwargs))
  File "/xxx/cattr/converters.py", line 276, in structure_attrs_fromdict
    converted = self._structure_attr_from_dict(a, name, obj)
  File "/xxx/cattr/converters.py", line 286, in _structure_attr_from_dict
    return mapping[name]
KeyError: 'b'

A fix something like will fix the issue: shwetas1205@7d6d6fa

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions