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

Feature/issue 233 group dependabot updates #235

Merged
merged 9 commits into from
Jun 17, 2024
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ updates:
directory: "/"
schedule:
interval: "monthly"
groups:
pip-dependencies:
patterns:
- "*"
# Raise pull requests for version updates
# to pip against the `develop` branch
target-branch: "develop"
Expand All @@ -17,6 +21,10 @@ updates:
schedule:
# Check for updates to GitHub Actions every week
interval: "monthly"
groups:
gha-dependencies:
patterns:
- "*"
# Raise pull requests for version updates
# to pip against the `develop` branch
target-branch: "develop"
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default_language_version:

repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
rev: v3.16.0
hooks:
- id: pyupgrade
args: [ "--py39-plus" ]
Expand All @@ -24,7 +24,7 @@ repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: 'v0.4.6'
rev: 'v0.4.9'
hooks:
- id: ruff
args: [ "--fix" ]
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [Issue #229](https://github.com/nasa/ncompare/issues/229): Added Journal of Open Source Software (JOSS) info to README and CITATION docs.
### Changed
- [Issue #233](https://github.com/nasa/ncompare/issues/233): Group dependabot updates into fewer PRs.
### Deprecated
### Removed
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion ncompare/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def _get_groups(nc_filepath: Union[str, Path]) -> list:
def _get_dims(nc_filepath: Union[str, Path]) -> list:
def __get_dim_list(decode_times=True):
with xr.open_dataset(nc_filepath, decode_times=decode_times) as dataset:
return list(dataset.dims.items())
return list(dataset.sizes.items())

try:
dims_list = __get_dim_list()
Expand Down
42 changes: 16 additions & 26 deletions ncompare/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,16 @@


def ensure_valid_path_exists(should_be_path: Union[str, Path]) -> Path:
"""Convert input to a pathlib.Path and check that the resulting filepath exists."""
fails_to_exist_msg = "Expected file does not exist: "
wrong_type_msg = "Unexpected type for something that should be convertable to a Path: "

if isinstance(should_be_path, str):
# Convert to a Path object
path_obj = Path(should_be_path)
if path_obj.exists():
return path_obj
raise FileNotFoundError(fails_to_exist_msg + str(should_be_path))

if isinstance(should_be_path, Path):
if should_be_path.exists():
return should_be_path
raise FileNotFoundError(fails_to_exist_msg + str(should_be_path))

raise TypeError(wrong_type_msg + str(type(should_be_path)))
"""Coerce input to a pathlib.Path and check that the resulting filepath exists."""
path_obj = _coerce_str_or_path_to_path(should_be_path)
if path_obj.exists():
return path_obj
raise FileNotFoundError("Expected file does not exist: " + str(should_be_path))


def ensure_valid_path_with_suffix(should_be_path: Union[str, Path], suffix: str) -> Path:
"""Coerce input to a pathlib.Path with given suffix."""
wrong_type_msg = "Unexpected type for something that should be convertable to a Path: "

if isinstance(should_be_path, str):
path_obj = Path(should_be_path)
elif isinstance(should_be_path, Path):
path_obj = should_be_path
else:
raise TypeError(wrong_type_msg + str(type(should_be_path)))

path_obj = _coerce_str_or_path_to_path(should_be_path)
return path_obj.with_suffix(suffix)


Expand All @@ -72,3 +52,13 @@ def coerce_to_str(some_object: Union[str, int, tuple]):
return str(some_object)

raise TypeError(f"Unable to coerce value to str. Unexpected type <{type(some_object)}>.")


def _coerce_str_or_path_to_path(should_be_path: Union[Path, str]) -> Path:
wrong_type_msg = "Unexpected type for something that should be convertable to a Path: "
if isinstance(should_be_path, str):
return Path(should_be_path)
elif isinstance(should_be_path, Path):
return should_be_path
else:
raise TypeError(wrong_type_msg + str(type(should_be_path)))
1,033 changes: 505 additions & 528 deletions poetry.lock

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ def ds_3dims_2vars_4coords(temp_data_dir) -> Path:
return filepath


@pytest.fixture(scope="session")
def ds_1dim_1var_1coord(temp_data_dir) -> Path:
ds = Dataset(
data_vars=dict(z1=(["y"], np.array([5, 6]))),
coords=dict(
y=range(2),
),
)
filepath = temp_data_dir / "test_1dim_1var_1coord.nc"
ds.to_netcdf(path=filepath)

return filepath


@pytest.fixture(scope="session")
def ds_1dim_1var_allnan_1coord(temp_data_dir) -> Path:
ds = Dataset(
data_vars=dict(z1=(["y"], np.array([np.nan, np.nan]))),
coords=dict(
y=range(2),
),
)
filepath = temp_data_dir / "test_1dim_1var_allnan_1coord.nc"
ds.to_netcdf(path=filepath)

return filepath


@pytest.fixture(scope="session")
def ds_4dims_3vars_5coords(temp_data_dir):
ds = Dataset(
Expand Down
9 changes: 0 additions & 9 deletions tests/test_complete_file_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,4 @@ def test_full_run_to_xlsx_output(temp_data_dir):
difference = df1[df1 != df2]
rows_with_differences = [(idx, row) for idx, row in difference.notnull().iterrows() if any(row)]

if len(rows_with_differences) != 0:
print(
f"Test will fail because of differences in <{len(rows_with_differences)}> rows. "
f"Differences identified (Row index ----> row contents):"
)
for idx, row in rows_with_differences:
diff = difference.loc[idx]
print(f" {idx} ----> {diff}")

assert len(rows_with_differences) == 0
40 changes: 32 additions & 8 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ def test_matching_random_values(
ds_3dims_2vars_4coords,
ds_4dims_3vars_5coords,
ds_3dims_3vars_4coords_1group,
ds_1dim_1var_1coord,
ds_1dim_1var_allnan_1coord,
outputter_to_console,
):
variable_array_1 = xr.open_dataset(ds_3dims_2vars_4coords).variables['z1']
variable_array_2 = xr.open_dataset(ds_4dims_3vars_5coords).variables['z1']
variable_array_3 = xr.open_dataset(ds_1dim_1var_1coord).variables['z1']
variable_array_allnan = xr.open_dataset(ds_1dim_1var_allnan_1coord).variables['z1']

assert (
_match_random_value(
Expand All @@ -93,6 +97,32 @@ def test_matching_random_values(
)
is False
)
assert (
_match_random_value(
outputter_to_console,
variable_array_3,
variable_array_3,
)
is True
)
# NaN to non-NaN is NOT considered a match
assert (
_match_random_value(
outputter_to_console,
variable_array_3,
variable_array_allnan,
)
is None
)
# NaN to NaN is considered a match
assert (
_match_random_value(
outputter_to_console,
variable_array_allnan,
variable_array_allnan,
)
is True
)


def test_print_values_runs_with_no_error(ds_3dims_3vars_4coords_1group, outputter_to_console):
Expand Down Expand Up @@ -137,10 +167,7 @@ def test_comparison_group_no_error_for_duplicate_dataset(
if "Variables within specified group <Group1>:" in line:
found_expected = True

if found_expected:
assert True
else:
assert False
assert found_expected


def test_comparison_var_no_error_for_duplicate_dataset(
Expand All @@ -160,10 +187,7 @@ def test_comparison_var_no_error_for_duplicate_dataset(
if "Sample values within specified variable <var1>:" in line:
found_expected = True

if found_expected:
assert True
else:
assert False
assert found_expected


def test_get_vars_with_group(ds_3dims_3vars_4coords_1group):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,19 @@ def test_make_valid_path_from_Path_in_repo():
assert isinstance(returnval, Path)


def test_error_from_wrong_path_type():
with pytest.raises(TypeError):
ensure_valid_path_exists((0, 1))


def test_coerce_int_to_str():
assert coerce_to_str(5) == "5"


def test_coerce_tuple_to_str():
assert coerce_to_str(('step', 123)) == "('step', 123)"


def test_error_from_not_able_to_coerce_to_str():
with pytest.raises(TypeError):
coerce_to_str(list[5, 6, 7])