Skip to content
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

Ability to use getters/setters? #211

Closed
aronchick opened this issue Sep 15, 2021 · 3 comments
Closed

Ability to use getters/setters? #211

aronchick opened this issue Sep 15, 2021 · 3 comments

Comments

@aronchick
Copy link

Hi - LOVE box - just terrific software.

Is there any way to add a getter/setter to a particular field? I've tried subclassing box, but it's not working as intended. Specifically, I'm looking to valid if a path set on a value exists or not when it's set.

@cdgriffith
Copy link
Owner

Sorry for delay on any feedback on this idea. It's a great idea, and something I didn't know wasn't working.

I did some initial testing and confirmed the behavior. Did a little digging into why....then backed off because I really didn't want to do that much work. I still don't, but at least admitting it for time being 😄

Example of the problem for future reference:

from box import Box


class MySubclass(Box):

    def __init__(self, *args, **kwargs):
        super(MySubclass, self).__init__(*args, **kwargs)
        self._my_field = None

    @property
    def my_field(self):
        return self._my_field

    @my_field.setter
    def my_field(self, data):
        print('SHOULD BE CALLED!')
        self._my_field = data

a = MySubclass()
a.my_field = 5

What seems to happen is my_field setter isn't called, as Box's __setattr__ is called fist and is unaware of it. So self._my_field is never set, instead is added to internal dict as designed. Then when my_field @property is called, it actually is being called correctly with the logic currently in __getattr__ so it would return None still, even after being set.

@cdgriffith
Copy link
Owner

Thanks @Serge45 for fixing this!

Adding this feature in Box 7! Please test and give feedback if possible pip install python-box[all]~=7.0.0rc0

cdgriffith added a commit that referenced this issue Feb 4, 2023
* Adding #169 default functions with the box_instance and key parameter (thanks to Коптев Роман Викторович)
* Adding #170 Be able to initialize with a flattened dict - by using DDBox (thanks to Ash A.)
* Adding #192 box_dots treats all keys with periods in them as separate keys (thanks to Rexbard)
* Adding #211 support for properties and setters in subclasses (thanks to Serge Lu and David Aronchick)
* Adding #226 namespace to track changes to the box (thanks to Jacob Hayes)
* Adding #236 iPython detection to prevent adding attribute lookup words (thanks to Nishikant Parmar)
* Adding #238 allow ``|`` and ``+`` for frozen boxes (thanks to Peter B)
* Adding new DDBox class (Default Dots Box) that is a subclass of SBox
* Fixing #235 how ``|`` and ``+`` updates were performed for right operations (thanks to aviveh21)
* Fixing #234 typos (thanks to Martin Schorfmann)
* Fixing no implicit optionals with type hinting
@cdgriffith
Copy link
Owner

Added in 7.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants