Skip to content

Commit

Permalink
ruff compat (#798)
Browse files Browse the repository at this point in the history
* ruff compat

* lint

* simple imports

* autoupdate

* lint

---------

Co-authored-by: MarcoGorelli <>
  • Loading branch information
MarcoGorelli committed Feb 16, 2023
1 parent 3e51866 commit 01fcdd9
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 24 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ repos:
- id: trailing-whitespace
- id: debug-statements
- repo: https://github.com/pre-commit/pre-commit
rev: v2.21.0
rev: v3.0.4
hooks:
- id: validate_manifest
- repo: https://github.com/psf/black
rev: 22.12.0
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-prettier
Expand All @@ -24,7 +24,7 @@ repos:
- id: prettier
exclude: ^README\.md$
- repo: https://github.com/PyCQA/pylint
rev: v2.16.0b1
rev: v2.16.2
hooks:
- id: pylint
files: ^(nbqa|tests)/
Expand All @@ -35,7 +35,7 @@ repos:
- id: flake8
additional_dependencies: [flake8-typing-imports==1.14.0]
- repo: https://github.com/PyCQA/isort
rev: 5.11.4
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/PyCQA/pydocstyle
Expand All @@ -50,7 +50,7 @@ repos:
args: [-v, --fail-under=100]
files: ^nbqa/
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.0.0
hooks:
- id: mypy
exclude: ^docs/
Expand Down Expand Up @@ -84,7 +84,7 @@ repos:
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/PyCQA/autoflake
rev: v2.0.0
rev: v2.0.1
hooks:
- id: autoflake
args: [--remove-all-unused-imports, -i]
Expand Down
2 changes: 0 additions & 2 deletions nbqa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ def _get_configs(cli_args: CLIArgs, project_root: Path) -> Configs:
# If a section is in pyproject.toml, use that.
pyproject_path = project_root / "pyproject.toml"
if pyproject_path.is_file():

config_file = tomli.loads(pyproject_path.read_text("utf-8"))
if "tool" in config_file and "nbqa" in config_file["tool"]:
file_config = config_file["tool"]["nbqa"]
Expand Down Expand Up @@ -639,7 +638,6 @@ def _main(cli_args: CLIArgs, configs: Configs) -> int:
sys.stderr.write(str(exc))
return 1
try: # pylint disable=R0912

if not nb_to_tmp_mapping:
sys.stderr.write("No notebooks found in given path(s)\n")
return 0
Expand Down
1 change: 0 additions & 1 deletion nbqa/find_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def find_project_root(
)

for directory in (common_base, *common_base.parents):

for known_project_root_dir in root_dirs:
if (directory / known_project_root_dir).is_dir():
return directory
Expand Down
1 change: 0 additions & 1 deletion nbqa/replace_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ def _print_diff(code_cell_number: int, cell_diff: Iterator[str]) -> bool:
# https://github.com/psf/black/blob/9a73bb86db59de1e12426fec81dcdb7f3bb9be7b/src/black/output.py#L79-L92
line_changes = []
for line in cell_diff:

if line.startswith("+++") or line.startswith("---"):
line_changes.append("\033[1;37m" + line + "\033[0m") # bold white, reset
elif line.startswith("@@"):
Expand Down
1 change: 1 addition & 0 deletions nbqa/save_code_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
NEWLINE = "\n"
NEWLINES = defaultdict(lambda: NEWLINE * 3)
NEWLINES["isort"] = NEWLINE * 2
NEWLINES["ruff"] = NEWLINE * 2
TRANSFORMED_MAGICS = frozenset(
(
"get_ipython().run_cell_magic",
Expand Down
56 changes: 56 additions & 0 deletions tests/data/simple_imports.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"skip-flake8"
]
},
"outputs": [],
"source": [
"import os\n",
"\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cwd = os.getcwd()\n",
"x = np.arange(1, 10)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"vscode": {
"interpreter": {
"hash": "864e9cd7dbf32e150cc52d563f4c3a35053498b9e9d82e11d8edd2e505b61bc9"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 0 additions & 1 deletion tests/tools/test_mdformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from nbqa.__main__ import main

if TYPE_CHECKING:

from _pytest.capture import CaptureFixture


Expand Down
1 change: 0 additions & 1 deletion tests/tools/test_pyupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from nbqa.__main__ import main

if TYPE_CHECKING:

from _pytest.capture import CaptureFixture


Expand Down
42 changes: 30 additions & 12 deletions tests/tools/test_ruff_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ def test_ruff_works(

out, err = capsys.readouterr()
expected_out = (
"Found 13 error(s).\n"
f"{expected_path_1}:cell_1:1:8: F401 `os` imported but unused\n"
f"{expected_path_1}:cell_1:3:8: F401 `glob` imported but unused\n"
f"{expected_path_1}:cell_1:5:8: F401 `nbqa` imported but unused\n"
f"{expected_path_0}:cell_1:1:8: F401 `os` imported but unused\n"
f"{expected_path_0}:cell_1:3:8: F401 `glob` imported but unused\n"
f"{expected_path_0}:cell_1:5:8: F401 `nbqa` imported but unused\n"
f"{expected_path_1}:cell_1:1:8: F401 [*] `os` imported but unused\n"
f"{expected_path_1}:cell_1:3:8: F401 [*] `glob` imported but unused\n"
f"{expected_path_1}:cell_1:5:8: F401 [*] `nbqa` imported but unused\n"
f"{expected_path_0}:cell_1:1:8: F401 [*] `os` imported but unused\n"
f"{expected_path_0}:cell_1:3:8: F401 [*] `glob` imported but unused\n"
f"{expected_path_0}:cell_1:5:8: F401 [*] `nbqa` imported but unused\n"
f"{expected_path_0}:cell_4:1:1: E402 Module level import not at top of file\n"
f"{expected_path_0}:cell_4:1:20: F401 `random.randint` imported but unused\n"
f"{expected_path_0}:cell_4:1:20: F401 [*] `random.randint` imported but unused\n"
f"{expected_path_0}:cell_5:1:1: E402 Module level import not at top of file\n"
f"{expected_path_0}:cell_5:2:1: E402 Module level import not at top of file\n"
f"{expected_path_2}:cell_1:1:8: F401 `os` imported but unused\n"
f"{expected_path_2}:cell_1:3:8: F401 `glob` imported but unused\n"
f"{expected_path_2}:cell_1:5:8: F401 `nbqa` imported but unused\n"
"10 potentially fixable with the --fix option.\n"
f"{expected_path_2}:cell_1:1:8: F401 [*] `os` imported but unused\n"
f"{expected_path_2}:cell_1:3:8: F401 [*] `glob` imported but unused\n"
f"{expected_path_2}:cell_1:5:8: F401 [*] `nbqa` imported but unused\n"
"Found 13 errors.\n"
"[*] 10 potentially fixable with the --fix option.\n"
)
assert "\n".join(sorted(out.replace("\r\n", "\n").splitlines())) == "\n".join(
sorted(expected_out.splitlines())
Expand All @@ -91,3 +91,21 @@ def test_cell_with_all_magics(capsys: "CaptureFixture") -> None:
out, err = capsys.readouterr()
assert out == ""
assert err == ""


def test_ruff_isort(capsys: "CaptureFixture") -> None:
"""
Should ignore cell with all magics.
Parameters
----------
capsys
Pytest fixture to capture stdout and stderr.
"""

path = os.path.join("tests", "data", "simple_imports.ipynb")
main(["ruff", path, "--select=I"])

out, err = capsys.readouterr()
assert out == ""
assert err == ""

0 comments on commit 01fcdd9

Please sign in to comment.