Skip to content

Commit 257b483

Browse files
authored
Update ruff and typings (#376)
* Update ruff and typings * satisfy mypy * fix import * ensure the event loop is closed in the test * ensure the event loop is closed in the test * ensure the event loop is closed in the test
1 parent 6921356 commit 257b483

32 files changed

+204
-174
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ repos:
3333
[mdformat-gfm, mdformat-frontmatter, mdformat-footnote]
3434

3535
- repo: https://github.com/pre-commit/mirrors-prettier
36-
rev: "v3.0.3"
36+
rev: "v3.1.0"
3737
hooks:
3838
- id: prettier
3939
types_or: [yaml, html, json]
@@ -45,7 +45,7 @@ repos:
4545
additional_dependencies: [black==23.7.0]
4646

4747
- repo: https://github.com/pre-commit/mirrors-mypy
48-
rev: "v1.6.1"
48+
rev: "v1.7.1"
4949
hooks:
5050
- id: mypy
5151
files: jupyter_core
@@ -67,7 +67,7 @@ repos:
6767
- id: rst-inline-touching-normal
6868

6969
- repo: https://github.com/astral-sh/ruff-pre-commit
70-
rev: v0.1.5
70+
rev: v0.1.6
7171
hooks:
7272
- id: ruff
7373
types_or: [python, jupyter]
@@ -76,7 +76,7 @@ repos:
7676
types_or: [python, jupyter]
7777

7878
- repo: https://github.com/scientific-python/cookie
79-
rev: "2023.10.27"
79+
rev: "2023.11.17"
8080
hooks:
8181
- id: sp-repo-review
8282
additional_dependencies: ["repo-review[cli]"]

docs/conf.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python3
2-
#
31
# jupyter_core documentation build configuration file, created by
42
# sphinx-quickstart on Wed Jun 24 11:51:36 2015.
53
#
@@ -11,9 +9,10 @@
119
#
1210
# All configuration values have a default; values that are commented out
1311
# serve to show the default.
12+
from __future__ import annotations
1413

15-
import os
1614
import shutil
15+
from pathlib import Path
1716

1817
from jupyter_core.version import __version__, version_info
1918

@@ -39,7 +38,7 @@
3938
]
4039

4140
try:
42-
import enchant # type:ignore # noqa
41+
import enchant # noqa: F401
4342

4443
extensions += ["sphinxcontrib.spelling"]
4544
except ImportError:
@@ -63,7 +62,7 @@
6362

6463
# General information about the project.
6564
project = "jupyter_core"
66-
copyright = "2015, Jupyter Development Team" # noqa
65+
copyright = "2015, Jupyter Development Team"
6766
author = "Jupyter Development Team"
6867

6968
# The version info for the project you're documenting, acts as replacement for
@@ -300,6 +299,6 @@
300299
intersphinx_mapping = {"https://docs.python.org/3/": None}
301300

302301

303-
def setup(app):
304-
here = os.path.dirname(os.path.abspath(__file__))
305-
shutil.copy(os.path.join(here, "..", "CHANGELOG.md"), "changelog.md")
302+
def setup(_):
303+
here = Path(__file__).parent.resolve()
304+
shutil.copy(Path(here, "..", "CHANGELOG.md"), "changelog.md")

jupyter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Launch the root jupyter command"""
2+
from __future__ import annotations
3+
24
if __name__ == "__main__":
35
from jupyter_core.command import main
46

jupyter_core/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
from .version import __version__, version_info # noqa

jupyter_core/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Launch the root jupyter command"""
2+
from __future__ import annotations
3+
24
from .command import main
35

46
main()

jupyter_core/application.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sys
1414
import typing as t
1515
from copy import deepcopy
16+
from pathlib import Path
1617
from shutil import which
1718

1819
from traitlets import Bool, List, Unicode, observe
@@ -62,7 +63,7 @@
6263
base_flags.update(_jupyter_flags)
6364

6465

65-
class NoStart(Exception): # noqa
66+
class NoStart(Exception):
6667
"""Exception to raise when an application shouldn't start"""
6768

6869

@@ -135,9 +136,9 @@ def write_default_config(self) -> None:
135136
if self.config_file:
136137
config_file = self.config_file
137138
else:
138-
config_file = os.path.join(self.config_dir, self.config_file_name + ".py")
139+
config_file = str(Path(self.config_dir, self.config_file_name + ".py"))
139140

140-
if os.path.exists(config_file) and not self.answer_yes:
141+
if Path(config_file).exists() and not self.answer_yes:
141142
answer = ""
142143

143144
def ask() -> str:
@@ -157,15 +158,15 @@ def ask() -> str:
157158

158159
config_text = self.generate_config_file()
159160
print("Writing default config to: %s" % config_file)
160-
ensure_dir_exists(os.path.abspath(os.path.dirname(config_file)), 0o700)
161-
with open(config_file, mode="w", encoding="utf-8") as f:
161+
ensure_dir_exists(Path(config_file).parent.resolve(), 0o700)
162+
with Path.open(Path(config_file), mode="w", encoding="utf-8") as f:
162163
f.write(config_text)
163164

