Skip to content

Commit

Permalink
add mypy to linting checks (#19)
Browse files Browse the repository at this point in the history
Reading through this project's code for work on
rapidsai/build-planning#31, I noticed it isn't
using type hints or running a type-checking

This proposes the following:

* adding `mypy` to checks run via pre-commit
* adding type hints for a few cases

I think this'll make the code a little easier to understand, and improve
the change of catching some types of bugs (like treating a value which
can be `None` as if it was unconditionally not `None) during
development.

### How I tested this

```shell
pre-commit run --all-files
```
  • Loading branch information
jameslamb committed May 2, 2024
1 parent cdebf61 commit fdd6b6e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
__pycache__
.cache
.mypy_cache/
.ruff_cache/
.vscode
*.swp
.DS_Store
Expand Down
10 changes: 9 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ repos:
hooks:
- id: verify-copyright
args: [--fix, --main-branch=main]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.10.0'
hooks:
- id: mypy
args: [
"--config-file=pyproject.toml",
"rapids_build_backend/"
]
pass_filenames: false

default_language_version:
python: python3
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ license-files = ["LICENSE"]
[tool.setuptools.packages.find]
include = ["rapids_build_backend*"]

[tool.mypy]
ignore_missing_imports = true

[tool.ruff]
lint.select = ["E", "F", "W", "I", "N", "UP"]
lint.fixable = ["ALL"]
15 changes: 14 additions & 1 deletion rapids_build_backend/config.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
# Copyright (c) 2024, NVIDIA CORPORATION.

import os
from typing import TYPE_CHECKING

from .utils import _get_pyproject

if TYPE_CHECKING:
from typing import Callable

# config options can be one of these types...
config_val_type = str | bool | None

# ... or a callable that returns one of those or some other mutable types
mutable_config_val_type = list[str]
config_val_callable = Callable[[], config_val_type | mutable_config_val_type]

config_options_type = dict[str, tuple[config_val_type | config_val_callable, bool]]


class Config:
"""Manage the build configuration for the project."""

# Mapping from config option to default value (None indicates that option is
# required) and whether it may be overridden by an environment variable or a config
# setting.
config_options = {
config_options: "config_options_type" = {
"build-backend": (None, False),
"commit-file": ("", False),
"dependencies-file": ("dependencies.yaml", True),
Expand Down
4 changes: 2 additions & 2 deletions rapids_build_backend/impls.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _get_cuda_version(require_cuda=False):


@lru_cache
def _get_cuda_suffix(require_cuda=False):
def _get_cuda_suffix(require_cuda=False) -> str:
"""Get the CUDA suffix based on nvcc.
Parameters
Expand All @@ -99,7 +99,7 @@ def _get_cuda_suffix(require_cuda=False):


@lru_cache
def _get_git_commit():
def _get_git_commit() -> str | None:
"""Get the current git commit.
Returns None if git is not in the PATH or if it fails to find the commit.
Expand Down
2 changes: 1 addition & 1 deletion rapids_build_backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tomli


def _get_pyproject(dirname="."):
def _get_pyproject(dirname: str = ".") -> dict:
"""Parse and return the pyproject.toml file."""
with open(os.path.join(dirname, "pyproject.toml"), "rb") as f:
return tomli.load(f)

0 comments on commit fdd6b6e

Please sign in to comment.