Skip to content

Commit 5abe81c

Browse files
authored
Remove redundant total_ordering decorator usage (#4203)
* remove redundant total ordering * clean up comment * clear up type * clean up import of collections * mypy fix * use instance check * revert type check change for now * clean up docstring
1 parent 7837761 commit 5abe81c

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/pymatgen/core/composition.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
if TYPE_CHECKING:
2727
from collections.abc import Generator, Iterator
28-
from typing import Any, ClassVar
28+
from typing import Any, ClassVar, Literal
2929

3030
from typing_extensions import Self
3131

@@ -774,17 +774,25 @@ def to_reduced_dict(self) -> dict[str, float]:
774774
def to_weight_dict(self) -> dict[str, float]:
775775
"""
776776
Returns:
777-
dict[str, float] with weight fraction of each component {"Ti": 0.90, "V": 0.06, "Al": 0.04}.
777+
dict[str, float]: weight fractions of each component, e.g. {"Ti": 0.90, "V": 0.06, "Al": 0.04}.
778778
"""
779779
return {str(el): self.get_wt_fraction(el) for el in self.elements}
780780

781781
@property
782-
def to_data_dict(self) -> dict[str, Any]:
782+
def to_data_dict(
783+
self,
784+
) -> dict[
785+
Literal["reduced_cell_composition", "unit_cell_composition", "reduced_cell_formula", "elements", "nelements"],
786+
Any,
787+
]:
783788
"""
784789
Returns:
785-
A dict with many keys and values relating to Composition/Formula,
786-
including reduced_cell_composition, unit_cell_composition,
787-
reduced_cell_formula, elements and nelements.
790+
dict with the following keys:
791+
- reduced_cell_composition
792+
- unit_cell_composition
793+
- reduced_cell_formula
794+
- elements
795+
- nelements.
788796
"""
789797
return {
790798
"reduced_cell_composition": self.reduced_composition,

src/pymatgen/core/periodic_table.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,6 @@ def print_periodic_table(filter_function: Callable | None = None) -> None:
878878
print(" ".join(row_str))
879879

880880

881-
@functools.total_ordering
882881
class Element(ElementBase):
883882
"""Enum representing an element in the periodic table."""
884883

@@ -1600,14 +1599,12 @@ def from_dict(cls, dct: dict) -> Self:
16001599
return cls(dct["element"], dct["oxidation_state"], spin=dct.get("spin"))
16011600

16021601

1603-
@functools.total_ordering
16041602
class Specie(Species):
16051603
"""This maps the historical grammatically inaccurate Specie to Species
16061604
to maintain backwards compatibility.
16071605
"""
16081606

16091607

1610-
@functools.total_ordering
16111608
class DummySpecie(DummySpecies):
16121609
"""This maps the historical grammatically inaccurate DummySpecie to DummySpecies
16131610
to maintain backwards compatibility.

src/pymatgen/core/structure.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import warnings
2020
from abc import ABC, abstractmethod
2121
from collections import defaultdict
22-
from collections.abc import MutableSequence
2322
from fnmatch import fnmatch
24-
from io import StringIO
2523
from typing import TYPE_CHECKING, Literal, cast, get_args
2624

2725
import numpy as np
@@ -245,7 +243,7 @@ def sites(self) -> list[PeriodicSite] | tuple[PeriodicSite, ...]:
245243
def sites(self, sites: Sequence[PeriodicSite]) -> None:
246244
"""Set the sites in the Structure."""
247245
# If self is mutable Structure or Molecule, set _sites as list
248-
is_mutable = isinstance(self._sites, MutableSequence)
246+
is_mutable = isinstance(self._sites, collections.abc.MutableSequence)
249247
self._sites: list[PeriodicSite] | tuple[PeriodicSite, ...] = list(sites) if is_mutable else tuple(sites)
250248

251249
@abstractmethod
@@ -1099,9 +1097,8 @@ def __init__(
10991097
self._properties = properties or {}
11001098

11011099
def __eq__(self, other: object) -> bool:
1100+
"""Define equality by comparing all three attributes: lattice, sites, properties."""
11021101
needed_attrs = ("lattice", "sites", "properties")
1103-
1104-
# Return NotImplemented as in https://docs.python.org/3/library/functools.html#functools.total_ordering
11051102
if not all(hasattr(other, attr) for attr in needed_attrs):
11061103
return NotImplemented
11071104

@@ -1110,8 +1107,10 @@ def __eq__(self, other: object) -> bool:
11101107

11111108
if other is self:
11121109
return True
1110+
11131111
if len(self) != len(other):
11141112
return False
1113+
11151114
if self.lattice != other.lattice:
11161115
return False
11171116
if self.properties != other.properties:
@@ -2984,7 +2983,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
29842983
return Prismatic(self).to_str()
29852984
elif fmt in ("yaml", "yml") or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
29862985
yaml = YAML()
2987-
str_io = StringIO()
2986+
str_io = io.StringIO()
29882987
yaml.dump(self.as_dict(), str_io)
29892988
yaml_str = str_io.getvalue()
29902989
if filename:
@@ -3925,7 +3924,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
39253924
return json_str
39263925
elif fmt in {"yaml", "yml"} or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
39273926
yaml = YAML()
3928-
str_io = StringIO()
3927+
str_io = io.StringIO()
39293928
yaml.dump(self.as_dict(), str_io)
39303929
yaml_str = str_io.getvalue()
39313930
if filename:

0 commit comments

Comments
 (0)