Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt sp-repo-review #126

Merged
merged 8 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,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
36 changes: 34 additions & 2 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 @@ -29,13 +30,44 @@ repos:
hooks:
- id: mdformat

- 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"]
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]"]
3 changes: 1 addition & 2 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

version: 2
sphinx:
configuration: docs/conf.py
Expand All @@ -12,4 +11,4 @@ python:
build:
os: ubuntu-22.04
tools:
python: "3.11"
python: "3.11"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This provides a [build hook](https://hatch.pypa.io/latest/config/build/#build-ho

## Documentation

The full documenation is available on [Read The Docs](https://hatch-jupyter-builder.readthedocs.io/en/latest/).
The full documentation is available on [Read The Docs](https://hatch-jupyter-builder.readthedocs.io/en/latest/).

## Installation

Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. hatch-jupyter-builder documentation master file, created by
sphinx-quickstart on Mon Jul 4 07:51:40 2022.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
contain the root ``toctree`` directive.

Welcome to hatch-jupyter-builder's documentation!
=================================================
Expand Down
2 changes: 1 addition & 1 deletion docs/source/background/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Background
==========
Explanation. Clarification and discussion of key topics.

Hatch Jupyter Builder is a plugin for the `hatchling` Python build backend.
Hatch Jupyter Builder is a plugin for the ``hatchling`` Python build backend.
It is primarily targeted for package authors who are providing JavaScript
as part of their Python packages.

Expand Down
5 changes: 3 additions & 2 deletions docs/source/get_started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Tutorials. A hands-on introduction to Hatch Jupyter Builder for maintainers.

config

If starting from a new project, use `hatch new "my project name"`, and follow
along with the prompts. Then add the appropriate [configuration](./config) for `hatch_jupyter_builder` in your `pyproject.toml` flag.
If starting from a new project, use ``hatch new "my project name"``, and follow
along with the prompts. Then add the appropriate [configuration](./config) for
``hatch_jupyter_builder`` in your ``pyproject.toml`` flag.

If migrating from existing project, find the appropriate [how to guides](../how_to_guides).
25 changes: 13 additions & 12 deletions hatch_jupyter_builder/compare_migrated/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Compare the dist file created by a migrated package to one created by the original."""
from __future__ import annotations

import argparse
import glob
import logging
Expand All @@ -8,10 +10,9 @@
import sys
import tarfile
import zipfile
from typing import Optional


def build_file(dirname, dist_name):
def build_file(dirname: str, dist_name: str) -> None:
"""Build a dist file in a directory."""
orig_dir = os.getcwd()
os.chdir(dirname)
Expand All @@ -21,21 +22,21 @@ def build_file(dirname, dist_name):
os.chdir(orig_dir)


def get_tar_names(dirname):
def get_tar_names(dirname: str) -> set[str]:
"""Get the tarball names in a directory."""
dist_file = glob.glob(f"{dirname}/dist/*.tar.gz")[0]
tarf = tarfile.open(dist_file, "r:gz")
return set(tarf.getnames())


def get_zip_names(dirname):
def get_zip_names(dirname: str) -> set[str]:
"""Get the zip (wheel) file names in a directory."""
wheel_file = glob.glob(f"{dirname}/dist/*.whl")[0]
with zipfile.ZipFile(wheel_file, "r") as f:
return set(f.namelist())


def filter_file(path):
def filter_file(path: str) -> bool:
"""Filter a file path for interesting files."""
if "egg-info" in path:
return True
Expand All @@ -47,7 +48,7 @@ def filter_file(path):
return False


def main(source_dir, target_dir, dist_name):
def main(source_dir: str, target_dir: str, dist_name: str) -> dict[str, list[str]]:
"""The main script."""
subprocess.check_call([sys.executable, "-m", "pip", "install", "build"])

Expand All @@ -64,25 +65,25 @@ def main(source_dir, target_dir, dist_name):
source_names = get_zip_names(source_dir)
target_names = get_zip_names(target_dir)

removed = source_names - target_names
removed = list(source_names - target_names)
removed = [r for r in removed if not filter_file(r)]
if removed:
logger.info("\nRemoved_files:")
[logger.info(f) for f in removed] # type:ignore
[logger.info(f) for f in removed] # type:ignore[func-returns-value]

added = target_names - source_names
added = list(target_names - source_names)
added = [a for a in added if not filter_file(a)]
if added:
logger.info("\nAdded files:")
[logger.info(f) for f in added] # type:ignore
[logger.info(f) for f in added] # type:ignore[func-returns-value]

logger.info("")

return {"added": added, "removed": removed}


def make_parser(
parser: Optional[argparse.ArgumentParser] = None, prog: Optional[str] = None
parser: argparse.ArgumentParser | None = None, prog: str | None = None
) -> argparse.ArgumentParser:
"""Make an arg parser."""
if parser is None:
Expand All @@ -93,7 +94,7 @@ def make_parser(
return parser


def run(args: Optional[argparse.Namespace] = None) -> None:
def run(args: argparse.Namespace | None = None) -> None:
"""Run the cli."""
if args is None:
parser = make_parser(prog=f"{sys.executable} -m hatch_jupyter_builder.compare_migrated")
Expand Down
4 changes: 3 additions & 1 deletion hatch_jupyter_builder/hooks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Register hooks for the plugin."""
from typing import Type

from hatchling.plugin import hookimpl

from .plugin import JupyterBuildHook


@hookimpl
def hatch_register_build_hook():
def hatch_register_build_hook() -> Type[JupyterBuildHook]:
"""Get the hook implementation."""
return JupyterBuildHook
4 changes: 2 additions & 2 deletions hatch_jupyter_builder/migrate/_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
)
else:
warnings.append("Fill in '[project][version]' in 'pyproject.toml'")
current_version = "!!UNKONWN!!"
current_version = "!!UNKNOWN!!"

# Run the hatch migration script.
logger.info("Running hatch migration")
Expand Down Expand Up @@ -91,7 +91,7 @@
if not matches:
continue

if matches and line.startswith("["):
if line.startswith("["):
break

flake8.append(line)
Expand Down
2 changes: 1 addition & 1 deletion hatch_jupyter_builder/migrate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from hatch_jupyter_builder import __version__ as builder_version


def main(td, target_dir):
def main(td: str, target_dir: str) -> None:
"""Main script."""
logger = logging.getLogger(__name__)
logging.basicConfig()
Expand Down
56 changes: 36 additions & 20 deletions hatch_jupyter_builder/migrate/jupyter_packaging.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"""Shim for jupyter packaging migration."""
from __future__ import annotations

import os
import sys
from pathlib import Path
from typing import Any

import tomli
import tomli_w

__this_shim = sys.modules.pop("jupyter_packaging")
__current_directory = sys.path.pop(0)

import jupyter_packaging as __real_jupyter_packaging # type:ignore
import jupyter_packaging as __real_jupyter_packaging # type:ignore[import]

sys.path.insert(0, __current_directory)
sys.modules["jupyter_packaging"] = __this_shim


def _write_config(path, data):
def _write_config(path: Any, data: Any) -> None:
pyproject = Path("pyproject.toml")
top = tomli.loads(pyproject.read_text(encoding="utf-8"))
current = top
Expand All @@ -36,15 +39,15 @@ def _write_config(path, data):
_npm_kwargs = ["path", "build_dir", "source_dir", "build_cmd", "npm"]


def _normalize_path(path):
def _normalize_path(path: str) -> str:
path = str(path)
cwd = os.getcwd()
if path.startswith(cwd):
return os.path.relpath(path, cwd)
return path


def _get_build_kwargs(**kwargs):
def _get_build_kwargs(**kwargs: Any) -> dict[str, Any]:
build_kwargs = {}
for name in _npm_kwargs:
value = kwargs[name]
Expand All @@ -57,15 +60,15 @@ def _get_build_kwargs(**kwargs):
return build_kwargs


def skip_if_exists(paths, *args):
def skip_if_exists(paths: list[str], *args: str) -> Any:
"""Shim for skip if exists"""
if paths:
data = {"skip-if-exists": [_normalize_path(p) for p in paths]}
_write_config("tool.hatch.build.hooks.jupyter-builder", data)
return __real_jupyter_packaging.skip_if_exists(paths, *args)


def ensure_targets(targets):
def ensure_targets(targets: list[str]) -> Any:
"""Shim for ensure targets"""
if targets:
data = {"ensured-targets": [_normalize_path(t) for t in targets]}
Expand All @@ -74,13 +77,13 @@ def ensure_targets(targets):


def wrap_installers(
pre_develop=None,
pre_dist=None,
post_develop=None,
post_dist=None,
ensured_targets=None,
skip_if_exists=None,
):
pre_develop: Any = None,
pre_dist: Any = None,
post_develop: Any = None,
post_dist: Any = None,
ensured_targets: Any = None,
skip_if_exists: Any = None,
) -> Any:
"""Shim for wrap_installers."""
if pre_develop or post_develop:
func = pre_develop or post_develop
Expand All @@ -107,8 +110,11 @@ def wrap_installers(


def create_cmdclass(
prerelease_cmd=None, package_data_spec=None, data_files_spec=None, exclude=None
):
prerelease_cmd: Any = None,
package_data_spec: Any = None,
data_files_spec: Any = None,
exclude: Any = None,
) -> Any:
"""Shim for create_cmdclass."""
shared_data = {}
if data_files_spec is not None:
Expand All @@ -131,8 +137,13 @@ def create_cmdclass(


def install_npm(
path=None, build_dir=None, source_dir=None, build_cmd="build", force=False, npm=None
):
path: Any = None,
build_dir: Any = None,
source_dir: Any = None,
build_cmd: str = "build",
force: bool = False,
npm: Any = None,
) -> Any:
"""Shim for install_npm."""
build_kwargs = _get_build_kwargs(**locals())
if build_kwargs:
Expand All @@ -149,8 +160,13 @@ def install_npm(


def npm_builder(
path=None, build_dir=None, source_dir=None, build_cmd="build", force=False, npm=None
):
path: Any = None,
build_dir: Any = None,
source_dir: Any = None,
build_cmd: str = "build",
force: bool = False,
npm: Any = None,
) -> Any:
"""Shim for npm_builder."""
func = __real_jupyter_packaging.npm_builder(
path=path,
Expand All @@ -166,7 +182,7 @@ def npm_builder(
return func


def __getattr__(name):
def __getattr__(name: str) -> Any:
"""Defer to the original for all others."""
return getattr(__real_jupyter_packaging, name)

Expand Down
Loading