Skip to content

Commit

Permalink
Rename function
Browse files Browse the repository at this point in the history
  • Loading branch information
Semir Vrana authored and Semir Vrana committed Jul 10, 2024
1 parent aa99fc4 commit a4b6137
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions nbisort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import more_itertools
import nbformat

IMPORT_RGX = re.compile(
TOPLEVEL_IMPORT_RGX = re.compile(
r"^(from\s+\w+(?:\.\w+)*\s+import\s+(?:\w+|\((?:[^\)]|\n)*\)).*|import\s+.*)",
flags=re.MULTILINE,
)
Expand All @@ -20,8 +20,8 @@ def run_isort(code: str) -> str:
)


def is_import(s: str | None) -> bool:
return bool(IMPORT_RGX.match(s)) if s else False
def is_toplevel_import(s: str | None) -> bool:
return bool(TOPLEVEL_IMPORT_RGX.match(s)) if s else False


def nbisort(nb: nbformat.NotebookNode) -> nbformat.NotebookNode:
Expand All @@ -32,7 +32,9 @@ def nbisort(nb: nbformat.NotebookNode) -> nbformat.NotebookNode:
if cell["cell_type"] != "code":
continue
source = cell["source"]
code, imports = more_itertools.partition(is_import, IMPORT_RGX.split(source))
code, imports = more_itertools.partition(
is_toplevel_import, TOPLEVEL_IMPORT_RGX.split(source)
)
all_imports |= set(imports)
new_source = "".join(filter(bool, code))

Expand Down
6 changes: 3 additions & 3 deletions tests/test_nbisort.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from more_itertools import zip_equal

from nbisort import is_import, nbisort
from nbisort import is_toplevel_import, nbisort


@pytest.mark.parametrize(
Expand All @@ -23,8 +23,8 @@
("from module import (\n func1,\n func2,\n func3,\n)", True),
],
)
def test_is_import(code, expected):
assert is_import(code) == expected
def test_is_toplevel_import(code, expected):
assert is_toplevel_import(code) == expected


def cell(text: str) -> nbformat.NotebookNode:
Expand Down

0 comments on commit a4b6137

Please sign in to comment.