mbakker7 / timml

An analytic element model for steady multi-layer flow
MIT License
38 stars 21 forks source link

Use ruff to format and lint #114

Closed Huite closed 2 months ago

Huite commented 5 months ago

In a nutshell:

There's some funny stuff here and there, which is touched by ruff, but not everything is fixed. For example, this bit in stripareasink changetrange:

            xn = x1 + u * (x2 - x1)
            yn = y1 + u * (y2 - y1)
            zn = xyzt1[2] + u * (xyzt2[2] - xyzt1[2])

None of these are used...? ruff has removed the assignment, but I think the lines could be deleted outright.

Huite commented 5 months ago

I'm not sure why the coverage report is way down, but I've remember seeing sudden changes in coverage reports before (e.g. flopy).

I don't think the coverage is super meaningful at the moment, though, since most of the tests don't check the answers. It's still nice to know there's no syntax errors though -- although I think ruff should catch that now.

dbrakenhoff commented 5 months ago

One more thought, we could consider adding isort and formatting checks using ruff, instead of only checking linting?

Huite commented 5 months ago

One more thought, we could consider adding isort and formatting checks using ruff, instead of only checking linting?

Running ruff check takes care of this.

If I scramble the imports:

import timml as tml
import numpy as np
import matplotlib.pyplot as plt

Ruff will complain appropriately:

❯ ruff check
examples\workshop_nhv\vlaketunnel_functions.py:3:1: I001 [*] Import block is un-sorted or un-formatted
Found 1 error.
[*] 1 fixable with the `--fix` option.

It's part of the current configuration:

[tool.ruff.lint]
# See: https://docs.astral.sh/ruff/rules/
select = [
    "C4",  # flake8-comprehensions
    "E",  # pycodestyle
    "F",  # pyflakes
    "I",  # isort
    "PT",  # pytest-style
    "D",  # pydocstyle
    "B",  # flake8-bugbear
    "NPY",  # numpy
]
dbrakenhoff commented 5 months ago

Sorry, i forgot import sorting is checked by ruff check. A code formatting check (a la black) can be added with ruff format --check? Or does ruff check do that as well?

Huite commented 5 months ago

You're right, turns out I was a little confused...

isort is part of the linting, but the formatting isn't. To check for the formatting, we need to run ruf format --check explicitly. I will add it to the CI.

Huite commented 5 months ago

Well, I just got confirmation from the CI that it does check:

All checks passed!
Would reformat: examples/timml_notebook0_sol.py
Would reformat: examples/workshop_nhv/vlaketunnel_functions.py
2 files would be reformatted, 31 files already formatted
Error: Process completed with exit code 1.

Turns out I had the ignore examples bit still in locally...

dbrakenhoff commented 2 months ago

Well that was dumb... completely forgot this PR was already open, and went and created my own... I'm gonna merge the two and then pull that into dev.