Skip to content

Commit

Permalink
feat: add type annotations (#37)
Browse files Browse the repository at this point in the history
Fix #33.

---------

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
  • Loading branch information
njzjz authored May 12, 2024
1 parent 5b43cf4 commit 9a88716
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ repos:
- id: ruff
args: ["--fix"]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
hooks:
- id: mypy
args: [--strict, --ignore-missing-imports]
additional_dependencies: [hatch]
8 changes: 5 additions & 3 deletions cmd/hatch_build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Type
from __future__ import annotations

from typing import Any

from hatchling.metadata.plugin.interface import MetadataHookInterface
from hatchling.plugin import hookimpl
Expand All @@ -7,10 +9,10 @@
class DependenciesMetadataHook(MetadataHookInterface):
PLUGIN_NAME = "dependencies"

def update(self, metadata: dict) -> None:
def update(self, metadata: dict[str, Any]) -> None:
metadata["dependencies"] = [f"nodejs-wheel-binaries=={metadata['version']}"]


@hookimpl
def hatch_register_metadata_hook() -> Type[DependenciesMetadataHook]:
def hatch_register_metadata_hook() -> type[DependenciesMetadataHook]:
return DependenciesMetadataHook
3 changes: 2 additions & 1 deletion nodejs_wheel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from .executable import (
node,
npm,
npx,
)


__all__ = [
"node",
"npm",
Expand Down
2 changes: 2 additions & 0 deletions nodejs_wheel/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from .executable import _node_entry_point

if __name__ == "__main__":
Expand Down
20 changes: 11 additions & 9 deletions nodejs_wheel/executable.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from __future__ import annotations

import os
import subprocess
import sys

from typing import Any

ROOT_DIR = os.path.dirname(__file__)


def _program(name, args, **kwargs):
def _program(name: str, args: list[str], **kwargs: Any) -> int:
bin_dir = ROOT_DIR if os.name == "nt" else os.path.join(ROOT_DIR, "bin")
return subprocess.call([os.path.join(bin_dir, name), *args], **kwargs)


def call_node(*args, **kwargs):
def call_node(*args: str, **kwargs: Any) -> int:
suffix = ".exe" if os.name == "nt" else ""
return _program("node" + suffix, list(args), **kwargs)


def node(args=None, **kwargs):
def node(args: list[str] | None = None, **kwargs: Any) -> int:
"""Call the node executable with the given arguments.
Parameters
Expand All @@ -37,7 +39,7 @@ def node(args=None, **kwargs):
return call_node(*args, **kwargs)


def npm(args=None, **kwargs):
def npm(args: list[str] | None = None, **kwargs: Any) -> int:
"""Call the npm executable with the given arguments.
Parameters
Expand All @@ -62,7 +64,7 @@ def npm(args=None, **kwargs):
)


def npx(args=None, **kwargs):
def npx(args: list[str] | None = None, **kwargs: Any) -> int:
"""Call the npx executable with the given arguments.
Parameters
Expand All @@ -87,13 +89,13 @@ def npx(args=None, **kwargs):
)


def _node_entry_point():
def _node_entry_point() -> None:
raise SystemExit(node(close_fds=False))


def _npm_entry_point():
def _npm_entry_point() -> None:
raise SystemExit(npm(close_fds=False))


def _npx_entry_point():
def _npx_entry_point() -> None:
raise SystemExit(npx(close_fds=False))
Empty file added nodejs_wheel/py.typed
Empty file.
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ select = [
"RUF", # ruff
"NPY", # numpy
"T20", # ban print
"I", # isort
"TCH", # flake8-type-checking
]

ignore = [
Expand All @@ -77,3 +79,9 @@ ignore-init-module-imports = true

[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.ruff.lint.isort]
required-imports = ["from __future__ import annotations"]

[tool.pyright]
exclude = ["cmd/hatch_build.py"]
2 changes: 2 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from nodejs_wheel import (
node,
npm,
Expand Down

0 comments on commit 9a88716

Please sign in to comment.