Skip to content

ruff: Enable NPY (NumPy-specific) rules #2779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
seisman opened this issue Oct 30, 2023 · 1 comment · Fixed by #2781, #2787 or #2782
Closed

ruff: Enable NPY (NumPy-specific) rules #2779

seisman opened this issue Oct 30, 2023 · 1 comment · Fixed by #2781, #2787 or #2782
Labels
maintenance Boring but important stuff for the core devs
Milestone

Comments

@seisman
Copy link
Member

seisman commented Oct 30, 2023

Currently, ruff provides three NumPy-specific rules (https://docs.astral.sh/ruff/rules/#numpy-specific-rules-npy), which can help us avoid deprecated NumPy functions.

To enable NumPy-specific rules, we just need to add NPY to the select option below:

pygmt/pyproject.toml

Lines 91 to 97 in 8f1d476

select = [
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
"W", # pycodestyle warnings
]

After enabling the NPY rules, running make format gives many [NPY002]*https://docs.astral.sh/ruff/rules/numpy-legacy-random/) warnings, like:

examples/gallery/histograms/histogram.py:15:1: NPY002 Replace legacy `np.random.seed` call with `np.random.Generator`
   |
13 | import pygmt
14 |
15 | np.random.seed(100)
   | ^^^^^^^^^^^^^^ NPY002
16 |
17 | # Generate random elevation data from a normal distribution
   |

The usage of np.random.seed(100) is deprecated. We should use something like below instead:

rng = np.random.default_rng(100)
rng.random()
@seisman seisman added maintenance Boring but important stuff for the core devs help wanted Helping hands are appreciated labels Oct 30, 2023
@yvonnefroehlich
Copy link
Member

yvonnefroehlich commented Oct 30, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment