19
19
import warnings
20
20
from abc import ABC , abstractmethod
21
21
from collections import defaultdict
22
- from collections .abc import MutableSequence
23
22
from fnmatch import fnmatch
24
- from io import StringIO
25
23
from typing import TYPE_CHECKING , Literal , cast , get_args
26
24
27
25
import numpy as np
@@ -245,7 +243,7 @@ def sites(self) -> list[PeriodicSite] | tuple[PeriodicSite, ...]:
245
243
def sites (self , sites : Sequence [PeriodicSite ]) -> None :
246
244
"""Set the sites in the Structure."""
247
245
# 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 )
249
247
self ._sites : list [PeriodicSite ] | tuple [PeriodicSite , ...] = list (sites ) if is_mutable else tuple (sites )
250
248
251
249
@abstractmethod
@@ -1099,9 +1097,8 @@ def __init__(
1099
1097
self ._properties = properties or {}
1100
1098
1101
1099
def __eq__ (self , other : object ) -> bool :
1100
+ """Define equality by comparing all three attributes: lattice, sites, properties."""
1102
1101
needed_attrs = ("lattice" , "sites" , "properties" )
1103
-
1104
- # Return NotImplemented as in https://docs.python.org/3/library/functools.html#functools.total_ordering
1105
1102
if not all (hasattr (other , attr ) for attr in needed_attrs ):
1106
1103
return NotImplemented
1107
1104
@@ -1110,8 +1107,10 @@ def __eq__(self, other: object) -> bool:
1110
1107
1111
1108
if other is self :
1112
1109
return True
1110
+
1113
1111
if len (self ) != len (other ):
1114
1112
return False
1113
+
1115
1114
if self .lattice != other .lattice :
1116
1115
return False
1117
1116
if self .properties != other .properties :
@@ -2984,7 +2983,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
2984
2983
return Prismatic (self ).to_str ()
2985
2984
elif fmt in ("yaml" , "yml" ) or fnmatch (filename , "*.yaml*" ) or fnmatch (filename , "*.yml*" ):
2986
2985
yaml = YAML ()
2987
- str_io = StringIO ()
2986
+ str_io = io . StringIO ()
2988
2987
yaml .dump (self .as_dict (), str_io )
2989
2988
yaml_str = str_io .getvalue ()
2990
2989
if filename :
@@ -3925,7 +3924,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
3925
3924
return json_str
3926
3925
elif fmt in {"yaml" , "yml" } or fnmatch (filename , "*.yaml*" ) or fnmatch (filename , "*.yml*" ):
3927
3926
yaml = YAML ()
3928
- str_io = StringIO ()
3927
+ str_io = io . StringIO ()
3929
3928
yaml .dump (self .as_dict (), str_io )
3930
3929
yaml_str = str_io .getvalue ()
3931
3930
if filename :
0 commit comments