Box icon indicating copy to clipboard operation
Box copied to clipboard

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

Open 12rambau opened this issue 2 years ago • 2 comments

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 ?

12rambau avatar Jun 08 '22 18:06 12rambau

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

cdgriffith avatar Jun 08 '22 19:06 cdgriffith

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

12rambau avatar Jun 09 '22 09:06 12rambau