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

[WIP] Check for valid kwargs in default from_dict #67

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion monty/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from inspect import getfullargspec

from warnings import warn

try:
import numpy as np
except ImportError:
Expand Down Expand Up @@ -163,8 +165,23 @@ def from_dict(cls, d):
:param d: Dict representation.
:return: MSONable class.
"""

# only pass valid args to class
args = getfullargspec(cls).args
decoded = {k: MontyDecoder().process_decoded(v) for k, v in d.items()
if not k.startswith("@")}
if (k in args) and (not k.startswith("@"))}

# if d contains args that are not valid, warn user (may be due to deprecation)
invalid_keys = {k for k in d.keys() if (k not in args) and (not k.startswith("@"))}
if invalid_keys:
msg = "Trying to de-serialize dictionary into {}, "\
"contains invalid key(s): {}.".format(cls.__name__, invalid_keys)
msg += " Valid keys are: {}".format(args)
if "@version" in d:
msg += " Dictionary was serialized using {} version {}."\
.format(d["@module"], d["@version"])
warn(msg)

return cls(**decoded)

def to_json(self) -> str:
Expand Down