Skip to content

Commit 1cb53f7

Browse files
Removed __orig_class__ attribute out of AttrDict instances (#1877) (#1880)
(cherry picked from commit ec60616) Co-authored-by: Miguel Grinberg <[email protected]>
1 parent 1b93757 commit 1cb53f7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: elasticsearch_dsl/utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ def __delitem__(self, key: str) -> None:
215215
del self._d_[key]
216216

217217
def __setattr__(self, name: str, value: _ValT) -> None:
218-
if name in self._d_ or not hasattr(self.__class__, name):
218+
# the __orig__class__ attribute has to be treated as an exception, as
219+
# is it added to an object when it is instantiated with type arguments
220+
if (
221+
name in self._d_ or not hasattr(self.__class__, name)
222+
) and name != "__orig_class__":
219223
self._d_[name] = value
220224
else:
221225
# there is an attribute on the class (could be property, ..) - don't add it as field

Diff for: tests/test_utils.py

+11
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,23 @@ class MyAttrDict(utils.AttrDict[str]):
4545
assert isinstance(l[:][0], MyAttrDict)
4646

4747

48+
def test_attrlist_with_type_argument() -> None:
49+
a = utils.AttrList[str](["a", "b"])
50+
assert list(a) == ["a", "b"]
51+
52+
4853
def test_attrdict_keys_items() -> None:
4954
a = utils.AttrDict({"a": {"b": 42, "c": 47}, "d": "e"})
5055
assert list(a.keys()) == ["a", "d"]
5156
assert list(a.items()) == [("a", {"b": 42, "c": 47}), ("d", "e")]
5257

5358

59+
def test_attrdict_with_type_argument() -> None:
60+
a = utils.AttrDict[str]({"a": "b"})
61+
assert list(a.keys()) == ["a"]
62+
assert list(a.items()) == [("a", "b")]
63+
64+
5465
def test_merge() -> None:
5566
a: utils.AttrDict[Any] = utils.AttrDict({"a": {"b": 42, "c": 47}})
5667
b = {"a": {"b": 123, "d": -12}, "e": [1, 2, 3]}

0 commit comments

Comments
 (0)