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

Remove redundant total_ordering decorator usage #4203

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/pymatgen/core/periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,6 @@ def print_periodic_table(filter_function: Callable | None = None) -> None:
print(" ".join(row_str))


@functools.total_ordering
class Element(ElementBase):
"""Enum representing an element in the periodic table."""

Expand Down Expand Up @@ -1597,14 +1596,12 @@ def from_dict(cls, dct: dict) -> Self:
return cls(dct["element"], dct["oxidation_state"], spin=dct.get("spin"))


@functools.total_ordering
class Specie(Species):
"""This maps the historical grammatically inaccurate Specie to Species
to maintain backwards compatibility.
"""


@functools.total_ordering
class DummySpecie(DummySpecies):
"""This maps the historical grammatically inaccurate DummySpecie to DummySpecies
to maintain backwards compatibility.
Expand Down
17 changes: 10 additions & 7 deletions src/pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations

import collections
import collections.abc
import contextlib
import functools
import inspect
Expand Down Expand Up @@ -1098,22 +1099,24 @@ def __init__(
self._properties = properties or {}

def __eq__(self, other: object) -> bool:
"""Define equality by comparing all three attributes: lattice, sites, properties."""
needed_attrs = ("lattice", "sites", "properties")

# Return NotImplemented as in https://docs.python.org/3/library/functools.html#functools.total_ordering
Copy link
Contributor Author

@DanielYang59 DanielYang59 Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess total_ordering was applied on IStructure at some point but I believe this comment should be removed now (currently it more servers as an equality definition):

class IStructure(SiteCollection, MSONable):

Also current implementation of IStructure doesn't fulfill the prerequisite of applying total_ordering (only __eq__ is defined):

The class must define one of lt(), le(), gt(), or ge(). In addition, the class should supply an eq() method.

if not all(hasattr(other, attr) for attr in needed_attrs):
return NotImplemented

# TODO (DanielYang59): fix below type
other = cast(Structure, other) # make mypy happy
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved

if other is self:
return True
if len(self) != len(other):

if hasattr(other, "__len__") and len(self) != len(other):
return False
if self.lattice != other.lattice:

if hasattr(other, "lattice") and self.lattice != other.lattice:
return False
if self.properties != other.properties:
if hasattr(other, "properties") and self.properties != other.properties:
return False

if not hasattr(other, "__contains__"):
return False
return all(site in other for site in self)

Expand Down
Loading