Skip to content

Commit

Permalink
Use Ruff as linter and formatter and introduce git pre-commit hook (#759
Browse files Browse the repository at this point in the history
)
  • Loading branch information
raoulcollenteur committed Jun 4, 2024
2 parents ff1199b + b0defd7 commit 3b3133e
Show file tree
Hide file tree
Showing 29 changed files with 200 additions and 135 deletions.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Add a short description describing the pull request (PR) here.
# Checklist before PR can be merged:
- [ ] closes issue #xxxx
- [ ] is documented
- [ ] Format code with [Black formatting](https://black.readthedocs.io)
- [ ] Format code with [ruff formatting](https://docs.astral.sh/ruff/)
- [ ] type hints for functions and methods
- [ ] tests added / passed
- [ ] Example Notebook (for new features)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_benchmark_notebooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
matrix:
include:
- name: Test suite for benchmark Notebooks
python: "3.9"
python: "3.10"
toxenv: notebooks
env:
# Color Output
Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/test_format_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI RUFF

on:
push:
pull_request:
branches:
- master
- dev

jobs:
test:
name: ${{ matrix.name }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- name: Formatting and linting with ruff
python: "3.10"
toxenv: ruff
env:
# Color Output
# Rich (pip)
FORCE_COLOR: 1
# Tox
PY_COLORS: 1
# Pytest
PYTEST_ADDOPTS: "--color=yes"
steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
check-latest: true
cache: "pip"
cache-dependency-path: pyproject.toml

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Setup tox environment
run: tox -e ${{ matrix.toxenv }} --notest

- name: Test
run: tox -e ${{ matrix.toxenv }} --skip-pkg-install
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: CI PYTEST

on:
push:
Expand Down Expand Up @@ -35,14 +35,6 @@ jobs:
python: "3.12"
toxenv: py312
experimental: false
- name: Formatting with black + isort
python: "3.9"
toxenv: format
experimental: false
- name: Linting with flake8
python: "3.9"
toxenv: lint
experimental: false
- name: Test suite for all unit tests including Notebooks
python: "3.9"
toxenv: all
Expand Down
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.4
hooks:
# Run the linter.
- id: ruff
types_or: [python, pyi, jupyter]
args: ["--extend-select", "I", "--fix"]
# Run the formatter.
- id: ruff-format
types_or: [python, pyi, jupyter]
8 changes: 3 additions & 5 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
from datetime import date

import requests

year = date.today().strftime("%Y")

from matplotlib import use

use("agg")

from pastas import __version__

year = date.today().strftime("%Y")
use("agg")

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
Expand Down
21 changes: 10 additions & 11 deletions doc/developers/code_style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ Pastas Code Style
=================
This page provides information on the code style to use when writing code for Pastas.

Black formatting
Formatting and Linting
----------------
To ensure high quality code that is easy to read and maintain we follow the
`Black <https://black.readthedocs.io/en/stable/index.html>`_ code formatting standard.
Please checkout the `Black Documentation <https://black.readthedocs.io/en/stable/index.html>`_
on how to format Python code this way.
`Ruff <https://docs.astral.sh/ruff/>`_ code standard. Please checkout the `Ruff
Documentation <https://docs.astral.sh/ruff/>`_ on how to format Python code
this way.

Type Hints
----------
Pastas uses TypeHinting, which is used to check user-provided input options. Please provide
TypeHints when creating new methods and classes.
Pastas uses Type Hinting, which is used to check user-provided input options.
Please provide Type Hints when creating new methods and classes.

Docstrings
----------
Documentation is
created using `Sphinxdoc <http://www.sphinx-doc.org>`_. Docstrings within
the method or class need to be written in `NumPy docformat <https://numpydoc
.readthedocs.io/en/latest/format.html#docstring-standard>`_ to enable
automatic documentation on this website.
Documentation is created using `Sphinxdoc <http://www.sphinx-doc.org>`_.
Docstrings within the method or class need to be written in `NumPy docformat
<https://numpydoc .readthedocs.io/en/latest/format.html#docstring-standard>`_
to enable automatic documentation on this website.

Optimization
------------
Expand Down
96 changes: 55 additions & 41 deletions doc/developers/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ to commit code changes to GitHub open source projects. Let's start!

0. Pick an Issue
----------------
Before you start, it is a good idea to check if there are any issues that you can help
with. You can find a list of issues that are open on the `GitHub Issues page
<https://github.com/pastas/pastas/labels/good-first-issue>`_` with the tag
"good-first-issue". These issues are a good place to start if you are new to Pastas
and want to contribute to the project.
Before you start, it is a good idea to check if there are any issues that you
can help with. You can find a list of issues that are open on the `GitHub
Issues page <https://github.com/pastas/pastas/labels/good-first-issue>`_` with
the tag "good-first-issue". These issues are a good place to start if you are
new to Pastas and want to contribute to the project.

1. Create a GitHub Issue
------------------------
Expand All @@ -27,63 +27,77 @@ propose to make and why these are necessary. This is an easy way to inform
the Pastas community in an early stage of any issues that needs to be solved
and allows others to help you work out a solution.

2. Fork Pastas
--------------
To start making changes to the original code, you need to make a local copy
of the Pastas, called "Forking" in git-language. You can read how to fork a
GitHub repository `here <https://help.github
.com/en/github/getting-started-with-github/fork-a-repo>`_.
2. Fork and install Pastas
--------------------------
To start making changes to the original code, you need to make a local copy of
the Pastas, called "Forking" in git-language. You can read how to fork a GitHub
repository `here
<https://help.github.com/en/github/getting-started-with-github/fork-a-repo>`_.
To use all the development tools; install Pastas in development mode by running
`pip install -e .[dev]` in the root of the repository. This will install all
development dependencies such as `tox`, `ruff`, `pre=commit` and `pytest`.

.. note::
Make sure to make changes in the make changes in your local Development
branch (Dev) or start an entirely new branch that branches of the
Dev-branch.
Make sure to make changes in the make changes in a new branch that branches
of the Dev-branch. This way you can easily create a Pull Request later on.

3. Write Code
-------------
After you forked Pastas, you can start making changes to the code or add new
features to it. To ensure high quality code that is easy to read and maintain
we follow the `Black <https://black.readthedocs.io/en/stable/index.html>`_ code
formatting standard. Check out the Pastas Code Style section to learn
we follow the `Ruff <https://docs.astral.sh/ruff/>`_ code
formatting and linting standard. Check out the Pastas Code Style section to learn
more about the formatting of code and docstrings.

.. note::
To make sure your code is up to standards, you can run the following:
- `ruff check --extend-select I --fix`
- `ruff format`
or use tox:
- `tox -e ruff_fix`

.. note::
If you want to make sure your code is formatted and linted on every commit:
consider using the git pre-commit hook by installing it with `pip install
pre-commit` and running `pre-commit install` in the root of the repository.

4. Test Code
------------
The ensure a proper functioning of the Pastas, it is important to supply
tests in the test-suite (`see here <https://github
-----------
The ensure a proper functioning of the Pastas, it is important to supply tests
in the test-suite (`see here <https://github
.com/pastas/pastas/tree/master/tests>`_). The ensure a proper functioning of
the Pastas, the software is automatically tested using Github Actions when changes
are made. Pastas uses `pytest <https://docs.pytest.org/en/stable/>`_ to run tests.
the Pastas, the software is automatically tested using Github Actions when
changes are made. Pastas uses `pytest <https://docs.pytest.org/en/stable/>`_ to
run tests.

5. Document Code
----------------
When submitting a new function, method or class, docstrings are required
before the new code will be pulled into the dev branch. Documentation is
created using `Sphinxdoc <http://www.sphinx-doc.org>`_. Docstrings within
the method or class need to be written in `NumPy docformat <https://numpydoc
.readthedocs.io/en/latest/format.html#docstring-standard>`_ to enable
automatic documentation on this website. In the case of a new module, the module
needs to be added to `index.rst` in the api-folder.
When submitting a new function, method or class, docstrings are required before
the new code will be pulled into the dev branch. Documentation is created using
`Sphinxdoc <http://www.sphinx-doc.org>`_. Docstrings within the method or class
need to be written in `NumPy docformat <https://numpydoc
.readthedocs.io/en/latest/format.html#docstring-standard>`_ to enable automatic
documentation on this website. In the case of a new module, the module needs to
be added to `index.rst` in the api-folder.

A Jupyter Notebook explaining the use of your new code can be added the to
examples folder. This Notebook will also be automatically converted and
placed on the Examples page on this website.
examples folder. This Notebook will also be automatically converted and placed
on the Examples page on this website.

6. Create a pull request
------------------------
Once you have written, tested, and documented your code you can start a pull
request on the development branch (dev) of Pastas. Pull requests can only
be submitted to the dev-branch and need to be reviewed by one of the core
request on the development branch (dev) of Pastas. Pull requests can only be
submitted to the dev-branch and need to be reviewed by one of the core
developers. When you start your Pull Request, you will automatically see a
checklist to go through to check if your PR is up to standards. Pastas will
run automatic code tests to ensure that the code works, is documented and
has a good code style.
checklist to go through to check if your PR is up to standards. Pastas will run
automatic code tests to ensure that the code works, is documented and has a
good code style.

7. Share and enjoy your work!
-----------------------------
After you have create a Pull Request the Core Development Team will
review your code and discuss potential improvements on GitHub before merging
your code into the development branch. After a successful Pull Request your
code will be included in the next release of Pastas when the master-branch is
updated. Congratulations, you are now officially a contributor to the Pastas
project!
After you have create a Pull Request the Core Development Team will review your
code and discuss potential improvements on GitHub before merging your code into
the development branch. After a successful Pull Request your code will be
included in the next release of Pastas when the master-branch is updated.
Congratulations, you are now officially a contributor to the Pastas project!
2 changes: 1 addition & 1 deletion pastas/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ def list_datasets(silent: bool = True) -> List[str]:
for folder in data:
print(f" - {folder}")
print(
f"Use ps.load_dataset('folder_name') to load a dataset from the repository."
"Use ps.load_dataset('folder_name') to load a dataset from the repository."
)
return data
8 changes: 5 additions & 3 deletions pastas/extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import logging

from pastas.extensions.accessor import register_model_accessor
from pastas.extensions.accessor import (
register_model_accessor as register_model_accessor,
)

logger = logging.getLogger(__name__)


def register_plotly():
"""Register Plotly extension for pastas.Model class for interactive plotting."""
from pastas.plotting.plotly import Plotly
from pastas.plotting.plotly import Plotly as Plotly

logger.info("Registered plotly extension in Model class, e.g. `ml.plotly.plot()`.")


def register_bokeh():
"""Register Bokeh extension for pastas.Model class for interactive plotting."""
from pastas.plotting.bokeh import Bokeh
from pastas.plotting.bokeh import Bokeh as Bokeh

logger.info("Registered bokeh extension in Model class, e.g. `ml.bokeh.plot()`.")
2 changes: 1 addition & 1 deletion pastas/extensions/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _register_accessor(name: str, cls):
.. code-block:: python
def __init__(self, pastas_object): # noqa: E999
def __init__(self, pastas_object):
...
Examples
Expand Down
1 change: 0 additions & 1 deletion pastas/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from os import path

from packaging import version
from pandas import to_numeric

import pastas as ps

Expand Down
6 changes: 4 additions & 2 deletions pastas/io/pas.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def pastas_hook(obj: dict):
elif key == "series":
try:
obj[key] = read_json(stringIO(value), typ="series", orient="split")
except:
except Exception as e:
logger.debug(e)
obj[key] = value
if isinstance(obj[key], Series):
obj[key].index = obj[key].index.tz_localize(None)
Expand All @@ -64,7 +65,8 @@ def pastas_hook(obj: dict):
else:
try:
obj[key] = json.loads(value, object_hook=pastas_hook)
except:
except Exception as e:
logger.debug(e)
obj[key] = value
return obj

Expand Down
7 changes: 3 additions & 4 deletions pastas/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from pastas.decorators import get_stressmodel
from pastas.io.base import _load_model, dump
from pastas.modelstats import Statistics
from pastas.noisemodels import ArNoiseModel
from pastas.plotting.modelplots import Plotting, _table_formatter_stderr
from pastas.rfunc import HantushWellModel
from pastas.solver import LeastSquares
Expand All @@ -38,10 +37,9 @@
get_sample,
)
from pastas.transform import ThresholdTransform
from pastas.typing import ArrayLike
from pastas.typing import ArrayLike, Solver, StressModel, TimestampType
from pastas.typing import Model as ModelType
from pastas.typing import NoiseModel as NoiseModelType
from pastas.typing import Solver, StressModel, TimestampType
from pastas.utils import validate_name
from pastas.version import __version__

Expand Down Expand Up @@ -1820,7 +1818,8 @@ def _get_file_info(self) -> dict:

try:
file_info["owner"] = getlogin()
except:
except Exception as e:
logger.debug(e)
file_info["owner"] = "Unknown"

return file_info
Expand Down
4 changes: 2 additions & 2 deletions pastas/objective_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""This module contains the objective functions that can be used with the pastas
`EmceeSolve` solver.
"""This module contains the objective functions that can be used with the pastas
`EmceeSolve` solver.
"""

Expand Down

0 comments on commit 3b3133e

Please sign in to comment.