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

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

Closed
alanhdu opened this issue Oct 23, 2020 · 6 comments · Fixed by #142
Closed

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

alanhdu opened this issue Oct 23, 2020 · 6 comments · Fixed by #142

Comments

@alanhdu
Copy link

alanhdu commented Oct 23, 2020

  • 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.

@alanhdu
Copy link
Author

alanhdu commented Oct 23, 2020

If this is something that you'd be open to (either as the default behavior, or as a flag on structure), I'd be happy to dig through the code and submit a PR for the implementation.

@Tinche
Copy link
Member

Tinche commented Oct 23, 2020

Hello,

I'm not really open to changing the default behavior since that would make it very hard to implement backwards compatibility for a lot of use cases cattrs is used for.

I would be open to including this as an option. This should be a new parameter to cattr.gen.make_dict_structure_fn (forbid_extra_fields I guess?).

@asford
Copy link
Contributor

asford commented Nov 19, 2020

Would this solution allow us to set this as the "standard" behavior of a GenConverter instance? It'd be ideal to be able to provide a "strict" vs "lenient" converter over the same types without needing pre-register custom structure/unstructure functions.

@Tinche
Copy link
Member

Tinche commented Nov 20, 2020

Implementing the functionality in make_dict_structure_fn is step 1. Modifying the GenConverter to pass that option to make_dict_structure_fn whenever a structuring function is generated would be step 2.

@bkurtz
Copy link
Contributor

bkurtz commented Mar 5, 2021

Another +1 for this feature. I'm open to doing some work on a PR if no one else has one open already.

@Tinche
Copy link
Member

Tinche commented Mar 5, 2021

@bkurtz Sure, go for it (minding my previous comments). Since there is interest for this I might do this myself in a week if no one else steps up.

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.

4 participants