Skip to content

Commit

Permalink
Merge c1a6d91 into 69a1044
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Sep 20, 2022
2 parents 69a1044 + c1a6d91 commit 2715693
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 62 deletions.
20 changes: 17 additions & 3 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,35 @@ jobs:
- uses: actions/checkout@v2

# Set up the conda-forge environment with mamba
- name: Set up Python ${{ matrix.python-version }} and dependencies
- name: Set up Python ${{ matrix.python-version }} and base environment
uses: conda-incubator/setup-miniconda@v2
with:
auto-update-conda: true
python-version: ${{ matrix.python-version }}
mamba-version: "*"
channels: conda-forge
channel-priority: strict
environment-file: dev-environment.yml
activate-environment: xdem-dev
environment-file: environment.yml
activate-environment: xdem

- name: Install project
run: |
pip install --no-dependencies --editable .
# This steps allows us to check the "import xdem" with the base environment provided to users, before adding
# development-specific dependencies by differencing the env and dev-env yml files
- name: Check normal environment import
run: |
python -c "import xdem"
- name: Update environment with dev dependencies
run: |
mamba env update --name xdem --file dev-environment.yml
- name: Re-install project
run: |
pip install -e . --no-dependencies
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand Down
5 changes: 3 additions & 2 deletions dev-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- proj>=7.2
- fiona
- shapely
- autovizwidget
- numba
- numpy
- matplotlib
Expand All @@ -16,12 +15,12 @@ dependencies:
- scipy
- tqdm
- scikit-image
- richdem
- proj-data
- scikit-gstat>=0.6.8
- pytransform3d

# Development-specific
- autovizwidget
- pytest
- pytest-xdist
- sphinx
Expand All @@ -31,6 +30,8 @@ dependencies:
- flake8
- sphinx-autodoc-typehints
- sphinx-gallery
- pyyaml
- richdem

- pip:
- git+https://github.com/GlacioHack/geoutils.git
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ scikit-gstat
flake8
opencv-contrib-python
sphinx-gallery
pyyaml
4 changes: 3 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ dependencies:
- proj-data
- scikit-gstat>=0.6.8
- pytransform3d
- geoutils>=0.0.8

- pip:
- git+https://github.com/GlacioHack/geoutils.git
148 changes: 92 additions & 56 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the xdem.misc functions."""
from __future__ import annotations

import os
import warnings

import pytest
Expand All @@ -9,59 +10,94 @@
import xdem.misc


@pytest.mark.parametrize("deprecation_increment", [-1, 0, 1, None]) # type: ignore
@pytest.mark.parametrize("details", [None, "It was completely useless!", "dunnowhy"]) # type: ignore
def test_deprecate(deprecation_increment: int | None, details: str | None) -> None:
"""
Test the deprecation warnings/errors.
If the removal_version is larger than the current, it should warn.
If the removal_version is smaller or equal, it should raise an error.
:param deprecation_increment: The version number relative to the current version.
:param details: An optional explanation for the description.
"""
warnings.simplefilter("error")

current_version = xdem.version.version

# Set the removal version to be the current version plus the increment (e.g. 0.0.5 + 1 -> 0.0.6)
removal_version = (
current_version[:-1] + str(int(current_version.rsplit(".", 1)[1]) + deprecation_increment)
if deprecation_increment is not None
else None
)

# Define a function with no use that is marked as deprecated.
@xdem.misc.deprecate(removal_version, details=details) # type: ignore
def useless_func() -> int:
return 1

# If True, a warning is expected. If False, a ValueError is expected.
should_warn = removal_version is None or removal_version > current_version

# Add the expected text depending on the parametrization.
text = (
"Call to deprecated function 'useless_func'."
if should_warn
else f"Deprecated function 'useless_func' was removed in {removal_version}."
)

if details is not None:
text += " " + details.strip().capitalize()

if not any(text.endswith(c) for c in ".!?"):
text += "."

if should_warn and removal_version is not None:
text += f" This functionality will be removed in version {removal_version}."
elif not should_warn:
text += f" Current version: {current_version}."

# Expect either a warning or an exception with the right text.
if should_warn:
with pytest.warns(DeprecationWarning, match="^" + text + "$"):
useless_func()
else:
with pytest.raises(ValueError, match="^" + text + "$"):
useless_func()
import yaml # type: ignore


class TestMisc:
def test_environment_files(self) -> None:
"""Check that environment yml files are properly written: all dependencies of env are also in dev-env"""

fn_env = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "environment.yml"))
fn_devenv = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "dev-environment.yml"))

# Load the yml as dictionaries
yaml_env = yaml.safe_load(open(fn_env))
yaml_devenv = yaml.safe_load(open(fn_devenv))

# Extract the dependencies values
conda_dep_env = yaml_env["dependencies"]
conda_dep_devenv = yaml_devenv["dependencies"]

# Check if there is any pip dependency, if yes pop it from the end of the list
if isinstance(conda_dep_devenv[-1], dict):
pip_dep_devenv = conda_dep_devenv.pop()

# Check if there is a pip dependency in the normal env as well, if yes pop it also
if isinstance(conda_dep_env[-1], dict):
pip_dep_env = conda_dep_env.pop()

# The diff below computes the dependencies that are in env but not in dev-env
# It should be empty, otherwise we raise an error
diff_pip_check = list(set(pip_dep_env) - set(pip_dep_devenv))
assert len(diff_pip_check) == 0

# We do the same for the conda dependency, first a sanity check that everything that is in env is also in dev-ev
diff_conda_check = list(set(conda_dep_env) - set(conda_dep_devenv))
assert len(diff_conda_check) == 0

@pytest.mark.parametrize("deprecation_increment", [-1, 0, 1, None]) # type: ignore
@pytest.mark.parametrize("details", [None, "It was completely useless!", "dunnowhy"]) # type: ignore
def test_deprecate(self, deprecation_increment: int | None, details: str | None) -> None:
"""
Test the deprecation warnings/errors.
If the removal_version is larger than the current, it should warn.
If the removal_version is smaller or equal, it should raise an error.
:param deprecation_increment: The version number relative to the current version.
:param details: An optional explanation for the description.
"""
warnings.simplefilter("error")

current_version = xdem.version.version

# Set the removal version to be the current version plus the increment (e.g. 0.0.5 + 1 -> 0.0.6)
removal_version = (
current_version[:-1] + str(int(current_version.rsplit(".", 1)[1]) + deprecation_increment)
if deprecation_increment is not None
else None
)

# Define a function with no use that is marked as deprecated.
@xdem.misc.deprecate(removal_version, details=details) # type: ignore
def useless_func() -> int:
return 1

# If True, a warning is expected. If False, a ValueError is expected.
should_warn = removal_version is None or removal_version > current_version

# Add the expected text depending on the parametrization.
text = (
"Call to deprecated function 'useless_func'."
if should_warn
else f"Deprecated function 'useless_func' was removed in {removal_version}."
)

if details is not None:
text += " " + details.strip().capitalize()

if not any(text.endswith(c) for c in ".!?"):
text += "."

if should_warn and removal_version is not None:
text += f" This functionality will be removed in version {removal_version}."
elif not should_warn:
text += f" Current version: {current_version}."

# Expect either a warning or an exception with the right text.
if should_warn:
with pytest.warns(DeprecationWarning, match="^" + text + "$"):
useless_func()
else:
with pytest.raises(ValueError, match="^" + text + "$"):
useless_func()

0 comments on commit 2715693

Please sign in to comment.