164165
def migrate_config(self) -> None:
165166
"""Migrate config/data from IPython 3"""
166167
try: # let's see if we can open the marker file
167168
# for reading and updating (writing)
168-
f_marker = open(os.path.join(self.config_dir, "migrated"), "r+") # noqa
169+
f_marker = Path.open(Path(self.config_dir, "migrated"), "r+")
169170
except PermissionError: # not readable and/or writable
170171
return # so let's give up migration in such an environment
171172
except FileNotFoundError: # cannot find the marker file
@@ -178,7 +179,7 @@ def migrate_config(self) -> None:
178179
from .migrate import get_ipython_dir, migrate
179180

180181
# No IPython dir, nothing to migrate
181-
if not os.path.exists(get_ipython_dir()):
182+
if not Path(get_ipython_dir()).exists():
182183
return
183184

184185
migrate()
@@ -262,7 +263,7 @@ def initialize(self, argv: t.Any = None) -> None:
262263
def start(self) -> None:
263264
"""Start the whole thing"""
264265
if self.subcommand:
265-
os.execv(self.subcommand, [self.subcommand] + self.argv[1:]) # noqa
266+
os.execv(self.subcommand, [self.subcommand] + self.argv[1:]) # noqa: S606
266267
raise NoStart()
267268

268269
if self.subapp:

jupyter_core/command.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import site
1616
import sys
1717
import sysconfig
18+
from pathlib import Path
1819
from shutil import which
1920
from subprocess import Popen
2021
from typing import Any
@@ -37,7 +38,6 @@ def epilog(self) -> str | None:
3738
@epilog.setter
3839
def epilog(self, x: Any) -> None:
3940
"""Ignore epilog set in Parser.__init__"""
40-
pass
4141

4242
def argcomplete(self) -> None:
4343
"""Trigger auto-completion, if enabled"""
@@ -63,7 +63,7 @@ def jupyter_parser() -> JupyterParser:
6363
"subcommand", type=str, nargs="?", help="the subcommand to launch"
6464
)
6565
# For argcomplete, supply all known subcommands
66-
subcommand_action.completer = lambda *args, **kwargs: list_subcommands() # type: ignore[attr-defined]
66+
subcommand_action.completer = lambda *args, **kwargs: list_subcommands() # type: ignore[attr-defined] # noqa: ARG005
6767

6868
group.add_argument("--config-dir", action="store_true", help="show Jupyter config dir")
6969
group.add_argument("--data-dir", action="store_true", help="show Jupyter data dir")
@@ -98,7 +98,7 @@ def list_subcommands() -> list[str]:
9898
if name.startswith("jupyter-"):
9999
if sys.platform.startswith("win"):
100100
# remove file-extension on Windows
101-
name = os.path.splitext(name)[0] # noqa
101+
name = os.path.splitext(name)[0] # noqa: PTH122, PLW2901
102102
subcommand_tuples.add(tuple(name.split("-")[1:]))
103103
# build a set of subcommand strings, excluding subcommands whose parents are defined
104104
subcommands = set()
@@ -120,7 +120,7 @@ def _execvp(cmd: str, argv: list[str]) -> None:
120120
cmd_path = which(cmd)
121121
if cmd_path is None:
122122
raise OSError("%r not found" % cmd, errno.ENOENT)
123-
p = Popen([cmd_path] + argv[1:]) # noqa
123+
p = Popen([cmd_path] + argv[1:]) # noqa: S603
124124
# Don't raise KeyboardInterrupt in the parent process.
125125
# Set this after spawning, to avoid subprocess inheriting handler.
126126
import signal
@@ -129,7 +129,7 @@ def _execvp(cmd: str, argv: list[str]) -> None:
129129
p.wait()
130130
sys.exit(p.returncode)
131131
else:
132-
os.execvp(cmd, argv) # noqa
132+
os.execvp(cmd, argv) # noqa: S606
133133

134134

135135
def _jupyter_abspath(subcommand: str) -> str:
@@ -177,13 +177,13 @@ def _path_with_self() -> list[str]:
177177
path_list.append(bindir)
178178

179179
scripts = [sys.argv[0]]
180-
if os.path.islink(scripts[0]):
180+
if Path(scripts[0]).is_symlink():
181181
# include realpath, if `jupyter` is a symlink
182182
scripts.append(os.path.realpath(scripts[0]))
183183

184184
for script in scripts:
185-
bindir = os.path.dirname(script)
186-
if os.path.isdir(bindir) and os.access(script, os.X_OK): # only if it's a script
185+
bindir = str(Path(script).parent)
186+
if Path(bindir).is_dir() and os.access(script, os.X_OK): # only if it's a script
187187
# ensure executable's dir is on PATH
188188
# avoids missing subcommands when jupyter is run via absolute path
189189
path_list.insert(0, bindir)
@@ -211,9 +211,8 @@ def _evaluate_argcomplete(parser: JupyterParser) -> list[str]:
211211
# increment word from which to start handling arguments
212212
increment_argcomplete_index()
213213
return cwords
214-
else:
215-
# Otherwise no subcommand, directly autocomplete and exit
216-
parser.argcomplete()
214+
# Otherwise no subcommand, directly autocomplete and exit
215+
parser.argcomplete()
217216
except ImportError:
218217
# traitlets >= 5.8 not available, just try to complete this without
219218
# worrying about subcommands
@@ -222,7 +221,7 @@ def _evaluate_argcomplete(parser: JupyterParser) -> list[str]:
222221
raise AssertionError(msg)
223222

224223

225-
def main() -> None: # noqa
224+
def main() -> None:
226225
"""The command entry point."""
227226
parser = jupyter_parser()
228227
argv = sys.argv

0 commit comments

Comments
 (0)