Skip to content

Conversation

@dopplershift
Copy link
Member

Description Of Changes

  • Add a config for Ruff and replace as much of our linting config as we can with that
  • Clean up Ruff-found lint
  • Migrate as much as possible from setup.cfg to pyproject.toml
  • Fix up warnings to use a proper stacklevel

Ruff is a Rust-based linter that is much faster. While speed isn't our primary concern (depending on how you feel about flake8 taking 5 seconds to run on our entire code base), it is nice to have it run instantly. It also makes it easier to migrate some linting to pre-commit as well. There's not much left on the basic flake8, and there is the prospect that we'll be able to migrate our custom flake8 plugin to this at some point in the future. Ruff also gives the option of automatically fixing some things. In fact, it fixed itself a lot of the code changes here--including removing our Python 2 handling/message, which seems fine at this point.

Ruff also can do some things like pyupgrade but automatically based on the python version required in the package metadata in pyproject.toml, so I took the opportunity to migrate us as much as possible away from setup.cfg. Flake8 itself will likely never support this, so we're stuck with what we have until we fully drop flake8. The biggest hurdle right now is that Ruff's implementation of isort doesn't yet support controlling how line wrapping is done for imports.

I also went ahead and fixed up the stacklevel "errors" that come from the new Bugbear version (Ruff also detects this--this closes #2966). Unfortunately, due to our extensive use of decorators, it's not trivial to determine the fixed stack level that gets to the user code, so I added a helper for warnings that does stack introspection to do this for us (based on similar code in Pandas). As an example of the stack level improvement, for this code:

import metpy.calc as mpcalc
from metpy.units import units
mpcalc.wind_components(10 * units.knots, 300)

it previously would give:

/Users/rmay/repos/metpy/src/metpy/calc/basic.py:1297: UserWarning: Input over 12.566370614359172 radians. Ensure proper units are given.
  warnings.warn('Input over {} radians. '

and now gives:

/var/folders/r8/bhrhn4m56lgdckj491rm6_w40000gp/T/ipykernel_67651/741032694.py:4: UserWarning: Input over 12.566370614359172 radians. Ensure proper units are given.
  mpcalc.wind_components(10 * units.knots, 300)

@dopplershift dopplershift added Area: Infrastructure Pertains to project infrastructure (e.g. CI, linting) Type: Maintenance Updates and clean ups (but not wrong) labels Mar 24, 2023
@dopplershift dopplershift requested review from a team and jthielen as code owners March 24, 2023 20:29
@dopplershift dopplershift requested review from dcamron and removed request for a team March 24, 2023 20:29
@dopplershift dopplershift force-pushed the ruff branch 2 times, most recently from 743dca2 to 9e4d29c Compare March 24, 2023 20:42
We can then try to slowly dial this back. We could get quite a bit lower
by fixing just a few offenders.
This isn't trivial due to all the decorators, so add a function (based
on similar functionality in Pandas) to search the stack for the first
frame that's outside the MetPy code base.
@dcamron dcamron merged commit 59a515d into Unidata:main Apr 3, 2023
@github-actions github-actions bot added this to the April 2023 milestone Apr 3, 2023
@dopplershift dopplershift deleted the ruff branch April 3, 2023 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Infrastructure Pertains to project infrastructure (e.g. CI, linting) Type: Maintenance Updates and clean ups (but not wrong)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants