question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

from_dict ignores extraneous data

See original GitHub issue

Probably related to type validation, but I noticed that dacite ignores extraneous data passed to from_dict.

I expected the last example to also raise an exception.

> @dataclass
> class A:  
>    x: str

> A(x="hello")
A(x='hello')
> A(x="hello", y="world")
TypeError: __init__() got an unexpected keyword argument 'y'

> dacite.from_dict(data_class=A, data={"x":"hello", "y": "world"})
A(x='hello')

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
konradhalascommented, Jan 5, 2019

Let’s choose this option:

from_dict(data_class=B, data=data, config=Config(strict=True))

I will try to implement it in upcoming week.

1reaction
jasiszcommented, Dec 19, 2018

I am not sure if this should be a default behaviour though. Such a strict mode IMHO should be enabled by setting it in dacite.Config and with ability to specify where should it be strict.

By “where” I mean the scenario like:

@dataclass
class A:
    x: str
    y: int


@dataclass
class B:
    a: A

data = {
    'a': {
        'x': 'test',
        'y': 1,
    },
    'not_important': 'value'
}

# should not fail, because `a` is ok
from_dict(data_class=B, data=data, config=Config(strict=['a']))

# should fail - strict "from the root level"
from_dict(data_class=B, data=data, config=Config(strict=True))

# should not fail - dafault as now
from_dict(data_class=B, data=data)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add dictionary to pandas data frame and ignore extra values
I want the columns I defined in the data frame only to add to data frame. As of now I'm adding all the...
Read more >
lidatong/dataclasses-json: Easily serialize Data ... - GitHub
Easily serialize Data Classes to and from JSON. ... (the from_dict method ignores them, when loading using schema() a ValidationError is raised.) ...
Read more >
Dataclass Wizard — Dataclass Wizard 0.22.2 documentation
The default behavior is to ignore any unknown or extraneous JSON keys that are encountered when from_dict or from_json is called, and emit...
Read more >
pandas.DataFrame.from_dict — pandas 1.5.2 documentation
Construct DataFrame from dict of array-like or dicts. Creates DataFrame object from dictionary by columns or by index allowing dtype specification. Parameters.
Read more >
Does anyone know why my use of URLSession doesn't return ...
I'm trying to learn how to fetch data from a JSON file hosted on 511.org into a SwiftUI project. More specifically, I am...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found