Skip to content
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

Translate more banned import config to ruff #57283

Merged
merged 2 commits into from
Feb 13, 2024
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
23 changes: 3 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,39 +208,22 @@ repos:
language: pygrep
entry: |
(?x)
# pytest.xfail instead of pytest.mark.xfail
pytest\.xfail

# imports from pandas._testing instead of `import pandas._testing as tm`
|from\ pandas\._testing\ import
from\ pandas\._testing\ import
|from\ pandas\ import\ _testing\ as\ tm

# No direct imports from conftest
|conftest\ import
|import\ conftest

# pandas.testing instead of tm
|pd\.testing\.

# pd.api.types instead of from pandas.api.types import ...
|(pd|pandas)\.api\.types\.

# np.testing, np.array_equal
|(numpy|np)(\.testing|\.array_equal)

# unittest.mock (use pytest builtin monkeypatch fixture instead)
|(unittest(\.| import )mock|mock\.Mock\(\)|mock\.patch)
# np.array_equal
|(numpy|np)\.array_equal

# pytest raises without context
|\s\ pytest.raises

# TODO
# pytest.warns (use tm.assert_produces_warning instead)
# |pytest\.warns

# os.remove
|os\.remove

# Unseeded numpy default_rng
|default_rng\(\)
files: ^pandas/tests/
Expand Down
4 changes: 2 additions & 2 deletions doc/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def html(self):
ret_code = self._sphinx_build("html")
zip_fname = os.path.join(BUILD_PATH, "html", "pandas.zip")
if os.path.exists(zip_fname):
os.remove(zip_fname)
os.remove(zip_fname) # noqa: TID251

if ret_code == 0:
if self.single_doc_html is not None:
Expand Down Expand Up @@ -285,7 +285,7 @@ def zip_html(self) -> None:
"""
zip_fname = os.path.join(BUILD_PATH, "html", "pandas.zip")
if os.path.exists(zip_fname):
os.remove(zip_fname)
os.remove(zip_fname) # noqa: TID251
dirname = os.path.join(BUILD_PATH, "html")
fnames = os.listdir(dirname)
os.chdir(dirname)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_series_setitem(indexer):

# using custom check instead of tm.assert_produces_warning because that doesn't
# fail if multiple warnings are raised
with pytest.warns() as record:
with pytest.warns() as record: # noqa: TID251
df["a"][indexer] = 0
assert len(record) == 1
assert record[0].category == ChainedAssignmentError
Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@ exclude = [

[tool.ruff.lint.flake8-tidy-imports.banned-api]
"urllib.request.urlopen".msg = "Use pandas.io.common.urlopen instead of urllib.request.urlopen"
# numpy.random is banned but np.random is not. Is this intentional?
# "numpy.random".msg = "Do not use numpy.random"
"pytest.warns".msg = "Use tm.assert_produces_warning instead of pytest.warns"
"pytest.xfail".msg = "Use pytest.mark.xfail instead of pytest.xfail"
"conftest".msg = "No direct imports from conftest"
"numpy.testing".msg = "Do not use numpy.testing"
# "numpy.array_equal".msg = "Do not use numpy.array_equal" # Used in pandas/core
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just ignore it's usage in pandas/core?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, We can only ignore TID251 as a whole, we can't just ignore enforcing this rule for numpy.array_equal in pandas/core

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Can we remove some of the old checks that are covered by this section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's done in the latest commit, you can take a look.

"unittest.mock".msg = "use pytest builtin monkeypatch fixture instead"
"os.remove".msg = "Do not use os.remove"



[tool.ruff.per-file-ignores]
# relative imports allowed for asv_bench
Expand Down
2 changes: 1 addition & 1 deletion web/tests/test_pandas_web.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import (
from unittest.mock import ( # noqa: TID251
mock_open,
patch,
)
Expand Down