Skip to content

Commit 4ee9b75

Browse files
committed
run ruff on all
1 parent bbeb732 commit 4ee9b75

File tree

6 files changed

+253
-288
lines changed

6 files changed

+253
-288
lines changed

nbdev/_modidx.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@
7575
'nbdev.doclinks.nbdev_export': ('api/doclinks.html#nbdev_export', 'nbdev/doclinks.py'),
7676
'nbdev.doclinks.nbglob': ('api/doclinks.html#nbglob', 'nbdev/doclinks.py'),
7777
'nbdev.doclinks.nbglob_cli': ('api/doclinks.html#nbglob_cli', 'nbdev/doclinks.py'),
78-
'nbdev.doclinks.patch_name': ('api/doclinks.html#patch_name', 'nbdev/doclinks.py')},
78+
'nbdev.doclinks.patch_name': ('api/doclinks.html#patch_name', 'nbdev/doclinks.py'),
79+
'nbdev.doclinks.ruff_fix': ('api/doclinks.html#ruff_fix', 'nbdev/doclinks.py'),
80+
'nbdev.doclinks.ruff_format': ('api/doclinks.html#ruff_format', 'nbdev/doclinks.py')},
7981
'nbdev.export': { 'nbdev.export.ExportModuleProc': ('api/export.html#exportmoduleproc', 'nbdev/export.py'),
8082
'nbdev.export.ExportModuleProc.__call__': ('api/export.html#exportmoduleproc.__call__', 'nbdev/export.py'),
8183
'nbdev.export.ExportModuleProc._default_exp_': ( 'api/export.html#exportmoduleproc._default_exp_',
@@ -86,8 +88,6 @@
8688
'nbdev.export.black_format': ('api/export.html#black_format', 'nbdev/export.py'),
8789
'nbdev.export.nb_export': ('api/export.html#nb_export', 'nbdev/export.py'),
8890
'nbdev.export.optional_procs': ('api/export.html#optional_procs', 'nbdev/export.py'),
89-
'nbdev.export.ruff_fix': ('api/export.html#ruff_fix', 'nbdev/export.py'),
90-
'nbdev.export.ruff_format': ('api/export.html#ruff_format', 'nbdev/export.py'),
9191
'nbdev.export.scrub_magics': ('api/export.html#scrub_magics', 'nbdev/export.py')},
9292
'nbdev.extract_attachments': {},
9393
'nbdev.frontmatter': { 'nbdev.frontmatter.FrontmatterProc': ('api/frontmatter.html#frontmatterproc', 'nbdev/frontmatter.py'),

nbdev/doclinks.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/05_doclinks.ipynb.
44

55
# %% auto 0
6-
__all__ = ['typs', 'bset', 'patch_name', 'nbglob', 'nbglob_cli', 'nbdev_export', 'create_index', 'NbdevLookup']
6+
__all__ = ['typs', 'bset', 'patch_name', 'ruff_format', 'ruff_fix', 'nbglob', 'nbglob_cli', 'nbdev_export', 'create_index',
7+
'NbdevLookup']
78

89
# %% ../nbs/api/05_doclinks.ipynb
910
from .config import *
@@ -117,6 +118,32 @@ def _build_modidx(dest=None, nbs_path=None, skip_exists=False):
117118
except ValueError: pass
118119
idxfile.write_text("# Autogenerated by nbdev\n\nd = "+pformat(res, width=140, indent=2, compact=True)+'\n')
119120

121+
# %% ../nbs/api/05_doclinks.ipynb
122+
def ruff_format():
123+
"Format code with `ruff format` on the entire library"
124+
import subprocess
125+
try:
126+
subprocess.run(
127+
['ruff', 'format'],
128+
capture_output=True,
129+
check=False
130+
)
131+
except FileNotFoundError:
132+
raise ImportError("You must install ruff: `pip install ruff` if you wish to use ruff formatting with nbdev")
133+
134+
# %% ../nbs/api/05_doclinks.ipynb
135+
def ruff_fix():
136+
"Lint and auto-fix code with `ruff check --fix` on the entire library"
137+
import subprocess
138+
try:
139+
subprocess.run(
140+
['ruff', 'check', '--fix'],
141+
capture_output=True,
142+
check=False
143+
)
144+
except FileNotFoundError:
145+
raise ImportError("You must install ruff: `pip install ruff` if you wish to use ruff formatting with nbdev")
146+
120147
# %% ../nbs/api/05_doclinks.ipynb
121148
@delegates(globtastic)
122149
def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', skip_file_re='^[_.]', key='nbs_path', as_path=False, **kwargs):
@@ -146,7 +173,7 @@ def nbglob_cli(
146173
@delegates(nbglob_cli)
147174
def nbdev_export(
148175
path:str=None, # Path or filename
149-
procs:Param("tokens naming the export processors to use.", nargs="*", choices=optional_procs())=["black_format", "ruff_format", "ruff_fix"],
176+
procs:Param("tokens naming the export processors to use.", nargs="*", choices=optional_procs())="black_format",
150177
**kwargs):
151178
"Export notebooks in `path` to Python modules"
152179
if os.environ.get('IN_TEST',0): return
@@ -156,9 +183,13 @@ def nbdev_export(
156183
procs = [getattr(nbdev.export, p) for p in L(procs)]
157184
files = nbglob(path=path, as_path=True, **kwargs).sorted('name')
158185
for f in files: nb_export(f, procs=procs)
159-
add_init(get_config().lib_path)
186+
cfg = get_config()
187+
add_init(cfg.lib_path)
160188
_build_modidx()
161189

190+
if cfg.ruff_formatting: ruff_format()
191+
if cfg.ruff_fixing: ruff_fix()
192+
162193
# %% ../nbs/api/05_doclinks.ipynb
163194
typs = 'module','class','method','function'
164195
bset = set(dir(builtins))

nbdev/export.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/04_export.ipynb.
44

55
# %% auto 0
6-
__all__ = ['ExportModuleProc', 'black_format', 'ruff_format', 'ruff_fix', 'scrub_magics', 'optional_procs', 'nb_export']
6+
__all__ = ['ExportModuleProc', 'black_format', 'scrub_magics', 'optional_procs', 'nb_export']
77

88
# %% ../nbs/api/04_export.ipynb
99
from .config import *
@@ -47,64 +47,6 @@ def black_format(cell, # Cell to format
4747
try: cell.source = _format_str(cell.source).strip()
4848
except: pass
4949

50-
# %% ../nbs/api/04_export.ipynb
51-
def ruff_format(cell, # Cell to format
52-
force=False): # Turn ruff formatting on regardless of settings.ini
53-
"Processor to format code with `ruff format`"
54-
try: cfg = get_config()
55-
except FileNotFoundError: return
56-
if (not cfg.ruff_formatting and not force) or cell.cell_type != 'code': return
57-
58-
try: import subprocess
59-
except: raise ImportError("subprocess is required")
60-
61-
try:
62-
subprocess.run(['ruff', '--version'], capture_output=True, check=True)
63-
except (subprocess.CalledProcessError, FileNotFoundError):
64-
raise ImportError("You must install ruff: `pip install ruff` if you wish to use ruff formatting with nbdev")
65-
else:
66-
try:
67-
result = subprocess.run(
68-
['ruff', 'format', '-'],
69-
input=cell.source,
70-
capture_output=True,
71-
text=True,
72-
check=False
73-
)
74-
if result.returncode == 0:
75-
cell.source = result.stdout.strip()
76-
except:
77-
pass
78-
79-
# %% ../nbs/api/04_export.ipynb
80-
def ruff_fix(cell, # Cell to lint and fix
81-
force=False): # Turn ruff fixing on regardless of settings.ini
82-
"Processor to lint and auto-fix code with `ruff check --fix`"
83-
try: cfg = get_config()
84-
except FileNotFoundError: return
85-
if (not cfg.ruff_fixing and not force) or cell.cell_type != 'code': return
86-
87-
try: import subprocess
88-
except: raise ImportError("subprocess is required")
89-
90-
try:
91-
subprocess.run(['ruff', '--version'], capture_output=True, check=True)
92-
except (subprocess.CalledProcessError, FileNotFoundError):
93-
raise ImportError("You must install ruff: `pip install ruff` if you wish to use ruff formatting with nbdev")
94-
else:
95-
try:
96-
result = subprocess.run(
97-
['ruff', 'check', '--fix', '-'],
98-
input=cell.source,
99-
capture_output=True,
100-
text=True,
101-
check=False
102-
)
103-
if result.returncode == 0 or result.returncode == 1: # 1 means fixes were applied
104-
cell.source = result.stdout.strip() if result.stdout else cell.source
105-
except:
106-
pass
107-
10850
# %% ../nbs/api/04_export.ipynb
10951
# includes the newline, because calling .strip() would affect all cells.
11052
_magics_pattern = re.compile(r'^\s*(%%|%).*\n?', re.MULTILINE)

nbs/api/04_export.ipynb

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,6 @@
107107
"### Optional export processors"
108108
]
109109
},
110-
{
111-
"cell_type": "code",
112-
"execution_count": null,
113-
"metadata": {},
114-
"outputs": [
115-
{
116-
"name": "stdout",
117-
"output_type": "stream",
118-
"text": [
119-
"False False False\n"
120-
]
121-
}
122-
],
123-
"source": [
124-
"print(\n",
125-
" get_config().black_formatting,\n",
126-
" get_config().ruff_formatting,\n",
127-
" get_config().ruff_fixing,\n",
128-
")"
129-
]
130-
},
131110
{
132111
"cell_type": "code",
133112
"execution_count": null,
@@ -160,100 +139,6 @@
160139
"test_eq(_cell.source, 'import ast\\n\\nj = [1, 2, 3]\\nfor i in j:\\n str(1)')"
161140
]
162141
},
163-
{
164-
"cell_type": "code",
165-
"execution_count": null,
166-
"metadata": {},
167-
"outputs": [],
168-
"source": [
169-
"#| export\n",
170-
"def ruff_format_cell(cell, # Cell to format\n",
171-
" force=False): # Turn ruff formatting on regardless of settings.ini\n",
172-
" \"Processor to format code with `ruff format`\"\n",
173-
" try: cfg = get_config()\n",
174-
" except FileNotFoundError: return\n",
175-
" if (not cfg.ruff_formatting and not force) or cell.cell_type != 'code': return\n",
176-
" \n",
177-
" try: import subprocess\n",
178-
" except: raise ImportError(\"subprocess is required\")\n",
179-
" \n",
180-
" try:\n",
181-
" subprocess.run(['ruff', '--version'], capture_output=True, check=True)\n",
182-
" except (subprocess.CalledProcessError, FileNotFoundError):\n",
183-
" raise ImportError(\"You must install ruff: `pip install ruff` if you wish to use ruff formatting with nbdev\")\n",
184-
" else:\n",
185-
" try:\n",
186-
" result = subprocess.run(\n",
187-
" ['ruff', 'format', '-'],\n",
188-
" input=cell.source,\n",
189-
" capture_output=True,\n",
190-
" text=True,\n",
191-
" check=False\n",
192-
" )\n",
193-
" if result.returncode == 0:\n",
194-
" cell.source = result.stdout.strip()\n",
195-
" except:\n",
196-
" pass"
197-
]
198-
},
199-
{
200-
"cell_type": "code",
201-
"execution_count": null,
202-
"metadata": {},
203-
"outputs": [],
204-
"source": [
205-
"_cell = read_nb('../../tests/export_procs.ipynb')['cells'][0]\n",
206-
"ruff_format_cell(_cell, force=True)\n",
207-
"test_eq(_cell.source, 'import ast\\n\\nj = [1, 2, 3]\\nfor i in j:\\n str(1)')"
208-
]
209-
},
210-
{
211-
"cell_type": "code",
212-
"execution_count": null,
213-
"metadata": {},
214-
"outputs": [],
215-
"source": [
216-
"#| export\n",
217-
"def ruff_fix_cell(cell, # Cell to lint and fix\n",
218-
" force=False): # Turn ruff fixing on regardless of settings.ini\n",
219-
" \"Processor to lint and auto-fix code with `ruff check --fix`\"\n",
220-
" try: cfg = get_config()\n",
221-
" except FileNotFoundError: return\n",
222-
" if (not cfg.ruff_fixing and not force) or cell.cell_type != 'code': return\n",
223-
" \n",
224-
" try: import subprocess\n",
225-
" except: raise ImportError(\"subprocess is required\")\n",
226-
"\n",
227-
" try:\n",
228-
" subprocess.run(['ruff', '--version'], capture_output=True, check=True)\n",
229-
" except (subprocess.CalledProcessError, FileNotFoundError):\n",
230-
" raise ImportError(\"You must install ruff: `pip install ruff` if you wish to use ruff formatting with nbdev\")\n",
231-
" else:\n",
232-
" try:\n",
233-
" result = subprocess.run(\n",
234-
" ['ruff', 'check', '--fix', '-'],\n",
235-
" input=cell.source,\n",
236-
" capture_output=True,\n",
237-
" text=True,\n",
238-
" check=False\n",
239-
" )\n",
240-
" if result.returncode == 0 or result.returncode == 1: # 1 means fixes were applied\n",
241-
" cell.source = result.stdout.strip() if result.stdout else cell.source\n",
242-
" except:\n",
243-
" pass"
244-
]
245-
},
246-
{
247-
"cell_type": "code",
248-
"execution_count": null,
249-
"metadata": {},
250-
"outputs": [],
251-
"source": [
252-
"_cell = read_nb('../../tests/export_procs.ipynb')['cells'][0]\n",
253-
"ruff_fix_cell(_cell, force=True)\n",
254-
"test_eq(_cell.source, 'j = [1,\\n 2,\\n 3\\n]\\nfor i in j:\\n str(1)')"
255-
]
256-
},
257142
{
258143
"cell_type": "code",
259144
"execution_count": null,
@@ -336,7 +221,7 @@
336221
"outputs": [],
337222
"source": [
338223
"# every optional processor should be explicitly listed here\n",
339-
"test_eq(optional_procs(), ['black_format', 'ruff_format_cell', 'ruff_fix_cell', 'scrub_magics'])"
224+
"test_eq(optional_procs(), ['black_format', 'scrub_magics'])"
340225
]
341226
},
342227
{

0 commit comments

Comments
 (0)