Skip to content

Commit

Permalink
Merge pull request astropy#16925 from neutrinoceros/cosmology/rf/pth_…
Browse files Browse the repository at this point in the history
…checks

MNT: Run PTH flake test in prep for supporting pathlib (cosmology)
  • Loading branch information
nstarman authored Sep 3, 2024
2 parents f5c2c4a + 3d9b483 commit 1b563c2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
1 change: 0 additions & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ lint.unfixable = [
"RET505", # superfluous-else-return
]
"astropy/cosmology/*" = [
"PTH", # all flake8-use-pathlib
"C408", # unnecessary-collection-call
"PT019", # pytest-fixture-param-without-value
]
Expand Down
19 changes: 9 additions & 10 deletions astropy/cosmology/_io/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import os
from pathlib import Path

import pytest

Expand All @@ -20,7 +21,7 @@ def read_json(filename, **kwargs):
Parameters
----------
filename : str
filename : str | bytes | os.PathLike
**kwargs
Keyword arguments into :meth:`~astropy.cosmology.Cosmology.from_format`
Expand All @@ -30,8 +31,7 @@ def read_json(filename, **kwargs):
"""
# read
if isinstance(filename, (str, bytes, os.PathLike)):
with open(filename) as file:
data = file.read()
data = Path(filename).read_text()
else: # file-like : this also handles errors in dumping
data = filename.read()

Expand Down Expand Up @@ -70,9 +70,10 @@ def write_json(cosmology, file, *, overwrite=False):
data["meta"][k] = {"value": v.value.tolist(), "unit": str(v.unit)}

# check that file exists and whether to overwrite.
if os.path.exists(file) and not overwrite:
file = Path(file)
if file.exists() and not overwrite:
raise OSError(f"{file} exists. Set 'overwrite' to write over.")
with open(file, "w") as write_file:
with file.open("w") as write_file:
json.dump(data, write_file)


Expand Down Expand Up @@ -125,8 +126,8 @@ def test_readwrite_json_subclass_partial_info(
cosmo.write(fp, format="json")

# partial information
with open(fp) as file:
L = file.readlines()[0]
with fp.open() as file:
L = file.readline()
L = (
L[: L.index('"cosmology":')] + L[L.index(", ") + 2 :]
) # remove cosmology : #203
Expand All @@ -136,9 +137,7 @@ def test_readwrite_json_subclass_partial_info(
) # second occurrence : #203

tempfname = tmp_path / f"{cosmo.name}_temp.json"
with open(tempfname, "w") as file:
file.writelines([L])

tempfname.write_text("".join(L))
# read with the same class that wrote fills in the missing info with
# the default value
got = cosmo_cls.read(tempfname, format="json")
Expand Down
7 changes: 4 additions & 3 deletions astropy/cosmology/setup_package.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import sys
from os.path import dirname, join, relpath
from os.path import relpath
from pathlib import Path

from setuptools import Extension

ASTROPY_COSMOLOGY_ROOT = dirname(__file__)
ASTROPY_COSMOLOGY_ROOT = Path(__file__).parent


if sys.platform.startswith("win"):
Expand All @@ -22,7 +23,7 @@ def get_extensions():
return [
Extension(
"astropy.cosmology._signature_deprecations",
[relpath(join(ASTROPY_COSMOLOGY_ROOT, "_signature_deprecations.c"))],
[relpath(Path(ASTROPY_COSMOLOGY_ROOT, "_signature_deprecations.c"))],
extra_compile_args=extra_compile_args,
),
]
7 changes: 3 additions & 4 deletions astropy/cosmology/tests/test_connect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import inspect
import os

import pytest

Expand Down Expand Up @@ -82,15 +81,15 @@ def test_readwrite_complete_info(self, cosmo, tmp_path, format, metaio, has_deps
if (format, Cosmology) not in readwrite_registry._readers:
pytest.xfail(f"no read method is registered for format {format!r}")

fname = str(tmp_path / f"{cosmo.name}.{format}")
fname = tmp_path / f"{cosmo.name}.{format}"
cosmo.write(fname, format=format)

# Also test kwarg "overwrite"
assert os.path.exists(fname) # file exists
assert fname.is_file()
with pytest.raises(IOError):
cosmo.write(fname, format=format, overwrite=False)

assert os.path.exists(fname) # overwrite file existing file
assert fname.exists() # overwrite file existing file
cosmo.write(fname, format=format, overwrite=True)

# Read back
Expand Down

0 comments on commit 1b563c2

Please sign in to comment.