Skip to content

[Feature] Throw error when structuring dict w/ extra keys #101

@alanhdu

Description

@alanhdu
  • cattrs version: 1.0.0
  • Python version: Python 3.6
  • Operating System: Linux (Fedora)

Description

Right now, cattr.structure will silently ignore extra keys when structuring into an attrs object:

In [1]: import attr
   ...: import cattr
   ...: 
   ...: @attr.s
   ...: class A:
   ...:     a: int = attr.ib()
   ...: 
   ...: cattr.structure({"a": 1, "b": 2}, A)
Out[1]: A(a=1)

I assume this is intended behavior (since I believe this changed since cattrs 0.9), but it'd be nice to have an option to throw an error on extra keys instead.

In our particular use-case, we are using cattrs and attrs to verify some configuration files we are using, some of which have default keys:

@attr.s
class Config:
    some_key: int = attr.ib(default=0)

b/c of this "silently allow extra keys" behavior, cattr.structure doesn't catch typos (e.g. some_key vs sme_key), because it will just ignore the incorrectly-typed key and use the default value for the substitute.

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