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

[pre-commit.ci] pre-commit autoupdate #70

Merged
merged 2 commits into from
Jan 21, 2025
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: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: 'v0.8.6'
rev: 'v0.9.2'
hooks:
# Run the formatter.
- id: ruff-format
Expand Down
4 changes: 2 additions & 2 deletions auto_walrus.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ def auto_walrus(
continue
# replace assignment
line_without_assignment = (
f"{lines[_assignment[1]-1][:_assignment[2]]}"
f"{lines[_assignment[1]-1][_assignment[4]:]}"
f"{lines[_assignment[1] - 1][: _assignment[2]]}"
f"{lines[_assignment[1] - 1][_assignment[4] :]}"
)
if (ENDS_WITH_COMMENT.search(lines[_assignment[1] - 1]) is not None) or (
ENDS_WITH_COMMENT.search(lines[_if_statement[1] - 1]) is not None
Expand Down
63 changes: 26 additions & 37 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
("src", "expected"),
[
(
"def foo():\n" " a = 0\n" " if a:\n" " print(a)\n",
"def foo():\n" " if (a := 0):\n" " print(a)\n",
"def foo():\n a = 0\n if a:\n print(a)\n",
"def foo():\n if (a := 0):\n print(a)\n",
),
(
"def foo():\n" " a = 0\n" " if a > 3:\n" " print(a)\n",
"def foo():\n" " if (a := 0) > 3:\n" " print(a)\n",
"def foo():\n a = 0\n if a > 3:\n print(a)\n",
"def foo():\n if (a := 0) > 3:\n print(a)\n",
),
(
"def foo():\n"
Expand All @@ -29,11 +29,7 @@
" print(a)\n"
" else:\n"
" pass\n",
"def foo():\n"
" if (a := 0):\n"
" print(a)\n"
" else:\n"
" pass\n",
"def foo():\n if (a := 0):\n print(a)\n else:\n pass\n",
),
(
"def foo():\n"
Expand All @@ -49,24 +45,20 @@
" print(a)\n",
),
(
"def foo():\n"
" a = 0\n"
" print(0)\n"
" if a:\n"
" print(a)\n",
"def foo():\n" " print(0)\n" " if (a := 0):\n" " print(a)\n",
"def foo():\n a = 0\n print(0)\n if a:\n print(a)\n",
"def foo():\n print(0)\n if (a := 0):\n print(a)\n",
),
(
"def foo():\n" " a = 0\n" " if (a):\n" " print(a)\n",
"def foo():\n" " if (a := 0):\n" " print(a)\n",
"def foo():\n a = 0\n if (a):\n print(a)\n",
"def foo():\n if (a := 0):\n print(a)\n",
),
(
"def foo():\n" " b = 0; a = 0\n" " if a:\n" " print(a)\n",
"def foo():\n" " b = 0; \n" " if (a := 0):\n" " print(a)\n",
"def foo():\n b = 0; a = 0\n if a:\n print(a)\n",
"def foo():\n b = 0; \n if (a := 0):\n print(a)\n",
),
(
"def foo():\n" " a = 0\n" " if a:\n" " print(a)",
"def foo():\n" " if (a := 0):\n" " print(a)",
"def foo():\n a = 0\n if a:\n print(a)",
"def foo():\n if (a := 0):\n print(a)",
),
(
"def foo():\n"
Expand Down Expand Up @@ -110,29 +102,26 @@ def test_rewrite(src: str, expected: str) -> None:
" b[0] = 1\n"
" if a:\n"
" print(a)\n",
"def foo():\n" " a = 1\n" " a = 2\n" " if a:\n" " print(a)\n",
"def foo():\n" " a = (\n" " 0,)\n" " if a:\n" " print(a)\n",
"def foo():\n" " a = (b==True)\n" " if a:\n" " print(a)\n",
"def foo():\n a = 1\n a = 2\n if a:\n print(a)\n",
"def foo():\n a = (\n 0,)\n if a:\n print(a)\n",
"def foo():\n a = (b==True)\n if a:\n print(a)\n",
"def foo():\n"
" a = thequickbrownfoxjumpsoverthelazydog\n"
" if a:\n"
" print(a)\n",
"def foo():\n" " a = 0 # no-walrus\n" " if a:\n" " print(a)\n",
"def foo():\n" " a = 0\n" " if a: # no-walrus\n" " print(a)\n",
"n = 10\n" "if foo(a := n+1):\n" " print(n)\n",
"a = 0\n" "if False and a:\n" " print(a)\n" "else:\n" " print(a)\n",
"def foo():\n" " a = 1\n" " if a:\n" " print(a)\n" " a = 2\n",
"def foo():\n a = 0 # no-walrus\n if a:\n print(a)\n",
"def foo():\n a = 0\n if a: # no-walrus\n print(a)\n",
"n = 10\nif foo(a := n+1):\n print(n)\n",
"a = 0\nif False and a:\n print(a)\nelse:\n print(a)\n",
"def foo():\n a = 1\n if a:\n print(a)\n a = 2\n",
"def foo():\n"
" n = 10\n"
" if True:\n"
" pass\n"
" elif foo(a := n+1):\n"
" print(n)\n",
"def foo():\n"
" n = 10\n"
" if n > np.sin(foo.bar.quox):\n"
" print(n)\n",
"def foo():\n" " n = 10\n" " if True or n > 3:\n" " print(n)\n",
"def foo():\n n = 10\n if n > np.sin(foo.bar.quox):\n print(n)\n",
"def foo():\n n = 10\n if True or n > 3:\n print(n)\n",
],
)
def test_noop(src: str) -> None:
Expand All @@ -142,8 +131,8 @@ def test_noop(src: str) -> None:

ProjectDirT = Tuple[pathlib.Path, List[pathlib.Path]]

SRC_ORIG = "def foo():\n" " a = 0\n" " if a:\n" " print(a)\n"
SRC_CHANGED = "def foo():\n" " if (a := 0):\n" " print(a)\n"
SRC_ORIG = "def foo():\n a = 0\n if a:\n print(a)\n"
SRC_CHANGED = "def foo():\n if (a := 0):\n print(a)\n"


@pytest.fixture
Expand Down Expand Up @@ -173,7 +162,7 @@ def project_dir(request: Any, tmp_path: pathlib.Path) -> ProjectDirT:
return tmp_path, python_files


PROJECT_CONFIG_EXCLUDE_A = "[tool.auto-walrus]\n" 'exclude = "/a"\n'
PROJECT_CONFIG_EXCLUDE_A = '[tool.auto-walrus]\nexclude = "/a"\n'


@pytest.mark.config_content(PROJECT_CONFIG_EXCLUDE_A)
Expand Down
Loading