[WIP] Check for valid kwargs in default from_dict#67
Conversation
|
I am unclear why this is a warning message is needed. Surely if the deserialization fail it is quite clear that the argument is wrong. |
|
The case where this came up:
This came up because one of the Custodian error handlers dropped an argument, but we have thousands of FireWorks that have still that argument hard-coded, so now even some |
Should not make changes directly on GitHub . . .
|
It is a very bad idea to deserialize by ignoring arguments. Code should be updated to handle deprecations. If devs want to ignore arguments, they can by all means write a wrapper function to clean the dict before doing deserialization. But it should not be that Monty just ignores bad arguments. |
|
So there is a pull request in Custodian to handle the deprecation in this specific instance -- but in general, the design of FireWorks is such that it becomes very fragile when these kinds of backwards incompatible changes are made. I think a warning is a fair compromise (imo). For example, currently any historical data that has been serialized via I agree that devs should handle this properly in the first place before making such changes but this does not always happen ... |
shyuep
left a comment
There was a problem hiding this comment.
Automated review by Claude (on behalf of @shyuep)
The intent here — warning when a serialized dict contains keys unknown to the current class constructor — is good for catching version-mismatch bugs. However, there is a correctness issue with the current implementation.
Behavioral regression for **kwargs constructors:
getfullargspec(cls).args only captures explicitly named parameters. If a class's __init__ accepts **kwargs, all extra keys would be silently dropped rather than forwarded. Previously, from_dict passed all non-@ keys through, which is the correct behavior for such classes. The filter should be skipped (or handle varkw) when getfullargspec(cls.__init__).varkw is not None.
Merge conflicts:
The PR has merge conflicts against master and needs to be rebased. Given that it has been open since 2019, a rebase would be needed regardless.
Suggested fix sketch:
spec = getfullargspec(cls.__init__)
if spec.varkw is None:
decoded = {k: ... for k, v in d.items() if k in spec.args and not k.startswith("@")}
# warn on invalid_keys
else:
decoded = {k: ... for k, v in d.items() if not k.startswith("@")}Please address these and rebase onto master.
Generated by Claude Code
Improves robustness of monty when backwards incompatible changes are made. See materialsproject/custodian#130