-
Notifications
You must be signed in to change notification settings - Fork 1
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
Subclassing NamedDict leads to infinite recursion in __repr__ #39
Comments
Thanks @jamesmallen . I'll look into this later this week as well. |
unpolished thought: one way to resolve this would be to have functions responsible for creating the injected methods and these functions could accept the typename as an argument: # namedfixedkeymap.py
...
@staticmethod
def _make_repr(typename):
def _repr(self): # pragma: no cover
kwargs = ', '.join('{}={!r}'.format(key, value) for key, value in self.items())
return '{}({})'.format(typename, kwargs)
return _repr
...
methods = {
...
'__repr__': NamedFixedKeyMapMeta._make_repr(typename),
... |
On second thought, I believe the only reason an infinite recursion occurs is because Aside: @jamesmallen have you witnessed any infinite recursion with maps other than |
@jamesmallen feel free to reopen if you do have other cases beyond |
Attempting to subclass
NamedDict
leads to an infinite recursion error when attempting to print out therepr
of an instance of the class. This is due to the use oftype(self)
in thesuper
call.The docs address this (http://maps.readthedocs.io/en/latest/user/design.html#class-vs-function), but I think there is a use case for being able to subclass
NamedDict
- specifically, if you want to be able to distinguish between types of NamedDict usingisinstance
, and/or if you want to add specific functionality to a bag of data.NamedDict is a trivial fix - just change the occurrences of
super(type(self), self)
tosuper(NamedDict, self)
.However, the metaclasses NamedFrozenMapMeta and NamedFixedKeyMapMeta still contain occurrences of
super(type(self), self)
. Due to the way the subclasses are created (usingexec
), it's not immediately apparent to me how one would address this issue - there seems to be a chicken and egg problem in we need to pass a reference to the class into the namespace, but we don't have that reference until it's created. I tried attaching__init__
to the class after creation viatype.__new__
, but that led to other issues:The text was updated successfully, but these errors were encountered: