Skip to content
This repository was archived by the owner on Feb 19, 2023. It is now read-only.

add check for array_equal and np.testing #27

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ a linter for pandas usage, please see [pandas-vet](https://github.com/deppen8/pa
| PDF022 | found import from 'numpy.random' |
| PDF023 | found assignment to single-letter variable |
| PDF024 | found string join() with generator expressions |
| PDF025 | found 'np.testing' or 'np.array_equal' (use 'pandas._testing' instead) |

## contributing

See `contributing.md` for how to get started.
Expand Down
40 changes: 40 additions & 0 deletions pandas_dev_flaker/_plugins_tree/numpy_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import ast
from typing import Iterator, Tuple

from pandas_dev_flaker._data_tree import State, register

MSG = (
"PDF025 found 'np.testing' or 'np.array_equal' "
"(use 'pandas._testing' instead)"
)


@register(ast.ImportFrom)
def visit_ImportFrom(
state: State,
node: ast.ImportFrom,
parent: ast.AST,
) -> Iterator[Tuple[int, int, str]]:
if (
(
"testing" in {name.name for name in node.names}
or "array_equal" in {name.name for name in node.names}
)
and node.module is not None
and node.module == "numpy"
) or (node.module is not None and node.module == "numpy.testing"):
yield node.lineno, node.col_offset, MSG


@register(ast.Attribute)
def visit_Attribute(
state: State,
node: ast.Attribute,
parent: ast.AST,
) -> Iterator[Tuple[int, int, str]]:
if (
node.attr in {"testing", "array_equal"}
and isinstance(node.value, ast.Name)
and node.value.id in {"numpy", "np"}
):
yield node.lineno, node.col_offset, MSG
77 changes: 77 additions & 0 deletions tests/numpy_testing_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import ast
import tokenize
from io import StringIO

import pytest

from pandas_dev_flaker.__main__ import run


def results(s):
return {
"{}:{}: {}".format(*r)
for r in run(
ast.parse(s),
list(tokenize.generate_tokens(StringIO(s).readline)),
)
}


@pytest.mark.parametrize(
"source",
(
pytest.param(
"import numpy as np\nnp.tester",
id="tester instead of testing",
),
),
)
def test_noop(source):
assert not results(source)


@pytest.mark.parametrize(
"source, expected",
(
pytest.param(
"import numpy\nnumpy.testing",
"2:0: PDF025 found 'np.testing' or 'np.array_equal' "
"(use 'pandas._testing' instead)",
id="access from numpy",
),
pytest.param(
"import numpy as np\nnp.testing",
"2:0: PDF025 found 'np.testing' or 'np.array_equal' "
"(use 'pandas._testing' instead)",
id="access from np",
),
pytest.param(
"import numpy as np\n"
"np.testing.assert_array_equal(result, expected)",
"2:0: PDF025 found 'np.testing' or 'np.array_equal' "
"(use 'pandas._testing' instead)",
id="np.testing.assert_array_equal",
),
pytest.param(
"from numpy import testing",
"1:0: PDF025 found 'np.testing' or 'np.array_equal' "
"(use 'pandas._testing' instead)",
id="import from numpy",
),
pytest.param(
"from numpy.testing import assert_array_equal",
"1:0: PDF025 found 'np.testing' or 'np.array_equal' "
"(use 'pandas._testing' instead)",
id="import from numpy",
),
pytest.param(
"from numpy import array_equal",
"1:0: PDF025 found 'np.testing' or 'np.array_equal' "
"(use 'pandas._testing' instead)",
id="array_equal",
),
),
)
def test_violation(source, expected):
(result,) = results(source)
assert result == expected