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

Reapply zopen explicit UTF-8 encoding, enable optional EncodingWarning PEP 597 in tests #4222

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0457c44
explicit mode for zopen
DanielYang59 Dec 7, 2024
4fccd59
fix bad mode for cifwrite
DanielYang59 Dec 7, 2024
88c1359
remove tag
DanielYang59 Dec 7, 2024
7df79b5
remove an unnecessary cast to int
DanielYang59 Dec 7, 2024
c9bf970
TODO: test monty zopen change
DanielYang59 Dec 8, 2024
4925a3e
fix implicit mode
DanielYang59 Dec 8, 2024
be411a0
Revert "TODO: test monty zopen change"
DanielYang59 Dec 8, 2024
e75c196
Revert "Revert "TODO: test monty zopen change""
DanielYang59 Dec 8, 2024
4d699d7
Revert "Revert "Revert "TODO: test monty zopen change"""
DanielYang59 Dec 8, 2024
14d501a
explicit text mode for stout
DanielYang59 Dec 8, 2024
3fdf8cb
some explicit utf-8 for zopen
DanielYang59 Dec 8, 2024
d899c87
TO BE REVERTED: test monty pr
DanielYang59 Dec 8, 2024
0ece310
add the rest encoding
DanielYang59 Dec 8, 2024
caee02a
Revert "TO BE REVERTED: test monty pr"
DanielYang59 Dec 8, 2024
cdf2987
Merge branch 'master' into zopen-explicit-mode
DanielYang59 Dec 11, 2024
e94d922
Merge branch 'master' into zopen-explicit-mode
DanielYang59 Dec 11, 2024
bbd53bf
enable PYTHONWARNDEFAULTENCODING
DanielYang59 Dec 12, 2024
2ccf30c
TO BE REVERTED: test leaving out encoding
DanielYang59 Dec 12, 2024
440a72f
fix zopen encoding warning as error
DanielYang59 Dec 12, 2024
24617c8
Revert "TO BE REVERTED: test leaving out encoding"
DanielYang59 Dec 12, 2024
50cf71b
add custom warning
DanielYang59 Dec 12, 2024
6438670
Reapply "TO BE REVERTED: test leaving out encoding"
DanielYang59 Dec 12, 2024
b242c8f
more descriptive comment
DanielYang59 Dec 12, 2024
2f3c320
Revert "Reapply "TO BE REVERTED: test leaving out encoding""
DanielYang59 Dec 12, 2024
d00ec1b
Merge branch 'master' into zopen-explicit-mode
DanielYang59 Dec 13, 2024
8fa2fa4
Merge branch 'master' into zopen-explicit-mode
DanielYang59 Jan 1, 2025
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: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ jobs:
runs-on: ${{ matrix.config.os }}

env:
PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}
MPLBACKEND: Agg # non-interactive backend for matplotlib
PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}
PYTHONWARNDEFAULTENCODING: "true" # PEP 597: Enable optional EncodingWarning

steps:
- name: Check out repo
Expand Down
2 changes: 1 addition & 1 deletion dev_scripts/potcar_scrambler.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def scramble_single_potcar(self, potcar: PotcarSingle) -> str:
return scrambled_potcar_str

def to_file(self, filename: str) -> None:
with zopen(filename, mode="wt") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(self.scrambled_potcars_str)

