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

I can create a method in a box that overwrite a key #222

Open
12rambau opened this issue Jun 8, 2022 · 2 comments
Open

I can create a method in a box that overwrite a key #222

12rambau opened this issue Jun 8, 2022 · 2 comments

Comments

@12rambau
Copy link

12rambau commented Jun 8, 2022

I was playing with the lib by subclassing it and I founds some funny behaviour. I know it's not the intended usage but though that was worth reporting. Feel free to close it if you think it's useless:

class toto(Box):
    
    def __init__(self, valeur):
        
        super().__init__({"valeur": [valeur, valeur], "print": "toto"})
        
    def print(self):
        print(self.valeur)
        return
    
toto("truc").print
>>>><bound method toto.print of Box({'valeur': ['truc', 'truc'], 'print': 'toto'})>

I didn't know what to expect but it's the method that have priority over the key.

but the key still exist

toto("truc")["print"]
>>>>'toto'

I think it would be handy to at least raise a warning ?

@cdgriffith
Copy link
Owner

I don't have a section of the docs for other devs subclassing and what that could mean, but for this case you'll need to expaind the _protected_keys to include print in this case.

https://github.com/cdgriffith/Box/blob/master/box/box.py#L148

Here is how I do it with a subclass of box https://github.com/cdgriffith/Box/blob/master/box/config_box.py#L20

@12rambau
Copy link
Author

12rambau commented Jun 9, 2022

that's exactly what I needed, thanks a lot. I assume I'll leave the issue open for documenting

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