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

Support inheritance in merge operators | and |= #151

Open
LutingWang opened this issue Jan 3, 2023 · 0 comments
Open

Support inheritance in merge operators | and |= #151

LutingWang opened this issue Jan 3, 2023 · 0 comments

Comments

@LutingWang
Copy link

LutingWang commented Jan 3, 2023

#127 introduces merge operators. However, the current implementation has two problems:

  1. Since Dict is a subclass of dict, is it necessary to check by isinstance(other, (Dict, dict))? Wouldn't isinstance(other, dict) be enough?
  2. | and |= always return Dict instances, even if the operands may be instances of subclasses. To expatiate, consider the following snippet:
from addict import Dict
class MyDict(Dict):
    def func(self):
        print(self)
a = MyDict()
b = MyDict()
c = a | b
c.func()  # TypeError: 'Dict' object is not callable

Intuitively, we would expect that c is an instance of MyDict but is actually of type Dict. Moreover, since Dict does not have a method called func, when looking up c.func, it returns a new empty Dict, causing the error message to be very misleading.

Will it be possible to return an instance of type self.__class__ in Dict.__or__ and Dict.__ror__?

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

1 participant