← index #2720Issue #3947
Related · high · value 0.870
QUERY · ISSUE

Can't get property value of the superclass

openby nuphaopened 2016-12-25updated 2023-03-06
bug

As following code shows, a subclass to override property 'p' of superclass. Calling super().p in subclass will return a property object, not the value. Further more, this property object has no .fget attribute as suggested in standard python. But a 'getter' method is found on property object, when I call .getter(o), another property object returned, still not value.

So, I can't get the property value of superclass. Any suggestion?

class A:
@property
def p(self):
return {"a":10}

class AA(A):
@property
def p(self):
# get super property, add some new value
v = super().p
v['aa'] = 20 # this will raise an exception, because v is a property object, not a dict
return v

CANDIDATE · ISSUE

BuiltinClass.some_property calls property getter/setter; should return <property> object

closedby dhalbertopened 2018-07-15updated 2018-07-15

If MICROPY_PY_BUILTINS_PROPERTY is enabled, builtin classes can have properties. However, the runtime mistakenly calls the property's function both when an instance and the class reference the property. E.g., BuiltinClass().some_property calls the property function, but BuiltinClass.some_property also does, whereas it should instead return a <property> object.

Discovered in CircuitPython, where many builtins provide properties. For instance microcontroller.Processor.temperature and microcontroller.cpu.temperature both call the temperature fetcher. cpu is a singleton instance of Processor. For some cases, we end up hard-faulting because a null object is passed in and then the code tries to use it (e.g., analogio.AnalogIn.value).

MicroPython doesn't use builtin properties at all, as far as I can tell, so there's no example code to run to exercise this. For MicroPython, this was a deliberate choice: https://github.com/micropython/micropython/issues/1430#issuecomment-131767567. We are renegades :smiley: and use properties extensively in our builtin I/O libraries.

PR coming later with a fix, ported from CircuitPython.

Ref: https://github.com/adafruit/circuitpython/issues/1027 and https://github.com/adafruit/circuitpython/pull/1028

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied