Skip to content

Commit

Permalink
Merge pull request #78 from khaeru/compat/pyam-1.7
Browse files Browse the repository at this point in the history
Adjust .compat.pyam for 1.7
  • Loading branch information
khaeru authored Jan 31, 2023
2 parents 37f78e1 + aa016d1 commit d73282e
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
# If the "Latest version testable on GitHub Actions" in pytest.yaml
# is not the latest 3.x stable version, adjust here to match:
python-version: "3.x"
python-version: "3.10"

- name: Upgrade pip, wheel, setuptools-scm
run: |
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dist
coverage.xml
htmlcov
prof/
# Generated by Computer.visualize()
/*.png
# Generated by .tests.core.test_computer.test_cache()
myfunc2-*.pkl

Expand Down
6 changes: 4 additions & 2 deletions doc/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ What's new
:backlinks: none
:depth: 1

.. Next release
.. ============
Next release
============

- Adjust for compatibility with pyam 1.7 (released 2022-12-19) (:pull:`78`).

v1.14.1 (2022-10-28)
====================
Expand Down
2 changes: 1 addition & 1 deletion genno/compat/pyam/computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def write_report(obj, path: Union[str, Path]) -> None:
if path.suffix == ".csv":
obj.to_csv(path)
elif path.suffix == ".xlsx":
obj.to_excel(path, merge_cells=False)
obj.to_excel(path)
else:
raise ValueError(
f"pyam.IamDataFrame can be written to .csv or .xlsx, not {path.suffix}"
Expand Down
10 changes: 5 additions & 5 deletions genno/computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ def group_sum(qty, group, sum):
@maybe_densify
def interpolate(
qty: Quantity,
coords: Mapping[Hashable, Any] = None,
coords: Optional[Mapping[Hashable, Any]] = None,
method: InterpOptions = "linear",
assume_sorted: bool = True,
kwargs: Mapping[str, Any] = None,
kwargs: Optional[Mapping[str, Any]] = None,
**coords_kwargs: Any,
) -> Quantity:
"""Interpolate `qty`.
Expand Down Expand Up @@ -494,7 +494,7 @@ def _load_file_csv(
) -> Quantity:
# Peek at the header, if any, and match a units expression
with open(path, "r", encoding="utf-8") as f:
for line, match in map(lambda l: (l, UNITS_RE.fullmatch(l)), f):
for line, match in map(lambda li: (li, UNITS_RE.fullmatch(li)), f):
if match:
if units:
log.warning(f"Replace {match.group(1)!r} from file with {units!r}")
Expand Down Expand Up @@ -667,7 +667,7 @@ def mul(*quantities: Quantity) -> Quantity:

def relabel(
qty: Quantity,
labels: Mapping[Hashable, Mapping] = None,
labels: Optional[Mapping[Hashable, Mapping]] = None,
**dim_labels: Mapping,
) -> Quantity:
"""Replace specific labels along dimensions of `qty`.
Expand Down Expand Up @@ -749,7 +749,7 @@ def select(qty, indexers, inverse=False):
new_indexers = {}
for dim, labels in indexers.items():
new_indexers[dim] = list(
filter(lambda l: l not in labels, qty.coords[dim].data)
filter(lambda lab: lab not in labels, qty.coords[dim].data)
)
indexers = new_indexers

Expand Down
2 changes: 1 addition & 1 deletion genno/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
STORE = set(["cache_path", "cache_skip"])


def configure(path: Union[Path, str] = None, **config):
def configure(path: Optional[Union[Path, str]] = None, **config):
"""Configure :mod:`genno` globally.
Modifies global variables that affect the behaviour of *all* Computers and
Expand Down
12 changes: 6 additions & 6 deletions genno/core/attrseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def assign_coords(self, coords=None, **coord_kwargs):
# Return a new object with the new index
return self.set_axis(new_idx)

def bfill(self, dim: Hashable, limit: int = None):
def bfill(self, dim: Hashable, limit: Optional[int] = None):
"""Like :meth:`xarray.DataArray.bfill`."""
return self.__class__(
self.unstack(dim)
Expand Down Expand Up @@ -172,7 +172,7 @@ def expand_dims(self, dim=None, axis=None, **dim_kwargs: Any) -> "AttrSeries":

return result

def ffill(self, dim: Hashable, limit: int = None):
def ffill(self, dim: Hashable, limit: Optional[int] = None):
"""Like :meth:`xarray.DataArray.ffill`."""
return self.__class__(
self.unstack(dim)
Expand All @@ -192,10 +192,10 @@ def item(self, *args):

def interp(
self,
coords: Mapping[Hashable, Any] = None,
coords: Optional[Mapping[Hashable, Any]] = None,
method: str = "linear",
assume_sorted: bool = True,
kwargs: Mapping[str, Any] = None,
kwargs: Optional[Mapping[str, Any]] = None,
**coords_kwargs: Any,
):
"""Like :meth:`xarray.DataArray.interp`.
Expand Down Expand Up @@ -393,7 +393,7 @@ def sel(self, indexers=None, drop=False, **indexers_kwargs):

def shift(
self,
shifts: Mapping[Hashable, int] = None,
shifts: Optional[Mapping[Hashable, int]] = None,
fill_value: Any = None,
**shifts_kwargs: int,
):
Expand Down Expand Up @@ -475,7 +475,7 @@ def transpose(self, *dims):
return self.reorder_levels(dims)

def to_dataframe(
self, name: Hashable = None, dim_order: List[Hashable] = None
self, name: Hashable = None, dim_order: Optional[List[Hashable]] = None
) -> pd.DataFrame:
"""Like :meth:`xarray.DataArray.to_dataframe`."""
if dim_order is not None:
Expand Down
15 changes: 12 additions & 3 deletions genno/core/computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def __init__(self, **kwargs):
self.configure(**kwargs)

def configure(
self, path: Union[Path, str] = None, fail: Union[str, int] = "raise", **config
self,
path: Optional[Union[Path, str]] = None,
fail: Union[str, int] = "raise",
**config,
):
"""Configure the Computer.
Expand Down Expand Up @@ -627,7 +630,7 @@ def aggregate(
weights: Optional[xr.DataArray] = None,
keep: bool = True,
sums: bool = False,
fail: Union[str, int] = None,
fail: Optional[Union[str, int]] = None,
):
"""Add a computation that aggregates *qty*.
Expand Down Expand Up @@ -784,13 +787,19 @@ def describe(self, key=None, quiet=True):
print(result, end="\n")
return result

def __dask_keys__(self):
return self.graph.keys()

def __dask_graph__(self):
return self.graph

def visualize(self, filename, **kwargs):
"""Generate an image describing the Computer structure.
This is a shorthand for :meth:`dask.visualize`. Requires
`graphviz <https://pypi.org/project/graphviz/>`__.
"""
return dask.visualize(self.graph, filename=filename, **kwargs)
return dask.visualize(self, filename=filename, traverse=False, **kwargs)

def write(self, key, path):
"""Write the result of `key` to the file `path`."""
Expand Down
28 changes: 15 additions & 13 deletions genno/core/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def units(self, value):
self.attrs["_unit"] = pint.get_application_registry().Unit(value)

# Type hints for mypy in downstream applications
def __len__(self) -> int:
def __len__(self) -> int: # type: ignore [empty-body]
... # pragma: no cover

def __mul__(self, other) -> "Quantity":
def __mul__(self, other) -> "Quantity": # type: ignore [empty-body]
... # pragma: no cover

def __radd__(self, other):
Expand All @@ -95,19 +95,21 @@ def __rsub__(self, other):
def __rtruediv__(self, other):
... # pragma: no cover

def __truediv__(self, other) -> "Quantity":
def __truediv__(self, other) -> "Quantity": # type: ignore [empty-body]
... # pragma: no cover

@property
def attrs(self) -> Dict[Any, Any]:
def attrs(self) -> Dict[Any, Any]: # type: ignore [empty-body]
... # pragma: no cover

@property
def coords(self) -> xarray.core.coordinates.DataArrayCoordinates:
def coords( # type: ignore [empty-body]
self,
) -> xarray.core.coordinates.DataArrayCoordinates:
... # pragma: no cover

@property
def dims(self) -> Tuple[Hashable, ...]:
def dims(self) -> Tuple[Hashable, ...]: # type: ignore [empty-body]
... # pragma: no cover

def assign_coords(
Expand All @@ -134,10 +136,10 @@ def expand_dims(

def interp(
self,
coords: Mapping[Any, Any] = None,
coords: Optional[Mapping[Any, Any]] = None,
method: InterpOptions = "linear",
assume_sorted: bool = False,
kwargs: Mapping[str, Any] = None,
kwargs: Optional[Mapping[str, Any]] = None,
**coords_kwargs: Any,
):
... # pragma: no cover
Expand All @@ -152,10 +154,10 @@ def rename(
): # NB "Quantity" here offends mypy
... # pragma: no cover

def sel(
def sel( # type: ignore [empty-body]
self,
indexers: Mapping[Any, Any] = None,
method: str = None,
indexers: Optional[Mapping[Any, Any]] = None,
method: Optional[str] = None,
tolerance=None,
drop: bool = False,
**indexers_kwargs: Any,
Expand All @@ -164,7 +166,7 @@ def sel(

def shift(
self,
shifts: Mapping[Hashable, int] = None,
shifts: Optional[Mapping[Hashable, int]] = None,
fill_value: Any = None,
**shifts_kwargs: int,
): # NB "Quantity" here offends mypy
Expand All @@ -182,7 +184,7 @@ def sum(
): # NB "Quantity" here offends mypy
... # pragma: no cover

def to_numpy(self) -> np.ndarray:
def to_numpy(self) -> np.ndarray: # type: ignore [empty-body]
... # pragma: no cover

# Internal methods
Expand Down
14 changes: 8 additions & 6 deletions genno/core/sparsedataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def __init__(
coords: Union[Sequence[Tuple], Mapping[Hashable, Any], None] = None,
dims: Union[Hashable, Sequence[Hashable], None] = None,
name: Hashable = None,
attrs: Mapping = None,
attrs: Optional[Mapping] = None,
# internal parameters
indexes: Dict[Hashable, pd.Index] = None,
indexes: Optional[Dict[Hashable, pd.Index]] = None,
fastpath: bool = False,
**kwargs,
):
Expand Down Expand Up @@ -158,7 +158,7 @@ def from_series(cls, obj, sparse=True):
# Call the parent method always with sparse=True, then re-wrap
return xr.DataArray.from_series(obj, sparse=True)._sda.convert()

def ffill(self, dim: Hashable, limit: int = None):
def ffill(self, dim: Hashable, limit: Optional[int] = None):
"""Override :meth:`~xarray.DataArray.ffill` to auto-densify."""
return self._sda.dense_super.ffill(dim, limit)._sda.convert()

Expand All @@ -178,8 +178,8 @@ def _item(self, *args):

def sel(
self,
indexers: Mapping[Any, Any] = None,
method: str = None,
indexers: Optional[Mapping[Any, Any]] = None,
method: Optional[str] = None,
tolerance=None,
drop: bool = False,
**indexers_kwargs: Any,
Expand All @@ -205,7 +205,9 @@ def sel(
)

def to_dataframe(
self, name: Optional[Hashable] = None, dim_order: Sequence[Hashable] = None
self,
name: Optional[Hashable] = None,
dim_order: Optional[Sequence[Hashable]] = None,
) -> pd.DataFrame:
"""Convert this array and its coords into a :class:`~xarray.DataFrame`.
Expand Down

0 comments on commit d73282e

Please sign in to comment.