Skip to content

Commit

Permalink
Adopt sp-repo-review (#360)
Browse files Browse the repository at this point in the history
* adopt sp-repo-review

* fix typing

* fix typing

* fix typing

* fix typing

* fix typing

* fix typing

* test troubleshoot

* restore additional mdformat deps
  • Loading branch information
blink1073 authored Sep 14, 2023
1 parent b9990b3 commit cde7c08
Show file tree
Hide file tree
Showing 34 changed files with 179 additions and 143 deletions.
17 changes: 0 additions & 17 deletions .flake8

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- name: Run Linters
run: |
- name: Run Linters
run: |
hatch run typing:test
hatch run lint:style
pipx run interrogate -v .
Expand Down
39 changes: 36 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ci:
autoupdate_schedule: monthly
autoupdate_commit_msg: "chore: update pre-commit hooks"

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down Expand Up @@ -28,15 +29,47 @@ repos:
rev: 0.7.17
hooks:
- id: mdformat
additional_dependencies:
[mdformat-gfm, mdformat-frontmatter, mdformat-footnote]

- repo: https://github.com/psf/black
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.2"
hooks:
- id: prettier
types_or: [yaml, html, json]

- repo: https://github.com/adamchainz/blacken-docs
rev: "1.16.0"
hooks:
- id: blacken-docs
additional_dependencies: [black==23.7.0]

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.7.0
hooks:
- id: black

- repo: https://github.com/codespell-project/codespell
rev: "v2.2.5"
hooks:
- id: codespell
args: ["-L", "sur,nd"]

- repo: https://github.com/pre-commit/pygrep-hooks
rev: "v1.10.0"
hooks:
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.287
hooks:
- id: ruff
args: ["--fix"]
exclude: script
args: ["--fix", "--show-fixes"]

- repo: https://github.com/scientific-python/cookie
rev: "2023.08.23"
hooks:
- id: sp-repo-review
additional_dependencies: ["repo-review[cli]"]
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ python:
build:
os: ubuntu-22.04
tools:
python: "3.11"
python: "3.11"
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ closed.
[on GitHub](https://github.com/jupyter/jupyter_core/releases/tag/4.6.2)

- Add ability to allow insecure writes with
JUPYTER_ALLOW_INSECURE_WRITES environement variable
JUPYTER_ALLOW_INSECURE_WRITES environment variable
([#182](https://github.com/jupyter/jupyter_core/pull/182)).
- Docs typo and build fixes
- Added python 3.7 and 3.8 builds to testing
Expand Down
79 changes: 39 additions & 40 deletions jupyter_core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import logging
import os
Expand All @@ -29,23 +30,25 @@
)
from .utils import ensure_dir_exists

# mypy: disable-error-code="no-untyped-call"

# aliases and flags

base_aliases: dict = {}
base_aliases: dict[str, t.Any] = {}
if isinstance(Application.aliases, dict):
# traitlets 5
base_aliases.update(Application.aliases)
base_aliases.update(Application.aliases) # type:ignore[arg-type]
_jupyter_aliases = {
"log-level": "Application.log_level",
"config": "JupyterApp.config_file",
}
base_aliases.update(_jupyter_aliases)

base_flags: dict = {}
base_flags: dict[str, t.Any] = {}
if isinstance(Application.flags, dict):
# traitlets 5
base_flags.update(Application.flags)
_jupyter_flags: dict = {
base_flags.update(Application.flags) # type:ignore[arg-type]
_jupyter_flags: dict[str, t.Any] = {
"debug": (
{"Application": {"log_level": logging.DEBUG}},
"set log level to logging.DEBUG (maximize logging output)",
Expand All @@ -69,71 +72,67 @@ class JupyterApp(Application):
name = "jupyter" # override in subclasses
description = "A Jupyter Application"

aliases = base_aliases
flags = base_flags
aliases = base_aliases # type:ignore[assignment]
flags = base_flags # type:ignore[assignment]

def _log_level_default(self):
def _log_level_default(self) -> int:
return logging.INFO

jupyter_path: t.Union[t.List[str], List] = List(Unicode())
jupyter_path: list[str] | List = List(Unicode())

def _jupyter_path_default(self):
def _jupyter_path_default(self) -> list[str]:
return jupyter_path()

config_dir: t.Union[str, Unicode] = Unicode()
config_dir: str | Unicode = Unicode()

def _config_dir_default(self):
def _config_dir_default(self) -> str:
return jupyter_config_dir()

@property
def config_file_paths(self):
def config_file_paths(self) -> list[str]:
path = jupyter_config_path()
if self.config_dir not in path:
# Insert config dir as first item.
path.insert(0, self.config_dir)
return path

data_dir: t.Union[str, Unicode] = Unicode()
data_dir: str | Unicode = Unicode()

def _data_dir_default(self):
def _data_dir_default(self) -> str:
d = jupyter_data_dir()
ensure_dir_exists(d, mode=0o700)
return d

runtime_dir: t.Union[str, Unicode] = Unicode()
runtime_dir: str | Unicode = Unicode()

def _runtime_dir_default(self):
def _runtime_dir_default(self) -> str:
rd = jupyter_runtime_dir()
ensure_dir_exists(rd, mode=0o700)
return rd

@observe("runtime_dir")
def _runtime_dir_changed(self, change):
@observe("runtime_dir") # type:ignore[misc]
def _runtime_dir_changed(self, change: t.Any) -> None:
ensure_dir_exists(change["new"], mode=0o700)

generate_config: t.Union[bool, Bool] = Bool(
generate_config: bool | Bool = Bool(
False, config=True, help="""Generate default config file."""
)

config_file_name: t.Union[str, Unicode] = Unicode(
config=True, help="Specify a config file to load."
)
config_file_name: str | Unicode = Unicode(config=True, help="Specify a config file to load.")

def _config_file_name_default(self):
def _config_file_name_default(self) -> str:
if not self.name:
return ""
return self.name.replace("-", "_") + "_config"

config_file: t.Union[str, Unicode] = Unicode(
config_file: str | Unicode = Unicode(
config=True,
help="""Full path of a config file.""",
)

answer_yes: t.Union[bool, Bool] = Bool(
False, config=True, help="""Answer yes to any prompts."""
)
answer_yes: bool | Bool = Bool(False, config=True, help="""Answer yes to any prompts.""")

def write_default_config(self):
def write_default_config(self) -> None:
"""Write our default config to a .py config file"""
if self.config_file:
config_file = self.config_file
Expand All @@ -143,7 +142,7 @@ def write_default_config(self):
if os.path.exists(config_file) and not self.answer_yes:
answer = ""

def ask():
def ask() -> str:
prompt = "Overwrite %s with default config? [y/N]" % config_file
try:
return input(prompt).lower() or "n"
Expand All @@ -166,7 +165,7 @@ def ask():
with open(config_file, mode="w", encoding="utf-8") as f:
f.write(config_text)

def migrate_config(self):
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)
Expand All @@ -188,7 +187,7 @@ def migrate_config(self):

migrate()

def load_config_file(self, suppress_errors=True):
def load_config_file(self, suppress_errors: bool = True) -> None: # type:ignore[override]
"""Load the config file.
By default, errors in loading config are handled, and a warning
Expand All @@ -209,7 +208,7 @@ def load_config_file(self, suppress_errors=True):
if self.config_file:
path, config_file_name = os.path.split(self.config_file)
else:
path = self.config_file_paths
path = self.config_file_paths # type:ignore[assignment]
config_file_name = self.config_file_name

if not config_file_name or (config_file_name == base_config):
Expand All @@ -227,12 +226,12 @@ def load_config_file(self, suppress_errors=True):
self.log.warning("Error loading config file: %s", config_file_name, exc_info=True)

# subcommand-related
def _find_subcommand(self, name):
def _find_subcommand(self, name: str) -> str:
name = f"{self.name}-{name}"
return which(name)
return which(name) or ""

@property
def _dispatching(self):
def _dispatching(self) -> bool:
"""Return whether we are dispatching to another command
or running ourselves.
Expand All @@ -242,7 +241,7 @@ def _dispatching(self):
subcommand = Unicode()

@catch_config_error
def initialize(self, argv=None):
def initialize(self, argv: t.Any = None) -> None:
"""Initialize the application."""
# don't hook up crash handler before parsing command-line
if argv is None:
Expand All @@ -264,7 +263,7 @@ def initialize(self, argv=None):
if allow_insecure_writes:
issue_insecure_write_warning()

def start(self):
def start(self) -> None:
"""Start the whole thing"""
if self.subcommand:
os.execv(self.subcommand, [self.subcommand] + self.argv[1:]) # noqa
Expand All @@ -279,10 +278,10 @@ def start(self):
raise NoStart()

@classmethod
def launch_instance(cls, argv=None, **kwargs):
def launch_instance(cls, argv: t.Any = None, **kwargs: t.Any) -> None:
"""Launch an instance of a Jupyter Application"""
try:
return super().launch_instance(argv=argv, **kwargs)
super().launch_instance(argv=argv, **kwargs)
except NoStart:
return

Expand Down
Loading

0 comments on commit cde7c08

Please sign in to comment.