Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception for del config[key] #7391

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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