Skip to content

Commit

Permalink
Update ruff and typings (#376)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
blink1073 authored Nov 28, 2023
1 parent 6921356 commit 257b483
Show file tree
Hide file tree
Showing 32 changed files with 204 additions and 174 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repos:
[mdformat-gfm, mdformat-frontmatter, mdformat-footnote]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.3"
rev: "v3.1.0"
hooks:
- id: prettier
types_or: [yaml, html, json]
Expand All @@ -45,7 +45,7 @@ repos:
additional_dependencies: [black==23.7.0]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.6.1"
rev: "v1.7.1"
hooks:
- id: mypy
files: jupyter_core
Expand All @@ -67,7 +67,7 @@ repos:
- id: rst-inline-touching-normal

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
rev: v0.1.6
hooks:
- id: ruff
types_or: [python, jupyter]
Expand All @@ -76,7 +76,7 @@ repos:
types_or: [python, jupyter]

- repo: https://github.com/scientific-python/cookie
rev: "2023.10.27"
rev: "2023.11.17"
hooks:
- id: sp-repo-review
additional_dependencies: ["repo-review[cli]"]
15 changes: 7 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3
#
# jupyter_core documentation build configuration file, created by
# sphinx-quickstart on Wed Jun 24 11:51:36 2015.
#
Expand All @@ -11,9 +9,10 @@
#
# All configuration values have a default; values that are commented out
# serve to show the default.
from __future__ import annotations

import os
import shutil
from pathlib import Path

from jupyter_core.version import __version__, version_info

Expand All @@ -39,7 +38,7 @@
]

try:
import enchant # type:ignore # noqa
import enchant # noqa: F401

extensions += ["sphinxcontrib.spelling"]
except ImportError:
Expand All @@ -63,7 +62,7 @@

# General information about the project.
project = "jupyter_core"
copyright = "2015, Jupyter Development Team" # noqa
copyright = "2015, Jupyter Development Team"
author = "Jupyter Development Team"

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


def setup(app):
here = os.path.dirname(os.path.abspath(__file__))
shutil.copy(os.path.join(here, "..", "CHANGELOG.md"), "changelog.md")
def setup(_):
here = Path(__file__).parent.resolve()
shutil.copy(Path(here, "..", "CHANGELOG.md"), "changelog.md")
2 changes: 2 additions & 0 deletions jupyter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Launch the root jupyter command"""
from __future__ import annotations

if __name__ == "__main__":
from jupyter_core.command import main

Expand Down
1 change: 0 additions & 1 deletion jupyter_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .version import __version__, version_info # noqa
2 changes: 2 additions & 0 deletions jupyter_core/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Launch the root jupyter command"""
from __future__ import annotations

from .command import main

main()
17 changes: 9 additions & 8 deletions jupyter_core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
import typing as t
from copy import deepcopy
from pathlib import Path
from shutil import which

from traitlets import Bool, List, Unicode, observe
Expand Down Expand Up @@ -62,7 +63,7 @@
base_flags.update(_jupyter_flags)


class NoStart(Exception): # noqa
class NoStart(Exception):
"""Exception to raise when an application shouldn't start"""


Expand Down Expand Up @@ -135,9 +136,9 @@ def write_default_config(self) -> None:
if self.config_file:
config_file = self.config_file
else:
config_file = os.path.join(self.config_dir, self.config_file_name + ".py")
config_file = str(Path(self.config_dir, self.config_file_name + ".py"))

if os.path.exists(config_file) and not self.answer_yes:
if Path(config_file).exists() and not self.answer_yes:
answer = ""

def ask() -> str:
Expand All @@ -157,15 +158,15 @@ def ask() -> str:

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

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

# No IPython dir, nothing to migrate
if not os.path.exists(get_ipython_dir()):
if not Path(get_ipython_dir()).exists():
return

migrate()
Expand Down Expand Up @@ -262,7 +263,7 @@ def initialize(self, argv: t.Any = None) -> None:
def start(self) -> None:
"""Start the whole thing"""
if self.subcommand:
os.execv(self.subcommand, [self.subcommand] + self.argv[1:]) # noqa
os.execv(self.subcommand, [self.subcommand] + self.argv[1:]) # noqa: S606
raise NoStart()

if self.subapp:
Expand Down
23 changes: 11 additions & 12 deletions jupyter_core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import site
import sys
import sysconfig
from pathlib import Path
from shutil import which
from subprocess import Popen
from typing import Any
Expand All @@ -37,7 +38,6 @@ def epilog(self) -> str | None:
@epilog.setter
def epilog(self, x: Any) -> None:
"""Ignore epilog set in Parser.__init__"""
pass

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

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


def _jupyter_abspath(subcommand: str) -> str:
Expand Down Expand Up @@ -177,13 +177,13 @@ def _path_with_self() -> list[str]:
path_list.append(bindir)

scripts = [sys.argv[0]]
if os.path.islink(scripts[0]):
if Path(scripts[0]).is_symlink():
# include realpath, if `jupyter` is a symlink
scripts.append(os.path.realpath(scripts[0]))

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


def main() -> None: # noqa
def main() -> None:
"""The command entry point."""
parser = jupyter_parser()
argv = sys.argv
Expand Down
Loading

0 comments on commit 257b483

Please sign in to comment.