Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shwetas1205 opened this issue Oct 27, 2017 · 2 comments
Closed

Comments

@shwetas1205
Copy link

  • 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

@Tinche
Copy link
Member

Tinche commented Oct 27, 2017

Hi! This is a duplicate of #10 I think. It's something I will look at after attrs 17.3 is released and I do another development pass of cattrs. Thanks for your interest :)

@shwetas1205
Copy link
Author

Oops, I think I missed #10 Thanks for pointing that out.

I wanted this support for one of my work so I went ahead added a fix here: https://github.com/Tinche/cattrs/pull/15 it would be great if you could have a look. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants