From ed957c63d04ecb612b4524794e2204c88eaaf6ab Mon Sep 17 00:00:00 2001 From: Remington Mallett Date: Sun, 8 Dec 2024 07:55:17 -0500 Subject: [PATCH] replaces black/flake8 formatting/linting with ruff and ensures numpy 2.0 compatibility (#446) * replace black/flake8 config with ruff config in .toml * run ruff linter * run ruff formatter * remove single unused import manually * replace black github action with ruff github action * enforce numpy 2.0 compatibility with ruff * replace black with ruff in docs * remove explicit gh output format (on by default) --- .github/workflows/black.yml | 10 ---------- .github/workflows/ruff.yml | 13 +++++++++++++ docs/contributing.rst | 4 ++-- pyproject.toml | 32 +++++++++++++------------------ src/pingouin/bayesian.py | 2 +- src/pingouin/contingency.py | 3 +-- src/pingouin/datasets/__init__.py | 1 - 7 files changed, 30 insertions(+), 35 deletions(-) delete mode 100644 .github/workflows/black.yml create mode 100644 .github/workflows/ruff.yml diff --git a/.github/workflows/black.yml b/.github/workflows/black.yml deleted file mode 100644 index 98b2a668..00000000 --- a/.github/workflows/black.yml +++ /dev/null @@ -1,10 +0,0 @@ -name: Lint - -on: [push, pull_request] - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: psf/black@stable \ No newline at end of file diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml new file mode 100644 index 00000000..6251dfa6 --- /dev/null +++ b/.github/workflows/ruff.yml @@ -0,0 +1,13 @@ +name: Ruff +on: [push, pull_request] +jobs: + ruff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: "Linting" + uses: astral-sh/ruff-action@v1 + - name: "Formatting" + uses: astral-sh/ruff-action@v1 + with: + args: "format --check" diff --git a/docs/contributing.rst b/docs/contributing.rst index 656c509e..83081e4c 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -12,11 +12,11 @@ Code guidelines *Before starting new code*, we highly recommend opening an issue on `GitHub `_ to discuss potential changes. -* Please use standard `pep8 `_ and `flake8 `_ Python style guidelines. Pingouin uses `black `_ for code formatting. Before submitting a PR, please make sure to run the following command in the root folder of Pingouin: +* Please use standard `pep8 `_ and `flake8 `_ Python style guidelines. Pingouin uses `ruff `_ for code formatting. Before submitting a PR, please make sure to run the following command in the root folder of Pingouin: .. code-block:: bash - $ black . --line-length=100 + $ ruff format --line-length=100 * Use `NumPy style `_ for docstrings. Follow existing examples for simplest guidance. diff --git a/pyproject.toml b/pyproject.toml index 390bbdaf..a47f0cdc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -107,25 +107,19 @@ source = ["src"] show_missing = true # sort = "Cover" -[tool.black] +[tool.ruff] line-length = 100 -target-version = ['py311'] -include = '\.pyi?$' - -[tool.flake8] -# W605 : bug when math equation in numpydoc -# W503, W504 : line-break with math operator -# E203: E203 whitespace before ':', not compatible with Black -# DXXX: Docstring related -max-line-length = 100 -ignore = ["N806", "N803", "W503", "W504", "W605", "D100", "D200", "D205", "D301", "D400", "D401", "E203"] +target-version = "py311" exclude = [ - ".git", - "__pycache__", - "docs", - "build", - "__init__.py", - "examples", - "setup.py", + "__init__.py", # Skip init files bc they use star imports (breaking rules F403, F405) + "notebooks", # Skip jupyter notebook examples +] + +[tool.ruff.lint] +select = [ + "E4", # Subset of pycodestyle rules + "E7", # Subset of pycodestyle rules + "E9", # Subset of pycodestyle rules + "F", # All Pyflakes rules + "NPY201", ] -statistics = true diff --git a/src/pingouin/bayesian.py b/src/pingouin/bayesian.py index fdcefa4b..18f458ef 100644 --- a/src/pingouin/bayesian.py +++ b/src/pingouin/bayesian.py @@ -10,7 +10,7 @@ def _format_bf(bf, precision=3, trim="0"): """Format BF10 to floating point or scientific notation.""" - if type(bf) == str: + if isinstance(bf, str): return bf if bf >= 1e4 or bf <= 1e-4: out = np.format_float_scientific(bf, precision=precision, trim=trim) diff --git a/src/pingouin/contingency.py b/src/pingouin/contingency.py index d9011f3e..dca04bde 100644 --- a/src/pingouin/contingency.py +++ b/src/pingouin/contingency.py @@ -367,8 +367,7 @@ def convert_elem(elem): elif lower in ("y", "yes", "present", "true", "t", "positive", "p"): return 1 raise ValueError( - "Invalid value to build a 2x2 contingency " - "table on column {}: {}".format(column, elem) + "Invalid value to build a 2x2 contingency table on column {}: {}".format(column, elem) ) return series.apply(convert_elem) diff --git a/src/pingouin/datasets/__init__.py b/src/pingouin/datasets/__init__.py index dba2f416..291e39f4 100644 --- a/src/pingouin/datasets/__init__.py +++ b/src/pingouin/datasets/__init__.py @@ -1,6 +1,5 @@ import pandas as pd import os.path as op -from pingouin.utils import print_table ddir = op.dirname(op.realpath(__file__)) dts = pd.read_csv(op.join(ddir, "datasets.csv"), sep=",")