Skip to content

Commit

Permalink
Fix exception for del config[key] (#7391)
Browse files Browse the repository at this point in the history
* Fix exception for `del config[key]`

Also, make `cfg.__setitem__` faster

* Fix regex in test for Python 3.10
  • Loading branch information
eriknw authored Apr 4, 2024
1 parent 26d218b commit 754173b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions networkx/utils/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,16 @@ def __getitem__(self, key):

def __setitem__(self, key, value):
try:
setattr(self, key, value)
self.__setattr__(key, value)
except AttributeError as err:
raise KeyError(*err.args) from None

def __delitem__(self, key):
try:
self.__delattr__(key)
except AttributeError as err:
raise KeyError(*err.args) from None

__delitem__ = __delattr__
_ipython_key_completions_ = __dir__ # config["<TAB>

# Go ahead and make it a `collections.abc.Mapping`
Expand Down
4 changes: 4 additions & 0 deletions networkx/utils/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ class FlexibleConfig(Config, strict=False):
del cfg["y"]
assert len(cfg) == 0
assert list(cfg) == []
with pytest.raises(AttributeError, match="y"):
del cfg.y
with pytest.raises(KeyError, match="y"):
del cfg["y"]
with pytest.raises(TypeError, match="missing 1 required keyword-only"):
FlexibleConfig()
# Be strict when first creating the config object
Expand Down

0 comments on commit 754173b

Please sign in to comment.