@@ -5183,3 +5183,79 @@ test_no_setattr()
51835183test_no_setattr_nonnative()
51845184
51855185[typing fixtures/typing-full.pyi]
5186+
5187+ [case testDelAttrWithDeletableAttr]
5188+ from testutil import assertRaises
5189+
5190+ class DelAttr:
5191+ __deletable__ = ["regular_attr"]
5192+
5193+ _attributes: dict[str, object]
5194+ regular_attr: int
5195+
5196+ def __init__(self, regular_attr: int) -> None:
5197+ object.__setattr__(self, "regular_attr", regular_attr)
5198+ object.__setattr__(self, "_attributes", {})
5199+
5200+ def __setattr__(self, key: str, val: object) -> None:
5201+ if key == "regular_attr":
5202+ object.__setattr__(self, key, val)
5203+ else:
5204+ self._attributes[key] = val
5205+
5206+ def __delattr__(self, key: str) -> None:
5207+ if key == "regular_attr":
5208+ object.__delattr__(self, "regular_attr")
5209+ else:
5210+ del self._attributes[key]
5211+
5212+ def test_deletable_attr() -> None:
5213+ i = DelAttr(101)
5214+ assert i.regular_attr == 101
5215+
5216+ del i.regular_attr
5217+ with assertRaises(AttributeError):
5218+ i.regular_attr
5219+
5220+ i.regular_attr = 102
5221+ assert i.regular_attr == 102
5222+
5223+ [case testDelAttrWithDeletableAttrInterpreted]
5224+ class DelAttr:
5225+ __deletable__ = ["regular_attr"]
5226+
5227+ _attributes: dict[str, object]
5228+ regular_attr: int
5229+
5230+ def __init__(self, regular_attr: int) -> None:
5231+ object.__setattr__(self, "regular_attr", regular_attr)
5232+ object.__setattr__(self, "_attributes", {})
5233+
5234+ def __setattr__(self, key: str, val: object) -> None:
5235+ if key == "regular_attr":
5236+ object.__setattr__(self, key, val)
5237+ else:
5238+ self._attributes[key] = val
5239+
5240+ def __delattr__(self, key: str) -> None:
5241+ if key == "regular_attr":
5242+ object.__delattr__(self, "regular_attr")
5243+ else:
5244+ del self._attributes[key]
5245+
5246+ [file driver.py]
5247+ from native import DelAttr
5248+ from testutil import assertRaises
5249+
5250+ def test_deletable_attr() -> None:
5251+ i = DelAttr(101)
5252+ assert i.regular_attr == 101
5253+
5254+ del i.regular_attr
5255+ with assertRaises(AttributeError):
5256+ i.regular_attr
5257+
5258+ i.regular_attr = 102
5259+ assert i.regular_attr == 102
5260+
5261+ test_deletable_attr()
0 commit comments