@classmethod
Expand Down
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,11 @@ docstring-code-format = true
[tool.pytest.ini_options]
addopts = "--durations=30 --quiet -r xXs --color=yes --import-mode=importlib"
filterwarnings = [
# NOTE: the last matching option is used
"ignore::Warning", # Ignore all Warning
"default::FutureWarning", # Show FutureWarnings
"default::DeprecationWarning", # Show DeprecationWarnings
# NOTE: the LAST matching option would be used
"ignore::UserWarning", # Ignore UserWarning
"error:We strongly encourage explicit `encoding`:EncodingWarning", # Mark `zopen` EncodingWarning as error
# TODO: remove the following filter once `monty.io` dropped custom EncodingWarning
"error:We strongly encourage explicit `encoding`:monty.io.EncodingWarning",
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved
# TODO: pybtex (perhaps some others) emits the following warnings
'ignore:pkg_resources is deprecated as an API:DeprecationWarning',
'ignore:distutils Version classes are deprecated:DeprecationWarning',
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/apps/borg/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def _get_transformation_history(path: PathLike):
"""Check for a transformations.json* file and return the history."""
if trans_json := glob(f"{path!s}/transformations.json*"):
try:
with zopen(trans_json[0]) as file:
with zopen(trans_json[0], mode="rt", encoding="utf-8") as file:
return json.load(file)["history"]
except Exception:
return None
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/apps/borg/queen.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ def save_data(self, filename: PathLike) -> None:
that if the filename ends with gz or bz2, the relevant gzip
or bz2 compression will be applied.
"""
with zopen(filename, mode="wt") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
json.dump(list(self._data), file, cls=MontyEncoder)

def load_data(self, filename: PathLike) -> None:
"""Load assimilated data from a file."""
with zopen(filename, mode="rt") as file:
with zopen(filename, mode="rt", encoding="utf-8") as file:
self._data = json.load(file, cls=MontyDecoder)


Expand Down
18 changes: 9 additions & 9 deletions src/pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2953,15 +2953,15 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
elif fmt == "json" or fnmatch(filename.lower(), "*.json*"):
json_str = json.dumps(self.as_dict())
if filename:
with zopen(filename, mode="wt") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(json_str)
return json_str
elif fmt == "xsf" or fnmatch(filename.lower(), "*.xsf*"):
from pymatgen.io.xcrysden import XSF

res_str = XSF(self).to_str()
if filename:
with zopen(filename, mode="wt", encoding="utf8") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(res_str)
return res_str
elif (
Expand All @@ -2987,15 +2987,15 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
yaml.dump(self.as_dict(), str_io)
yaml_str = str_io.getvalue()
if filename:
with zopen(filename, mode="wt") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(yaml_str)
return yaml_str
elif fmt == "aims" or fnmatch(filename, "geometry.in"):
from pymatgen.io.aims.inputs import AimsGeometryIn

geom_in = AimsGeometryIn.from_structure(self)
if filename:
with zopen(filename, mode="w") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(geom_in.get_header(filename))
file.write(geom_in.content)
file.write("\n")
Expand All @@ -3010,7 +3010,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:

res_str = ResIO.structure_to_str(self)
if filename:
with zopen(filename, mode="wt", encoding="utf8") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(res_str)
return res_str
elif fmt == "pwmat" or fnmatch(filename.lower(), "*.pwmat") or fnmatch(filename.lower(), "*.config"):
Expand Down Expand Up @@ -3173,7 +3173,7 @@ def from_file(
return struct

fname = os.path.basename(filename)
with zopen(filename, mode="rt", errors="replace") as file:
with zopen(filename, mode="rt", errors="replace", encoding="utf-8") as file:
contents = file.read()
if fnmatch(fname.lower(), "*.cif*") or fnmatch(fname.lower(), "*.mcif*"):
return cls.from_str(
Expand Down Expand Up @@ -3919,7 +3919,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
elif fmt == "json" or fnmatch(filename, "*.json*") or fnmatch(filename, "*.mson*"):
json_str = json.dumps(self.as_dict())
if filename:
with zopen(filename, mode="wt", encoding="utf8") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(json_str)
return json_str
elif fmt in {"yaml", "yml"} or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
Expand All @@ -3928,7 +3928,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
yaml.dump(self.as_dict(), str_io)
yaml_str = str_io.getvalue()
if filename:
with zopen(filename, mode="wt", encoding="utf8") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(yaml_str)
return yaml_str
else:
Expand Down Expand Up @@ -4010,7 +4010,7 @@ def from_file(cls, filename: PathLike) -> Self | None:
"""
filename = str(filename)

with zopen(filename) as file:
with zopen(filename, mode="rt", encoding="utf-8") as file:
contents = file.read()
fname = filename.lower()
if fnmatch(fname, "*.xyz*"):
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/core/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def write_Xdatcar(

xdatcar_str = "\n".join(lines) + "\n"

with zopen(filename, mode="wt") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(xdatcar_str)

def as_dict(self) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/adf.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def _parse_logfile(self, logfile):
# The last non-empty line of the logfile must match the end pattern.
# Otherwise the job has some internal failure. The TAPE13 part of the
# ADF manual has a detailed explanation.
with zopen(logfile, mode="rt") as file:
with zopen(logfile, mode="rt", encoding="utf-8") as file:
for line in reverse_readline(file):
if line == "":
continue
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/aims/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def from_file(cls, filepath: str | Path) -> Self:
Returns:
AimsGeometryIn: The input object represented in the file
"""
with zopen(filepath, mode="rt") as in_file:
with zopen(filepath, mode="rt", encoding="utf-8") as in_file:
content = in_file.read()
return cls.from_str(content)

Expand Down Expand Up @@ -759,7 +759,7 @@ def from_file(cls, filename: str, label: str | None = None) -> Self:
Returns:
AimsSpeciesFile
"""
with zopen(filename, mode="rt") as file:
with zopen(filename, mode="rt", encoding="utf-8") as file:
return cls(data=file.read(), label=label)

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions src/pymatgen/io/cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def from_file(cls, filename: PathLike) -> Self:
Returns:
CifFile
"""
with zopen(filename, mode="rt", errors="replace") as file:
with zopen(filename, mode="rt", errors="replace", encoding="utf-8") as file:
return cls.from_str(file.read())


Expand Down Expand Up @@ -1760,9 +1760,9 @@ def cif_file(self) -> CifFile:

def write_file(
self,
filename: str | Path,
mode: Literal["w", "a", "wt", "at"] = "w",
filename: PathLike,
mode: Literal["wt", "at"] = "wt",
) -> None:
"""Write the CIF file."""
with zopen(filename, mode=mode) as file:
with zopen(filename, mode=mode, encoding="utf-8") as file:
file.write(str(self))
6 changes: 3 additions & 3 deletions src/pymatgen/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def to_cube(self, filename, comment: str = ""):
filename (str): Name of the cube file to be written.
comment (str): If provided, this will be added to the second comment line
"""
with zopen(filename, mode="wt") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(f"# Cube file for {self.structure.formula} generated by Pymatgen\n")
file.write(f"# {comment}\n")
file.write(f"\t {len(self.structure)} 0.000000 0.000000 0.000000\n")
Expand Down Expand Up @@ -386,7 +386,7 @@ def from_cube(cls, filename: str | Path) -> Self:
Args:
filename (str): of the cube to read
"""
file = zopen(filename, mode="rt")
file = zopen(filename, mode="rt", encoding="utf-8")

# skip header lines
file.readline()
Expand Down Expand Up @@ -529,7 +529,7 @@ def __getitem__(self, item):
f"No parser defined for {item}. Contents are returned as a string.",
stacklevel=2,
)
with zopen(fpath, "rt") as f:
with zopen(fpath, mode="rt", encoding="utf-8") as f:
return f.read()

def get_files_by_name(self, name: str) -> dict[str, Any]:
Expand Down
6 changes: 3 additions & 3 deletions src/pymatgen/io/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def write_file(self, filename: PathLike) -> None:
Args:
filename: The filename to output to, including path.
"""
with zopen(Path(filename), mode="wt") as file:
with zopen(Path(filename), mode="wt", encoding="utf-8") as file:
file.write(self.get_str())

@classmethod
Expand Down Expand Up @@ -102,7 +102,7 @@ def from_file(cls, path: PathLike) -> None:
Returns:
InputFile
"""
with zopen(Path(path), mode="rt") as file:
with zopen(Path(path), mode="rt", encoding="utf-8") as file:
return cls.from_str(file.read()) # from_str not implemented


Expand Down Expand Up @@ -218,7 +218,7 @@ def write_input(
if isinstance(contents, InputFile):
contents.write_file(file_path)
else:
with zopen(file_path, mode="wt") as file:
with zopen(file_path, mode="wt", encoding="utf-8") as file:
file.write(str(contents))

if zip_inputs:
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/cp2k/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def _from_dict(cls, dct: dict):
@classmethod
def from_file(cls, filename: str | Path) -> Self:
"""Initialize from a file."""
with zopen(filename, mode="rt") as file:
with zopen(filename, mode="rt", encoding="utf-8") as file:
txt = preprocessor(file.read(), os.path.dirname(file.name))
return cls.from_str(txt)

Expand Down
16 changes: 8 additions & 8 deletions src/pymatgen/io/cp2k/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def parse_initial_structure(self):
)

coord_table = []
with zopen(self.filename, mode="rt") as file:
with zopen(self.filename, mode="rt", encoding="utf-8") as file:
while True:
line = file.readline()
if re.search(r"Atom\s+Kind\s+Element\s+X\s+Y\s+Z\s+Z\(eff\)\s+Mass", line):
Expand Down Expand Up @@ -789,7 +789,7 @@ def parse_atomic_kind_info(self):
except (TypeError, IndexError, ValueError):
atomic_kind_info[kind]["total_pseudopotential_energy"] = None

with zopen(self.filename, mode="rt") as file:
with zopen(self.filename, mode="rt", encoding="utf-8") as file:
j = -1
lines = file.readlines()
for k, line in enumerate(lines):
Expand Down Expand Up @@ -1010,7 +1010,7 @@ def parse_mo_eigenvalues(self):
eigenvalues = []
efermi = []

with zopen(self.filename, mode="rt") as file:
with zopen(self.filename, mode="rt", encoding="utf-8") as file:
lines = iter(file.readlines())
for line in lines:
try:
Expand Down Expand Up @@ -1349,7 +1349,7 @@ def parse_hyperfine(self, hyperfine_filename=None):
else:
return None

with zopen(hyperfine_filename, mode="rt") as file:
with zopen(hyperfine_filename, mode="rt", encoding="utf-8") as file:
lines = [line for line in file.read().split("\n") if line]

hyperfine = [[] for _ in self.ionic_steps]
Expand All @@ -1370,7 +1370,7 @@ def parse_gtensor(self, gtensor_filename=None):
else:
return None

with zopen(gtensor_filename, mode="rt") as file:
with zopen(gtensor_filename, mode="rt", encoding="utf-8") as file:
lines = [line for line in file.read().split("\n") if line]

data = {}
Expand Down Expand Up @@ -1407,7 +1407,7 @@ def parse_chi_tensor(self, chi_filename=None):
else:
return None

with zopen(chi_filename, mode="rt") as file:
with zopen(chi_filename, mode="rt", encoding="utf-8") as file:
lines = [line for line in file.read().split("\n") if line]

data = {k: [] for k in "chi_soft chi_local chi_total chi_total_ppm_cgs PV1 PV2 PV3 ISO ANISO".split()}
Expand Down Expand Up @@ -1554,7 +1554,7 @@ def read_table_pattern(
row_pattern, or a dict in case that named capturing groups are defined by
row_pattern.
"""
with zopen(self.filename, mode="rt") as file:
with zopen(self.filename, mode="rt", encoding="utf-8") as file:
if strip:
lines = file.readlines()
text = "".join(
Expand Down Expand Up @@ -1691,7 +1691,7 @@ def parse_pdos(dos_file=None, spin_channel=None, total=False):
"""
spin = Spin(spin_channel) if spin_channel else Spin.down if "BETA" in os.path.split(dos_file)[-1] else Spin.up

with zopen(dos_file, mode="rt") as file:
with zopen(dos_file, mode="rt", encoding="utf-8") as file:
lines = file.readlines()
kind = re.search(r"atomic kind\s(.*)\sat iter", lines[0]) or re.search(r"list\s(\d+)\s(.*)\sat iter", lines[0])
kind = kind.groups()[0]
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/cp2k/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def preprocessor(data: str, dir: str = ".") -> str: # noqa: A002
raise ValueError(f"length of inc should be 2, got {len(inc)}")
inc = inc[1].strip("'")
inc = inc.strip('"')
with zopen(os.path.join(dir, inc)) as file:
with zopen(os.path.join(dir, inc), mode="rt", encoding="utf-8") as file:
data = re.sub(rf"{incl}", file.read(), data)
variable_sets = re.findall(r"(@SET.+)", data, re.IGNORECASE)
for match in variable_sets:
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/cssr.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def write_file(self, filename):
Args:
filename (str): Filename to write to.
"""
with zopen(filename, mode="wt") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(str(self) + "\n")

@classmethod
Expand Down Expand Up @@ -98,5 +98,5 @@ def from_file(cls, filename: str | Path) -> Self:
Returns:
Cssr object.
"""
with zopen(filename, mode="rt") as file:
with zopen(filename, mode="rt", encoding="utf-8") as file:
return cls.from_str(file.read())
2 changes: 1 addition & 1 deletion src/pymatgen/io/exciting/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def from_file(cls, filename: str | Path) -> Self:
Returns:
ExcitingInput
"""
with zopen(filename, mode="rt") as file:
with zopen(filename, mode="rt", encoding="utf-8") as file:
data = file.read().replace("\n", "")
return cls.from_str(data)

Expand Down
Loading
Loading