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

Fix clash with ruff format v0.3.0 enforcing single blank line after module doc string #11

Merged
merged 3 commits into from
Mar 3, 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
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Python 3.10
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.10"
cache: pip
Expand All @@ -34,7 +34,7 @@ jobs:
if: github.event_name == 'release' && needs.tests.result == 'success'
steps:
- name: Check out repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Build and upload dist
run: |
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ exclude: tests/fixtures/raw_nb.py

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
rev: v0.3.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.0
rev: v1.8.0
hooks:
- id: mypy

Expand Down
21 changes: 21 additions & 0 deletions format_ipy_cells/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,24 @@ def delete_last_cell_if_empty(text: str) -> str:
"""
# \Z matches only at end of string
return re.sub(r"(?m)\s+^# %%\s*\Z", r"\n", text)


def single_blank_line_after_doc_string(text: str) -> str:
"""Ensure single blank line between cell delimiter and preceding module doc string.

Added to avoid clash with ruff format. Started enforcing single blank line in 0.3.0.

--- before ---
'''module doc string.'''


# %%
some_code = 'here'

--- after ---
'''module doc string.'''

# %%
some_code = 'here'
"""
return re.sub(r'(?m)(["\']{3})\n+(# %%)', r"\1\n\n\2", text)
3 changes: 3 additions & 0 deletions format_ipy_cells/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
format_comments_after_cell_delimiters,
remove_empty_cells,
remove_empty_lines_starting_cell,
single_blank_line_after_doc_string,
)


Expand Down Expand Up @@ -39,6 +40,8 @@ def fix_file(filename: str) -> int:

text = delete_last_cell_if_empty(text)

text = single_blank_line_after_doc_string(text)

if orig_text != text:
print(f"Rewriting {filename}")

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ no_implicit_optional = false
target-version = "py310"
select = ["ALL"]
ignore = [
"COM812", # trailing comma missing
"D100", # Missing docstring in public module
"D104", # Missing docstring in public package
"D205", # 1 blank line required between summary line and description
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/clean_nb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""module doc string."""

# %%
foo = "hello"

Expand All @@ -9,6 +11,7 @@
# %% a comment following a cell delimiter with too much space
baz = 42

"""longer comment"""

# %% empty cell with comment (should not be removed despite empty)

Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/raw_nb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
"""module doc string."""


# %%
foo = "hello"
# %%a comment following a cell delimiter without space
bar = "world"
# %% a comment following a cell delimiter with too much space
baz = 42

"""longer comment"""



# %% empty cell with comment (should not be removed despite empty)


Expand Down
15 changes: 12 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import difflib
import filecmp
import shutil
from importlib.metadata import version
Expand All @@ -15,10 +16,18 @@ def test_main_format_cells(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -
shutil.copy2(clean_nb, clean_tmp := str(tmp_path / "clean_nb.py"))

ret = main((raw_tmp, clean_tmp))

assert ret == 1
assert filecmp.cmp(raw_tmp, clean_nb), "Formatted file has unexpected content"
assert filecmp.cmp(clean_tmp, clean_nb), "clean file should not have changed"

with open(clean_nb) as file, open(clean_tmp) as tmp_file:
clean_lines, tmp_lines = file.readlines(), tmp_file.readlines()
file_diff = "\n".join(difflib.unified_diff(clean_lines, tmp_lines))

assert filecmp.cmp(
raw_tmp, clean_nb, shallow=False
), f"Formatted file has unexpected content:\n{file_diff}"
assert filecmp.cmp(
clean_tmp, clean_nb, shallow=False
), "clean file should not have changed"

out, err = capsys.readouterr()
assert out == f"Rewriting {raw_tmp}\n"
Expand Down