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

__getitem__ subclassing issue for new 2.2.0 #107

Closed
yhhsteven opened this issue Dec 26, 2018 · 5 comments
Closed

__getitem__ subclassing issue for new 2.2.0 #107

yhhsteven opened this issue Dec 26, 2018 · 5 comments

Comments

@yhhsteven
Copy link

yhhsteven commented Dec 26, 2018

Hi I want to say thank you first as I used a lot addict, it's a very nice module!

I upgraded from 2.1.3 to 2.2.0 and encountered this problem:

return self.__class__(__parent=self, __key=name)
TypeError: __init__() got an unexpected keyword argument '__key'

My code:

class PLAYER( Dict ):
    def __init__(self, pid):
        super(PLAYER, self).__init__()
        self.pid = pid

    def __getitem__ (self, name):
        out = super(PLAYER, self).__getitem__(name)   ### ONLY compatible with addict 2.1.3
        if out == {}:
            return getattr(PLAYERDB(self.pid), name)
        else:
            return out

The same code was working perfectly fine with 2.1.3. But for 2.2.0, it has to be solved by:
out = super(PLAYER, self).get( name, {})

However, I wonder if I were wrong at beginning . Since it is stated that 2.2.0 has been improved for better subclassing, please let me know any better subclassing suggestion for the same purpose. The goal is simple: if there was a missing key in PLAYER, return key from PLAYERDB object.

Sorry for my English. Thanks for your help!

@yhhsteven
Copy link
Author

Hi, any update?

@mewwts
Copy link
Owner

mewwts commented Jan 15, 2019

Hey Steven,

Please see this discussion to see if it helps resolve your issue #101. If it doesn't please ping me again!

@MisterVladimir
Copy link
Contributor

Hi @yhhsteven,

The difference between addict 2.2.0 and 2.1.3 is that passing a missing key to __getitem__ 2.2.0 returns an empty instance of your derived class instead of an empty addict.Dict. What do you think of replacing if out == {} with if len(out) == 0? A more elegant solution would be to re-implement the __missing__(self, name) method of PLAYER to return getattr(PLAYERDB(self.pid), name). What do you think?

@yhhsteven
Copy link
Author

yhhsteven commented Jan 3, 2020

@MisterVladimir @mewwts Thanks for your contributing! I like the idea changed in 2.2.0. I'm using the missing style right now. However, I ran into a problem when implement addict in this way, I'd like know if there is a better way to change my code:

class D(addict.Dict):
    def __init__( self, x ):
        super(D, self).__init__()
        self.x = x

This intends to design a custom attribute x while only utilizing addict as an attribute dict (not the recursive feature). This gives error TypeError: __init__() got an unexpected keyword argument '__parent' when trying to access the missing attribute like D(1).y.

I wonder if the addict's missing should add an AttributeError instead, for a better exception handling, or should I change my code to more correct way when subclassing addict?

Thank you and happy new year!

@mewwts
Copy link
Owner

mewwts commented Jan 7, 2020

Happy new year @yhhsteven,

If you only want to use addict as an attribute dict perhaps you should instead make a class like

class D:
    def __init__(self, x):
        self.attrs = addict.Dict()
        self.x = x

and then use it like D(1).attrs.y?

Thanks for using addict!

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

No branches or pull requests

3 participants