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

Possible bug with super() in combination with descriptor #7

Open
smarie opened this issue Jan 31, 2020 · 0 comments
Open

Possible bug with super() in combination with descriptor #7

smarie opened this issue Jan 31, 2020 · 0 comments
Labels

Comments

@smarie
Copy link
Owner

smarie commented Jan 31, 2020

From the documentation

The attribute lookup super(B, obj).m searches obj.class.mro for the base class A immediately following B and then returns A.dict['m'].get(obj, B). If not a descriptor, m is returned unchanged. If not in the dictionary, m reverts to a search using object.getattribute().

It seems that at least in some cases this leads to counterintuitive behaviour, and falling back to A.__dict__['m'].__get__(obj, A) (note the change in owner from B to A) seems preferable. Is that a particular case for pyfields because we have a descriptor for the __init__ method and therefore would like it to "stay" on A once called, or would this be preferable in all cases ?

class AInitDescriptor(object):
    def __get__(self, instance, owner):
        print("called on instance %s - creating init for owner class %s" % (instance, owner.__name__))
        def __init__():
            pass
        
        # Assign the __init__ method to the class
        if owner.__name__ != 'A':
            raise ValueError("This is the __init__ descriptor for A, not for %s!" % owner.__name__)
        owner.__init__ = __init__

        # finally return
        return __init__

class A(object):
    __init__ = AInitDescriptor()

class B(A):
    def __init__(self):
        super(B, self).__init__()


# this leads to an error
b = B()

Note that trivial workarounds for this specific example can be found, but the general question remains interesting to discuss.

See smarie/python-pyfields#53 (comment)

@smarie smarie changed the title [pyfields] bug: [pyfields] possible bug with super() in combination with descriptor Jan 31, 2020
@smarie smarie added the pyfields label Sep 7, 2020
@smarie smarie changed the title [pyfields] possible bug with super() in combination with descriptor Possible bug with super() in combination with descriptor Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant