-
Notifications
You must be signed in to change notification settings - Fork 31
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
Inconsistent parsing of kwargs in some Children of MongoStore #1013
Comments
It could be a remnant from the python2 days which is unlikely, though. There's also this commit from 2020 that explicitly changed it from no argument to including the class. There's no explanation but it seems to been have done on purpose. |
Thanks for the fast reply! The are obvious benefits of this: the point of failure is now much closer to the the actual function being called. But I don't know if it makes sense to just have Illustration of the reason why we have those weird looking from monty.json import MSONable
# Common pattern - but has issues
from monty.json import MSONable
# Common pattern - but has issues
class Parent(MSONable):
def __init__(self, required_arg, **kwargs):
self.required_arg = required_arg
self.kwargs = kwargs
class Child(Parent):
def __init__(self, **kwargs):
super().__init__(**kwargs)
c = Child(required_arg=1, a = 1)
c.as_dict() The
Missing the Line responsible for this behavior: Attempt at solving solving the issue: def get_all_init_specs(cls):
specs = []
for c in cls.__mro__:
try:
if '__init__' in c.__dict__: # Only get classes that define their own __init__
spec = getfullargspec(c.__init__)
specs.append((c.__name__, spec))
except TypeError: # Skip cases where we can't get argspec (like built-in classes)
continue
return specs |
Thanks for investigating @jmmshn . I'm happy to consider modifying this if you think it's best. Speaking of |
@rkingsbury, the However, I would like to see MSONable serialize up the MRO call stack which will solve this entire class of problems. Currently from monty.json import MSONable
from dataclasses import dataclass
@dataclass
class Parent(MSONable):
required_arg: int
@dataclass
class Child(Parent):
a: int
c = Child(required_arg=1, a = 1)
c.as_dict() gives:
I would be really nice to see it work like this generally. I'll have to think a bit hard about how such a change will interact with cases where there is existing workarounds like we see for |
It looks like we have a few places where the
__init__
skips theMongoStore.__init__
which can leave some flags likesafe_update
unset.maggma/src/maggma/stores/mongolike.py
Line 478 in 0d7bb46
It's not clear to me why we use
super(MongoStore, self)
here instead ofsuper()
. Perhaps it allow more fine-grained re-work of some of the initialization behavior, but it does create some weird edge cases that are hard to catch.The text was updated successfully, but these errors were encountered: