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

self can't be used as a key #93

Closed
grundic opened this issue Apr 13, 2017 · 3 comments
Closed

self can't be used as a key #93

grundic opened this issue Apr 13, 2017 · 3 comments

Comments

@grundic
Copy link

grundic commented Apr 13, 2017

Dict(self='this')
>>> TypeError: __init__() got multiple values for keyword argument 'self'

That's because init method is present and has self parameter.

There are several ways to overcome this issue:

  1. rename self parameter in init method to some unique string/guid. Here how it could look like:
    def __init__(_8629116b_4d94_42d9_bee1_d8374f6938a7_, *args, **kwargs):
        object.__setattr__(_8629116b_4d94_42d9_bee1_d8374f6938a7_, '__parent', kwargs.pop('__parent', None))
        object.__setattr__(_8629116b_4d94_42d9_bee1_d8374f6938a7_, '__key', kwargs.pop('__key', None))
        for arg in args:
            if not arg:
                continue
            elif isinstance(arg, dict):
                for key, val in arg.items():
                    _8629116b_4d94_42d9_bee1_d8374f6938a7_[key] = _8629116b_4d94_42d9_bee1_d8374f6938a7_._hook(val)
            elif isinstance(arg, tuple) and (not isinstance(arg[0], tuple)):
                _8629116b_4d94_42d9_bee1_d8374f6938a7_[arg[0]] = _8629116b_4d94_42d9_bee1_d8374f6938a7_._hook(arg[1])
            else:
                for key, val in iter(arg):
                    _8629116b_4d94_42d9_bee1_d8374f6938a7_[key] = _8629116b_4d94_42d9_bee1_d8374f6938a7_._hook(val)

        for key, val in kwargs.items():
            _8629116b_4d94_42d9_bee1_d8374f6938a7_[key] = _8629116b_4d94_42d9_bee1_d8374f6938a7_._hook(val)

Though, it's still impossible to use key with _8629116b_4d94_42d9_bee1_d8374f6938a7_ name :)
2. create factory method, which will protect field names and the unprotect them. Here is an example.

Not sure whether you are interested in fixing it, but I thought you would be interested in it.

@mewwts
Copy link
Owner

mewwts commented Jun 9, 2017

Thanks for reaching out!
There's actually two other field names which are problematic; __parent and __key which are used other places in the code. For now I think doing __self instead of self should limit the impact of this?

@grundic
Copy link
Author

grundic commented Jun 12, 2017

Hi @mewwts,
Yep, __self should be less possible option for some JSON API, where self is actually part of it :)

@mewwts
Copy link
Owner

mewwts commented Jun 12, 2017

Right, thanks. In any case keep in mind that you can use self as a key, just not as a kwarg.

mewwts added a commit that referenced this issue Jun 12, 2017
@mewwts mewwts closed this as completed Oct 2, 2017
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

2 participants