Skip to content

Support inheritance in merge operators | and |=  #151

Description

@LutingWang

#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__?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions