Skip to content

NPY201 triggers on code written to support both numpy 2 and legacy numpy #12476

@jenshnielsen

Description

@jenshnielsen

Considder an example like this which is explicitly written to support both numpy 2 and legacy numpy

import warnings

try:
    from numpy.exceptions import VisibleDeprecationWarning
except ImportError:
    # numpy < 1.25.0
    from numpy import VisibleDeprecationWarning


with warnings.catch_warnings():
    warnings.filterwarnings(
        "ignore",
        category=VisibleDeprecationWarning,
        message="Creating an ndarray from ragged nested sequences",
    )

Running ruff check file.py with NPY201 enabled triggers

error: Failed to create fix for Numpy2Deprecation: Unable to insert `VisibleDeprecationWarning` into scope due to name conflict
src\qcodes\utils\numpy_utils.py:13:18: NPY201 `np.VisibleDeprecationWarning` will be removed in NumPy 2.0. Use `numpy.exceptions.VisibleDeprecationWarning` instead.
   |
11 |     warnings.filterwarnings(
12 |         "ignore",
13 |         category=VisibleDeprecationWarning,
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ NPY201
14 |         message="Creating an ndarray from ragged nested sequences",
15 |     )
   |
   = help: Replace with `numpy.exceptions.VisibleDeprecationWarning`

This is a false positive since the deprecated location is only used in legacy numpy versions that do not have a exceptions submodule.

This started happening in 0.5.1 probably as a result of #12065

Keyworkds Numpy, NPY201

Ruff 0.5.1 to 0.5.4

Config:

[tool.ruff.lint]
select = ["NPY201"]

Metadata

Metadata

Assignees

Labels

ruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions