Skip to content

Commit

Permalink
Removed __orig_class__ attribute out of AttrDict instances
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Aug 13, 2024
1 parent 6bd7aa3 commit e5c4556
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion elasticsearch_dsl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ def __delitem__(self, key: str) -> None:
del self._d_[key]

def __setattr__(self, name: str, value: _ValT) -> None:
if name in self._d_ or not hasattr(self.__class__, name):
# the __orig__class__ attribute has to be treated as an exception, as
# is it added to an object when it is instantiated with type arguments
if (
name in self._d_ or not hasattr(self.__class__, name)
) and name != "__orig_class__":
self._d_[name] = value
else:
# there is an attribute on the class (could be property, ..) - don't add it as field
Expand Down
11 changes: 11 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,23 @@ class MyAttrDict(utils.AttrDict[str]):
assert isinstance(l[:][0], MyAttrDict)


def test_attrlist_with_type_argument() -> None:
a = utils.AttrList[str](["a", "b"])
assert list(a) == ["a", "b"]


def test_attrdict_keys_items() -> None:
a = utils.AttrDict({"a": {"b": 42, "c": 47}, "d": "e"})
assert list(a.keys()) == ["a", "d"]
assert list(a.items()) == [("a", {"b": 42, "c": 47}), ("d", "e")]


def test_attrdict_with_type_argument() -> None:
a = utils.AttrDict[str]({"a": "b"})
assert list(a.keys()) == ["a"]
assert list(a.items()) == [("a", "b")]


def test_merge() -> None:
a: utils.AttrDict[Any] = utils.AttrDict({"a": {"b": 42, "c": 47}})
b = {"a": {"b": 123, "d": -12}, "e": [1, 2, 3]}
Expand Down

0 comments on commit e5c4556

Please sign in to comment.