Skip to content

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

@seisman

Description

@seisman

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    maintenanceBoring but important stuff for the core devs

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions