Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ability to weakly reference an instance of a class lost due to mutliple inheritance #95710

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

Open
markshannon opened this issue Aug 5, 2022 · 2 comments
Labels
3.10 only security fixes 3.11 only security fixes 3.12 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@markshannon
Copy link
Member

This is related to #95589 due to underlying mechanism, but the behavior is distinct.

class C: 
    pass

class D(int, C):
    pass
>>> c = C()
>>> weakref.ref(c)
<weakref at 0x7fa5b2af9c60; to 'C' at 0x7fa5b2e501c0>
>>> d = D()
>>> weakref.ref(d)
TypeError: cannot create weak reference to 'D' object
>>> isinstance(d, C)
True

If I can weakly reference an instance of C, then I should be able to weakly reference an instance of D. Instances of D are instances of C

@markshannon markshannon added type-bug An unexpected behavior, bug, or error 3.11 only security fixes 3.10 only security fixes 3.12 only security fixes labels Aug 5, 2022
@markshannon markshannon changed the title Ability to weakly reference a class lost due to mutliple inheritance Ability to weakly reference an instance of a class lost due to mutliple inheritance Aug 5, 2022
@Mandera
Copy link

Mandera commented Aug 5, 2022

I don't think this has much to do with inheritance. It's caused by int not supporting weakref.

See https://stackoverflow.com/a/52011601/3936044

int has a __weakrefoffset__ of 0 meaning it has no weakref pointer, remove the int baseclass and it works fine

@markshannon
Copy link
Member Author

C supports weakrefs, D is a subclass of C, but D does not support weakrefs.
This breaks the Liskov substitution principle.

Whether or not int supports weakrefs is irrelevant.

list does not support weakrefs, but class D(list, C) does.

@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants