Skip to content

Commit

Permalink
Update and apply Pre-Commit hooks (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger authored Oct 24, 2024
1 parent 471abb3 commit 46e27a8
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 49 deletions.
57 changes: 33 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# GitHub: https://github.com/btschwertfeger
#

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.9
rev: v0.7.0
hooks:
- id: ruff
args: [--fix]
args:
- --fix
- --preview
- --exit-non-zero-on-fix
- id: ruff-format
- repo: https://github.com/pycqa/flake8
rev: 7.1.0
hooks:
- id: flake8
args: ["--select=E9,F63,F7,F82", "--show-source", "--statistics"]
# - repo: https://github.com/pre-commit/mirrors-mypy # FIXME
# rev: v1.8.0
# rev: v1.13.0
# hooks:
# - id: mypy
# name: mypy
Expand All @@ -23,14 +26,14 @@ repos:
rev: v2.3.0
hooks:
- id: codespell
additional_dependencies:
- tomli
additional_dependencies: [tomli]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
# all available hooks can be found here: https://github.com/pre-commit/pre-commit-hooks/blob/main/.pre-commit-hooks.yaml
- id: check-yaml
- id: check-ast
- id: check-json
- id: check-toml
- id: check-docstring-first
- id: check-case-conflict
Expand All @@ -53,26 +56,32 @@ repos:
rev: v1.10.0
hooks:
- id: python-use-type-annotations
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-check-mock-methods
- id: rst-backticks
- id: python-no-eval
- id: python-no-log-warn
- id: rst-backticks
# - id: rst-inline-touching-normal
- id: rst-inline-touching-normal
- id: rst-directive-colons
- id: text-unicode-replacement-char
# - repo: https://github.com/psf/black
# rev: 24.2.0
# hooks:
# - id: black
# - repo: https://github.com/adamchainz/blacken-docs
# rev: 1.16.0
# hooks:
# - id: blacken-docs
# additional_dependencies: [black==23.12.0]
- repo: https://github.com/PyCQA/isort # TODO: remove as soon as ruff is stable
rev: 5.13.2
hooks:
- id: isort
args: ["--profile=black"] # solves conflicts between black and isort
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.2
args: [--profile=black]
- repo: https://github.com/PyCQA/bandit
rev: 1.7.10
hooks:
- id: bandit
exclude: "^tests/.*|examples/.*"
- repo: https://github.com/yunojuno/pre-commit-xenon
rev: v0.1
hooks:
- id: prettier
- id: xenon
args:
- --max-average=B
- --max-modules=B
- --max-absolute=C
exclude: '\.nc$|^tests/fixture/|\.ipynb$'
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,13 @@ Notes:

- First check if there is an existing issue or PR that addresses your
problem/solution. If not - create one first - before creating a PR.
- Typo fixes, project configuration, CI, documentation or style/formatting PRs will be
rejected. Please create an issue for that.
- Typo fixes, project configuration, CI, documentation or style/formatting PRs
will be rejected. Please create an issue for that.
- PRs must provide a reasonable, easy to understand and maintain solution for an
existing problem. You may want to propose a solution when creating the issue
to discuss the approach before creating a PR.
- There is currently no need for the implementation of further bias correction
methods.

<a name="references"></a>

Expand Down
2 changes: 1 addition & 1 deletion cmethods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def print_version(
"""Prints the version of the package"""
if not value or ctx.resilient_parsing:
return
from importlib.metadata import version
from importlib.metadata import version # noqa: PLC0415

echo(version("python-cmethods"))
ctx.exit()
Expand Down
2 changes: 1 addition & 1 deletion cmethods/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def adjust(
# to unrealistic monthly transitions. If such behavior is wanted,
# mock this function or apply ``CMethods.__apply_ufunc` directly
# on your data sets.
if kwargs.get("group", None) is None:
if kwargs.get("group") is None:
return apply_ufunc(method, obs, simh, simp, **kwargs).to_dataset()

if method not in SCALING_METHODS:
Expand Down
8 changes: 4 additions & 4 deletions cmethods/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def quantile_mapping(
"""
check_adjust_called(
function_name="quantile_mapping",
adjust_called=kwargs.get("adjust_called", None),
adjust_called=kwargs.get("adjust_called"),
)
check_np_types(obs=obs, simh=simh, simp=simp)

Expand Down Expand Up @@ -73,7 +73,7 @@ def quantile_mapping(
xbins,
cdf_simh,
left=kwargs.get("val_min", 0.0),
right=kwargs.get("val_max", None),
right=kwargs.get("val_max"),
)
return get_inverse_of_cdf(cdf_obs, epsilon, xbins) # Eq. 2

Expand Down Expand Up @@ -157,7 +157,7 @@ def detrended_quantile_mapping(
xbins,
cdf_simh,
left=kwargs.get("val_min", 0.0),
right=kwargs.get("val_max", None),
right=kwargs.get("val_max"),
)
X = np.interp(epsilon, cdf_obs, xbins) * ensure_dividable(
m_simp_mean,
Expand Down Expand Up @@ -205,7 +205,7 @@ def quantile_delta_mapping(
"""
check_adjust_called(
function_name="quantile_delta_mapping",
adjust_called=kwargs.get("adjust_called", None),
adjust_called=kwargs.get("adjust_called"),
)
check_np_types(obs=obs, simh=simh, simp=simp)

Expand Down
6 changes: 3 additions & 3 deletions cmethods/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def linear_scaling(
"""
check_adjust_called(
function_name="linear_scaling",
adjust_called=kwargs.get("adjust_called", None),
adjust_called=kwargs.get("adjust_called"),
)
check_np_types(obs=obs, simh=simh, simp=simp)

Expand Down Expand Up @@ -85,7 +85,7 @@ def variance_scaling(
"""
check_adjust_called(
function_name="variance_scaling",
adjust_called=kwargs.get("adjust_called", None),
adjust_called=kwargs.get("adjust_called"),
)
check_np_types(obs=obs, simh=simp, simp=simp)

Expand Down Expand Up @@ -130,7 +130,7 @@ def delta_method(
"""
check_adjust_called(
function_name="delta_method",
adjust_called=kwargs.get("adjust_called", None),
adjust_called=kwargs.get("adjust_called"),
)
check_np_types(obs=obs, simh=simh, simp=simp)

Expand Down
2 changes: 1 addition & 1 deletion cmethods/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self: UnknownMethodError, method: str, available_methods: list):

def check_adjust_called(
function_name: str,
adjust_called: Optional[bool] = None,
adjust_called: Optional[bool] = None, # noqa: FBT001
) -> None:
"""
Displays a user warning in case a correction function was not called via
Expand Down
5 changes: 2 additions & 3 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"""This module is the configuration for the Sphinx documentation building process"""

import sys
from os.path import join
from pathlib import Path
from shutil import copyfile

project = "python-cmethods"
copyright = "2023, Benjamin Thomas Schwertfeger" # pylint: disable=redefined-builtin
Expand All @@ -29,9 +31,6 @@


def setup(app) -> None: # noqa: ARG001
from os.path import join
from shutil import copyfile

copyfile(join("..", "examples", "examples.ipynb"), "examples.ipynb")


Expand Down
12 changes: 6 additions & 6 deletions doc/methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where:
>>> variable = "tas" # temperatures
>>> result = adjust(
... method="linear_scaling",
... obs=obs[variable],
... obs=obsh[variable],
... simh=simh[variable],
... simp=simp[variable],
... kind="+",
Expand Down Expand Up @@ -173,7 +173,7 @@ enables the adjustment of the standard deviation in the following step.
>>> variable = "tas" # temperatures
>>> result = adjust(
... method="variance_scaling",
... obs=obs[variable],
... obs=obsh[variable],
... simh=simh[variable],
... simp=simp[variable],
... kind="+",
Expand Down Expand Up @@ -257,7 +257,7 @@ where:
>>> variable = "tas" # temperatures
>>> result = adjust(
... method="delta_method",
... obs=obs[variable],
... obs=obsh[variable],
... simh=simh[variable],
... simp=simp[variable],
... kind="+",
Expand Down Expand Up @@ -340,7 +340,7 @@ In the following the equations of Alex J. Cannon (2015) are shown and explained:
>>> variable = "tas" # temperatures
>>> qm_adjusted = adjust(
... method="quantile_mapping",
... obs=obs[variable],
... obs=obsh[variable],
... simh=simh[variable],
... simp=simp[variable],
... n_quantiles=250,
Expand Down Expand Up @@ -417,7 +417,7 @@ where:
>>> simp = xr.open_dataset("path/to/the_dataset_to_adjust-scenario_period.nc")
>>> variable = "tas" # temperatures
>>> qm_adjusted = detrended_quantile_mapping(
... obs=obs[variable],
... obs=obsh[variable],
... simh=simh[variable],
... simp=simp[variable],
... n_quantiles=250
Expand Down Expand Up @@ -520,7 +520,7 @@ following the additive and multiplicative variant are shown.
>>> variable = "tas" # temperatures
>>> qdm_adjusted = adjust(
... method="quantile_delta_mapping",
... obs=obs[variable],
... obs=obsh[variable],
... simh=simh[variable],
... simp=simp[variable],
... n_quantiles=250,
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ cache-dir = ".cache/ruff"
lint.task-tags = ["todo", "TODO"]

[tool.ruff.lint.per-file-ignores]
"cmethods/types.py" = [
"A005", # Shadowing builtin
]
"doc/*.py" = [
"CPY001", # Missing copyright notice at top of file
# "CPY001", # Missing copyright notice at top of file
"PTH118", # `os.path.join()` should be replaced by `Path` with `/` operator,
"PTH123", # `open()` should be replaced by `Path.open()`
"PTH100", # `os.path.abspath()` should be replaced by `Path.resolve()`
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
FIXTURE_DIR: str = os.path.join(os.path.dirname(__file__), "fixture")


@pytest.fixture()
@pytest.fixture
def cli_runner() -> CliRunner:
"""Provide a cli-runner for testing the CLI"""
return CliRunner()
Expand All @@ -43,7 +43,7 @@ def dask_cluster() -> Any:
client.close()


@pytest.fixture()
@pytest.fixture
def datasets() -> dict:
obsh_add, obsp_add, simh_add, simp_add = get_datasets(kind="+")
obsh_mult, obsp_mult, simh_mult, simp_mult = get_datasets(kind="*")
Expand All @@ -65,7 +65,7 @@ def datasets() -> dict:


@lru_cache(maxsize=None)
@pytest.fixture()
@pytest.fixture
def datasets_from_zarr() -> dict:
return {
"+": {
Expand Down

0 comments on commit 46e27a8

Please sign in to comment.