ironpython3
ironpython3 copied to clipboard
Confusion between class variable and instance variable when descriptor is used
Prerequisites
- [X] Are you running the latest version?
- [X] Are you reporting to the correct repository?
- [X] Did you perform a cursory search?
Description
Consider the following program:
class ListDescriptor:
def __set__(self, obj, value):
obj.__dict__["x"] = value
class Test:
x = ListDescriptor()
def __init__(self):
self.x = [1,2,3]
t = Test()
print(t.x)
The output from this program differs between CPython and IronPython.
Steps to Reproduce
- Run the above program
Expected behavior: Output should be [1, 2, 3].
Actual behavior: Output is <ListDescriptor object at 0x000000000000002B>.
Workarounds
You can work around this by explictly defining the __get__ method in ListDescriptor. However, we encountered this in third-party code that we would rather not modify if it could be avoided.
Note that there's already a test case that could be enabled which would cover this test_descr.test_set_and_no_get.