WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit cb92106

Browse files
authored
Merge pull request #54 from asmeurer/optional-nb
Make notebook support and dependencies optional
2 parents 5911465 + 10365a2 commit cb92106

File tree

6 files changed

+57
-29
lines changed

6 files changed

+57
-29
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ jobs:
3737
python-version: ${{ matrix.python-version }}
3838
allow-prereleases: true
3939

40-
- name: Install removestar
40+
- name: Install removestar (lite)
4141
run: |
4242
python -m pip install -e ".[dev]"
4343
44-
- name: Test removestar
44+
- name: Test removestar (lite)
45+
run: |
46+
pytest --cov=removestar . -vv
47+
48+
- name: Install removestar (nb)
49+
run: |
50+
python -m pip install -e ".[dev,nb]"
51+
52+
- name: Test removestar (nb)
4553
run: |
4654
pytest --cov=removestar . -vv
4755

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Install `removestar` globally to use it through CLI using `pypi` -
2222

2323
```bash
2424
pip install removestar
25+
pip install "removestar[nb]" # notebook support
2526
```
2627

2728
or `conda` -
@@ -39,7 +40,8 @@ or add `removestar` in `.pre-commit-config.yaml` -
3940
- id: removestar
4041
args: [-i] # See docs for all args (-i edits file in-place)
4142
additional_dependencies: # The libraries or packages your code imports
42-
- ... # Should be . if running inside a library (to install the library itself in the environment)
43+
- ... # Add . if running inside a library (to install the library itself in the environment)
44+
- ... # Add nbformat and nbconvert for notebook support
4345
```
4446
4547
## Usage
@@ -72,7 +74,7 @@ $ removestar -i file.py # Edits file.py in-place
7274
7375
$ removestar -i module/ # Modifies every Python file in module/ recursively
7476
75-
# notebooks
77+
# notebooks (make sure nbformat and nbconvert are installed)
7678
7779
$ removestar file.ipynb # Shows diff but does not edit file.ipynb
7880

pyproject.toml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,33 @@ description = "A tool to automatically replace 'import *' imports with explicit
1010
license = "MIT"
1111
requires-python = ">=3.7"
1212
authors = [
13-
{ name = "Aaron Meurer", email = "[email protected]" },
13+
{ name = "Aaron Meurer", email = "[email protected]" },
1414
]
1515
maintainers = [
16-
{ name = "Aaron Meurer", email = "[email protected]" },
17-
{ name = "Saransh Chopra", email = "[email protected]" }
16+
{ name = "Aaron Meurer", email = "[email protected]" },
17+
{ name = "Saransh Chopra", email = "[email protected]" }
1818
]
1919
classifiers = [
20-
"Development Status :: 5 - Production/Stable",
21-
"License :: OSI Approved :: MIT License",
22-
"Operating System :: OS Independent",
23-
"Programming Language :: Python",
24-
"Programming Language :: Python :: 3 :: Only",
25-
"Programming Language :: Python :: 3.8",
26-
"Programming Language :: Python :: 3.9",
27-
"Programming Language :: Python :: 3.10",
28-
"Programming Language :: Python :: 3.11",
20+
"Development Status :: 5 - Production/Stable",
21+
"License :: OSI Approved :: MIT License",
22+
"Operating System :: OS Independent",
23+
"Programming Language :: Python",
24+
"Programming Language :: Python :: 3 :: Only",
25+
"Programming Language :: Python :: 3.8",
26+
"Programming Language :: Python :: 3.9",
27+
"Programming Language :: Python :: 3.10",
28+
"Programming Language :: Python :: 3.11",
29+
"Programming Language :: Python :: 3.12",
2930
]
3031
dependencies = [
31-
"pyflakes",
32-
"nbformat",
33-
"nbconvert",
32+
"pyflakes",
3433
]
3534

3635
[project.optional-dependencies]
36+
nb = [
37+
"nbformat",
38+
"nbconvert",
39+
]
3740
dev = [
3841
"pytest>=6",
3942
"pytest-cov>=3",

removestar/__main__.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616

1717
import argparse
1818
import glob
19+
import importlib.util
1920
import io
2021
import os
2122
import sys
2223
import tempfile
2324

24-
import nbformat
25-
from nbconvert import PythonExporter
26-
2725
from . import __version__
2826
from .helper import get_diff_text
2927
from .output import get_colored_diff, red
30-
from .removestar import fix_code, replace_in_nb
28+
from .removestar import fix_code
3129

3230

3331
class RawDescriptionHelpArgumentDefaultsHelpFormatter(
@@ -92,6 +90,14 @@ def main(): # noqa: PLR0912, C901
9290
if args.max_line_length == 0:
9391
args.max_line_length = float("inf")
9492

93+
try:
94+
import nbformat
95+
from nbconvert import PythonExporter
96+
97+
from .removestar import replace_in_nb
98+
except ImportError:
99+
pass
100+
95101
exit_1 = False
96102
for file in _iter_paths(args.paths):
97103
_, filename = os.path.split(file)
@@ -144,7 +150,11 @@ def main(): # noqa: PLR0912, C901
144150
)
145151
)
146152
)
147-
elif file.endswith(".ipynb"):
153+
elif (
154+
file.endswith(".ipynb")
155+
and importlib.util.find_spec("nbconvert") is not None
156+
and importlib.util.find_spec("nbformat") is not None
157+
):
148158
tmp_file = tempfile.NamedTemporaryFile()
149159
tmp_path = tmp_file.name
150160

removestar/removestar.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import ast
22
import builtins
3+
import contextlib
34
import os
45
import re
56
import sys
67
from functools import lru_cache
78
from pathlib import Path
89

9-
from nbconvert import NotebookExporter
1010
from pyflakes.checker import _MAGIC_GLOBALS, Checker, ModuleScope
1111
from pyflakes.messages import ImportStarUsage, ImportStarUsed
1212

13+
with contextlib.suppress(ImportError):
14+
from nbconvert import NotebookExporter
15+
1316
from .output import green, yellow
1417

1518
# quit and exit are not included in old versions of pyflakes

tests/test_removestar_nb.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22
import tempfile
33

4-
import nbformat as nbf
5-
from nbconvert import NotebookExporter, PythonExporter
4+
import pytest
65

76
from removestar.removestar import fix_code, replace_in_nb
87

8+
nbf = pytest.importorskip("nbformat")
9+
nbc = pytest.importorskip("nbconvert")
10+
911
fixed_code = """#!/usr/bin/env python
1012
# coding: utf-8
1113
# # Notebook for testing.
@@ -35,7 +37,7 @@ def prepare_nb(output_path="_test.ipynb"):
3537
with open(output_path) as f:
3638
nb = nbf.reads(f.read(), nbf.NO_CONVERT)
3739

38-
exporter = PythonExporter()
40+
exporter = nbc.PythonExporter()
3941
code, _ = exporter.from_notebook_node(nb)
4042
tmp_file.write(code.encode("utf-8"))
4143

@@ -78,7 +80,7 @@ def test_replace_nb():
7880
with open("_test.ipynb", "w+") as f:
7981
f.writelines(fixed_code)
8082

81-
exporter = NotebookExporter()
83+
exporter = nbc.NotebookExporter()
8284
code, _ = exporter.from_filename("_test.ipynb")
8385

8486
assert code == fixed_code

0 commit comments

Comments
 (0)