This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ebw
Recipients ebw, r.david.murray, rhettinger, serhiy.storchaka
Date 2018-11-28.16:00:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I actually have a real life use case of that problem. Using the eval() function of pandas need to use a custom resolver:

class Resolver(object):
    def __getitem__(self, name):
        if name == "test":
            return 0.5
            raise KeyError

resolvers = (Resolver(), )
pd.eval("test", resolvers=resolvers)

But pandas store the resolvers used for eval() in a DeepChainMap and uses bool(len(self.resolvers)) in the property has_resolvers. Which with my custom Resolver raise a KeyError because __getiem__ gets call with name = 0.

The way to solve/bypass the problem was to make Resolver a inherit from UserDict.

class Resolver(UserDict):
    def __init__(self, data={}):
        super(Resolver, self).__init__(data)["test"] = None
    def __getitem__(self, name):
        if (name == "test"):
            return 0.5
            super(Resolver, self).__getitem__(name)

resolvers = (Resolver(), )
pd.eval("test", resolvers=resolvers)

I had to add a dummy "test" value to dict to avoid len(self.resolvers) to be 0. I tried to implement Resolver.__len__, keys... but it didn't help has ChainMap compute the len using set().union(*self.maps).
Date User Action Args
2018-11-28 16:00:26ebwsetrecipients: + ebw, rhettinger, r.david.murray, serhiy.storchaka
2018-11-28 16:00:26ebwsetmessageid: <>
2018-11-28 16:00:26ebwlinkissue30689 messages
2018-11-28 16:00:26ebwcreate