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

cattr.structure does not respect default value from attr.ib #10

Closed
uhjish opened this issue Sep 3, 2017 · 1 comment · Fixed by #15
Closed

cattr.structure does not respect default value from attr.ib #10

uhjish opened this issue Sep 3, 2017 · 1 comment · Fixed by #15

Comments

@uhjish
Copy link

uhjish commented Sep 3, 2017

  • cattrs version: 0.40
  • Python version: 3.6.2
  • Operating System: Ubuntu 16.04 x86_64

Description

The attrs package allows default values for any given field, and allows initialisation of an attrs class without keys for which a default was provided.

With the following definition,

@attr.s(slots=True, frozen=True)
class Cfg(object):
    a = typed(int)
    b = typed(Optional[int])
    c = attr.ib(convert=int, default=7)
    d = typed(Optional[int], default=5)

We can instantiate a Cfg object as:

In [243]: Cfg(**dt)
Out[243]: Cfg(a=5, b=None, c=7, d=5)

When used with cattr.structure, however, the default values seem to be entirely ignored, moreover there is a KeyError exception thrown. While wrapping the attr.ib type in an Optional avoids the error, we wind up with None instead of the provided default value.

cattr.structure_attrs_fromdict({'a':5,'b':None}, Cfg)

In [208]: cattr.structure_attrs_fromdict(dt, Cfg)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-208-5adc18822e6a> in <module>()
----> 1 cattr.structure_attrs_fromdict(dt, Cfg)

~/dev/survey_stats/venv/lib/python3.6/site-packages/cattr/converters.py in structure_attrs_fromdict(self, obj, cl)
    255             name = a.name
    256             # We detect the type by metadata.
--> 257             converted = self._structure_attr_from_dict(a, name, obj)
    258             conv_obj[name] = converted
    259

~/dev/survey_stats/venv/lib/python3.6/site-packages/cattr/converters.py in _structure_attr_from_dict(self, a, name, mapping)
    265         if type_ is None:
    266             # No type.
--> 267             return mapping[name]
    268         if isinstance(type_, _Union):
    269             # This is a union.

KeyError: 'c'

In [249]: cattr.structure_attrs_fromdict({'a':5,'b':None, 'c':3}, Cfg)
Out[249]: Cfg(a=5, b=None, c=3, d=None)
@Tinche
Copy link
Member

Tinche commented Sep 3, 2017

Hm. Well, cattr.structure is supposed to be able to reverse cattr.unstructure, and by default both cattr.unstructure and attr.asdict will generate a dictionary with the keys present, but the values set to their defaults.

However I agree this is an interesting use case that we should support. I'm awaiting some significant changes in attrs that will make using cattrs more pleasant, so when they land I'll put in another dev cycle into cattrs and see about making this work.

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

Successfully merging a pull request may close this issue.

2 participants