ironpython3 icon indicating copy to clipboard operation
ironpython3 copied to clipboard

Confusion between class variable and instance variable when descriptor is used

Open danabr opened this issue 3 years ago • 1 comments

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

  1. 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.

danabr avatar Aug 11 '23 07:08 danabr

Note that there's already a test case that could be enabled which would cover this test_descr.test_set_and_no_get.

slozier avatar Aug 14 '23 00:08 slozier