From 9a88716b7927c02a52d570d02b9f1a0d6d1117c8 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 12 May 2024 02:51:47 -0400 Subject: [PATCH] feat: add type annotations (#37) Fix #33. --------- Signed-off-by: Jinzhe Zeng --- .pre-commit-config.yaml | 6 ++++++ cmd/hatch_build.py | 8 +++++--- nodejs_wheel/__init__.py | 3 ++- nodejs_wheel/__main__.py | 2 ++ nodejs_wheel/executable.py | 20 +++++++++++--------- nodejs_wheel/py.typed | 0 pyproject.toml | 8 ++++++++ tests/test_api.py | 2 ++ 8 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 nodejs_wheel/py.typed diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ed1722e..4e0b1af 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] diff --git a/cmd/hatch_build.py b/cmd/hatch_build.py index 5445fe5..22f14f2 100644 --- a/cmd/hatch_build.py +++ b/cmd/hatch_build.py @@ -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 @@ -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 diff --git a/nodejs_wheel/__init__.py b/nodejs_wheel/__init__.py index add3bf8..cdcdf9b 100644 --- a/nodejs_wheel/__init__.py +++ b/nodejs_wheel/__init__.py @@ -1,10 +1,11 @@ +from __future__ import annotations + from .executable import ( node, npm, npx, ) - __all__ = [ "node", "npm", diff --git a/nodejs_wheel/__main__.py b/nodejs_wheel/__main__.py index 674ada8..c65b0bd 100644 --- a/nodejs_wheel/__main__.py +++ b/nodejs_wheel/__main__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from .executable import _node_entry_point if __name__ == "__main__": diff --git a/nodejs_wheel/executable.py b/nodejs_wheel/executable.py index df35e9d..aa1a94e 100644 --- a/nodejs_wheel/executable.py +++ b/nodejs_wheel/executable.py @@ -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 @@ -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 @@ -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 @@ -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)) diff --git a/nodejs_wheel/py.typed b/nodejs_wheel/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/pyproject.toml b/pyproject.toml index f534309..4d50baf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,6 +56,8 @@ select = [ "RUF", # ruff "NPY", # numpy "T20", # ban print + "I", # isort + "TCH", # flake8-type-checking ] ignore = [ @@ -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"] diff --git a/tests/test_api.py b/tests/test_api.py index af28b78..de38472 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from nodejs_wheel import ( node, npm,