Skip to content

Commit

Permalink
also override del and in methdos
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Oct 19, 2024
1 parent 435320d commit 51f5922
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/pymatgen/io/vasp/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,11 @@ def __getitem__(self, key: str) -> Any:
"""
return super().__getitem__(key.strip().upper())

def get(self, key: str, default: Any = None) -> Any:
"""
Get a value for a case-insensitive key, return default if not found.
"""
return super().get(key.strip().upper(), default)
def __delitem__(self, key: str) -> None:
super().__delitem__(key.strip().upper())

def __contains__(self, key: str) -> bool:
return super().__contains__(key.upper().strip())

def __str__(self) -> str:
return self.get_str(sort_keys=True, pretty=False)
Expand All @@ -779,6 +779,12 @@ def __add__(self, other: Self) -> Self:
params[key] = val
return type(self)(params)

def get(self, key: str, default: Any = None) -> Any:
"""
Get a value for a case-insensitive key, return default if not found.
"""
return super().get(key.strip().upper(), default)

def as_dict(self) -> dict:
"""MSONable dict."""
dct = dict(self)
Expand Down
8 changes: 8 additions & 0 deletions tests/io/vasp/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,14 @@ def test_key_case_insensitive(self):
incar[test_tag.upper()] = 500
assert incar[test_tag.lower()] == 500

# Test delete
del incar["algo"]
assert "ALGO" not in incar

# Test membership check
assert test_tag.upper() in incar
assert test_tag.lower() in incar

# Test update
incar.update({test_tag.lower(): 510})
assert incar[test_tag] == 510
Expand Down

0 comments on commit 51f5922

Please sign in to comment.