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

Fix importlib.resources deprecations for py 3.9 #1485

Merged
merged 9 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 8 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install tox
run: pip install tox~=4.0
run: |
pip install tox~=4.0
- name: Test with tox
run: tox -e ${{ matrix.tox-env }}
run: |
tox -e ${{ matrix.tox-env }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_FLAG_NAME: run-{{ matrix.tox-env }}
Expand Down Expand Up @@ -117,9 +119,11 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install tox
run: pip install tox~=4.0
run: |
pip install tox~=4.0
- name: Test with tox
run: tox -e ${{ matrix.tox-env }} -- ${{ matrix.markers }}
run: |
tox -e ${{ matrix.tox-env }} -- ${{ matrix.markers }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_FLAG_NAME: run-{{ matrix.tox-env }}
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Internal changes
* GitHub testing workflows now use `Concurrency` instead of the styfle/cancel-workflow-action to cancel redundant workflows. (:pull:`1487`).
* The `pkg_resources` library has been replaced for the `packaging` library when version comparisons have been performed, and a few warning messages have been silenced in the testing suite. (:issue:`1489`, :pull:`1490`).
* New ``xclim.testing.helpers.assert_lazy`` context manager to assert the laziness of code blocks. (:pull:`1484`).
* Added a fix for the deprecation warnings that `importlib.resources` throws, made backwards-compatible for Python3.8 with `importlib_resources` backport. (:pull:`1485`).

v0.45.0 (2023-09-05)
--------------------
Expand Down
2 changes: 2 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
- cftime>=1.4.1
- Click >=8.1
- dask>=2.6.0
- importlib-resources # For Python3.8
- jsonpickle
- lmoments3
- numba
Expand Down Expand Up @@ -42,6 +43,7 @@ dependencies:
- ipython
- matplotlib
- mypy
- nbqa
- nbsphinx
- nbval
- nc-time-axis
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies = [
"cftime>=1.4.1",
"Click>=8.1",
"dask[array]>=2.6",
"importlib-resources; python_version == '3.8'",
"jsonpickle",
"lmoments3>=1.0.5",
"numba",
Expand Down
23 changes: 11 additions & 12 deletions xclim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Climate indices computation package based on Xarray."""
from __future__ import annotations

from importlib.resources import contents, path
try:
from importlib.resources import files as _files
except ImportError:
from importlib_resources import files as _files

from xclim.core import units # noqa
from xclim.core.indicator import build_indicator_module_from_yaml
Expand All @@ -14,19 +17,15 @@
__version__ = "0.45.13-beta"


_module_data = _files("xclim.data")

# Load official locales
for filename in contents("xclim.data"):
for filename in _module_data.glob("??.json"):
# Only select <locale>.json and not <module>.<locale>.json
if filename.endswith(".json") and filename.count(".") == 1:
locale = filename.split(".")[0]
with path("xclim.data", filename) as f:
load_locale(f, locale)
load_locale(filename, filename.stem)


# Virtual modules creation:
with path("xclim.data", "icclim.yml") as f:
build_indicator_module_from_yaml(f.with_suffix(""), mode="raise")
with path("xclim.data", "anuclim.yml") as f:
build_indicator_module_from_yaml(f.with_suffix(""), mode="raise")
with path("xclim.data", "cf.yml") as f:
build_indicator_module_from_yaml(f.with_suffix(""), mode="raise")
build_indicator_module_from_yaml(_module_data / "icclim", mode="raise")
build_indicator_module_from_yaml(_module_data / "anuclim", mode="raise")
build_indicator_module_from_yaml(_module_data / "cf", mode="raise")
12 changes: 9 additions & 3 deletions xclim/core/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
import logging
import re
import warnings
from importlib.resources import open_text

try:
from importlib.resources import files
except ImportError:
from importlib_resources import files

from inspect import _empty, signature # noqa
from typing import Any, Callable, Tuple
from typing import Any, Callable

import numpy as np
import pint
Expand Down Expand Up @@ -112,7 +117,8 @@
units.add_context(hydro)


CF_CONVERSIONS = safe_load(open_text("xclim.data", "variables.yml"))["conversions"]
with (files("xclim.data") / "variables.yml").open() as f:
CF_CONVERSIONS = safe_load(f)["conversions"]
_CONVERSIONS = {}


Expand Down
14 changes: 10 additions & 4 deletions xclim/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
from collections import defaultdict
from enum import IntEnum
from functools import partial
from importlib.resources import open_text

try:
from importlib.resources import files
except ImportError:
from importlib_resources import files

from inspect import Parameter, _empty # noqa
from io import StringIO
from pathlib import Path
from typing import Callable, Mapping, NewType, Sequence, TypeVar
from typing import Callable, NewType, Sequence, TypeVar

import numpy as np
import xarray as xr
Expand All @@ -37,8 +42,9 @@
#: Type annotation for thresholds and other not-exactly-a-variable quantities
Quantified = TypeVar("Quantified", xr.DataArray, str, Quantity)

VARIABLES = safe_load(open_text("xclim.data", "variables.yml"))["variables"]
"""Official variables definitions.
with (files("xclim.data") / "variables.yml").open() as f:
VARIABLES = safe_load(f)["variables"]
"""Official variables definitions.

A mapping from variable name to a dict with the following keys:

Expand Down
9 changes: 4 additions & 5 deletions xclim/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import os
import warnings
from importlib.resources import open_text
from pathlib import Path

import numpy as np
Expand All @@ -13,6 +12,7 @@
from yaml import safe_load

from xclim.core import calendar
from xclim.core.utils import VARIABLES
from xclim.indices import (
longwave_upwelling_radiation_from_net_downwelling,
shortwave_upwelling_radiation_from_net_downwelling,
Expand Down Expand Up @@ -234,13 +234,12 @@ def test_timeseries(
else:
coords = pd.date_range(start, periods=len(values), freq=freq)

data_on_var = safe_load(open_text("xclim.data", "variables.yml"))["variables"]
if variable in data_on_var:
if variable in VARIABLES:
attrs = {
a: data_on_var[variable].get(a, "")
a: VARIABLES[variable].get(a, "")
for a in ["description", "standard_name", "cell_methods"]
}
attrs["units"] = data_on_var[variable]["canonical_units"]
attrs["units"] = VARIABLES[variable]["canonical_units"]

else:
warnings.warn(f"Variable {variable} not recognised. Attrs will not be filled.")
Expand Down
4 changes: 2 additions & 2 deletions xclim/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
from __future__ import annotations

import hashlib
import importlib
import json
import logging
import os
import platform
import re
import sys
import warnings
from importlib import import_module
from io import StringIO
from pathlib import Path
from shutil import copy
Expand Down Expand Up @@ -563,7 +563,7 @@ def show_versions(
if modname in sys.modules:
mod = sys.modules[modname]
else:
mod = importlib.import_module(modname)
mod = import_module(modname)
except (KeyError, ModuleNotFoundError):
deps_blob.append((modname, None))
else:
Expand Down