mccartnm / purepy

Pure Virtual Utility for Python
MIT License
0 stars 0 forks source link

Bug with properties #5

Open mccartnm opened 5 years ago

mccartnm commented 5 years ago

Because getattr() will follow through with the get on a property, we can have objects in a bad state.

from purepy import PureVirtualMeta, pure_virtual
class Foo(metaclass=PureVirtualMeta):
    def __init__(self):
        self._thing = 10

    @property
    def get_thing_math(self):
        return self.some_func(self._thing)

    @pure_virtual
    def some_func(self):
        raise NotImplementedError()

    def overload_me_to_not_pv(self, thing):
        raise NotImplementedError()

class Bar(Foo):
    def some_func(self, thing):
        self.overload_me_to_not_pv(thing)

# We don't have to overload overload_me_to_not_pv(), but when we make a Bar() object:
b = Bar()
# ...
# File "C:\virtualenv\pgfun\lib\site-packages\purepy\__init__.py", line 80, in __call__
#     functions = PureVirtualMeta.pure_virtual_functions(inst)
# File "C:\virtualenv\pgfun\lib\site-packages\purepy\__init__.py", line 154, in pure_virtual_functions
#    for name, call in inspect.getmembers(instance, predicate=PureVirtualMeta._predicate):
#  File "C:\Users\Michael McCartney\AppData\Local\Programs\Python\Python37\lib\inspect.py", line 341, in getmembers
#    value = getattr(object, key)
...
# raise NotImplementedError()

This is wrong because we don't want to evaluate the property. We just want to find the virtual functions. May have to rethink the design slightly

mccartnm commented 5 years ago

This is made more challenging by the fact that inspect.getmembers does the evaluation automatically. We might have to get a little more dirty and peer into the getmemebers source.