-
Notifications
You must be signed in to change notification settings - Fork 306
Make pre-commit work on Windows #2327
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
base: main
Are you sure you want to change the base?
Changes from all commits
f45008d
21a7730
9684d19
a446787
25525cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ repos: | |
|
|
||
| - id: stubgen-pyx-cuda-core | ||
| name: Generate .pyi stubs for cuda_core | ||
| entry: stubgen-pyx cuda_core/cuda --continue-on-error --include-private | ||
| entry: python ./toolshed/run_stubgen_pyx.py | ||
| language: python | ||
| files: ^cuda_core/cuda/.*\.(pyx|pxd)$ | ||
| pass_filenames: false | ||
|
|
@@ -88,7 +88,7 @@ repos: | |
| - id: check-yaml | ||
| - id: debug-statements | ||
| - id: end-of-file-fixer | ||
| exclude: &gen_exclude '^(?:cuda_python/README\.md|cuda_bindings/cuda/bindings/.*\.in?|cuda_bindings/docs/source/module/.*\.rst?|.*\.pyi)$' | ||
| exclude: &gen_exclude '^(?:cuda_python/README\.md|(?:^|/)CLAUDE\.md|(?:^|/)\.git_archival\.txt|cuda_bindings/cuda/bindings/.*\.in?|cuda_bindings/docs/source/module/.*\.rst?|.*\.pyi)$' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still misses eight nested symlink placeholders. Because the complete expression is anchored with |
||
| - id: mixed-line-ending | ||
| - id: trailing-whitespace | ||
| exclude: | | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ Thank you for your interest in contributing to CUDA Python! Based on the type of | |
| - [Table of Contents](#table-of-contents) | ||
| - [Type stubs for cuda.core](#type-stubs-for-cudacore) | ||
| - [Pre-commit](#pre-commit) | ||
| - [Pre-commit on Windows](#pre-commit-on-windows) | ||
| - [Signing Your Work](#signing-your-work) | ||
| - [Code signing](#code-signing) | ||
| - [Developer Certificate of Origin (DCO)](#developer-certificate-of-origin-dco) | ||
|
|
@@ -74,6 +75,17 @@ between commits, leaving stale headers or out-of-date stubs in the history. | |
| If the hook isn't installed, `pre-commit run` (and CI) will print a visible | ||
| warning reminding you to run `pre-commit install`. | ||
|
|
||
| ### Pre-commit on Windows | ||
|
|
||
| For development on Windows (not WSL), the `lychee` pre-commit task will not work | ||
| when running `pre-commit run --all-files`. This problem does not occur if you | ||
| install the pre-commit hook and run it automatically as part of your `git | ||
| commit` workflow. To resolve this, you can either: | ||
|
|
||
| 1. Run `pre-commit` it in Git Bash, rather directly in PowerShell or cmd | ||
|
|
||
| 2. Skip it by setting the environment variable `SKIP` to `lychee`. This would | ||
| be `$env:SKIP = "lychee"` in PowerShell or `SKIP=lychee` in cmd. | ||
|
Comment on lines
+85
to
+88
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Command Prompt requires |
||
|
|
||
| ## Signing Your Work | ||
|
|
||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Touching this file seems unnecessary, why does pre-commit care on Windows?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because
IME, this stuff is necessary to make type-checking complete, annoying as it is. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,13 @@ | |
|
|
||
| import ctypes | ||
| import functools | ||
| import sys | ||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( | ||
| load_nvidia_dynamic_lib as _load_nvidia_dynamic_lib, | ||
| ) | ||
| from cuda.pathfinder._utils.platform_aware import IS_WINDOWS | ||
|
|
||
|
|
||
| class QueryDriverCudaVersionError(RuntimeError): | ||
|
|
@@ -63,10 +63,8 @@ def query_driver_cuda_version() -> DriverCudaVersion: | |
| def _query_driver_cuda_version_int() -> int: | ||
| """Return the encoded CUDA driver version from ``cuDriverGetVersion()``.""" | ||
| loaded_cuda = _load_nvidia_dynamic_lib("cuda") | ||
| if IS_WINDOWS: | ||
| # `ctypes.WinDLL` exists on Windows at runtime. The ignore is only for | ||
| # Linux mypy runs, where the platform stubs do not define that attribute. | ||
| loader_cls: Callable[[str], ctypes.CDLL] = ctypes.WinDLL # type: ignore[attr-defined] | ||
| if sys.platform == "win32": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The direct I asked my agent about teaching tests to keep if sys.platform == "win32":
_DRIVER_LIB_LOADER: Callable[[str], ctypes.CDLL] = ctypes.WinDLL
else:
_DRIVER_LIB_LOADER = ctypes.CDLLand then: driver_lib = _DRIVER_LIB_LOADER(loaded_cuda.abs_path)Unit tests can patch |
||
| loader_cls: Callable[[str], ctypes.CDLL] = ctypes.WinDLL | ||
| else: | ||
| loader_cls = ctypes.CDLL | ||
| driver_lib = loader_cls(loaded_cuda.abs_path) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Run stubgen-pyx for cuda_core and normalize line endings in the generated | ||
| stubs across platforms. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import pathlib | ||
| import subprocess | ||
|
|
||
|
|
||
| def _normalize_generated_stubs(root: pathlib.Path) -> None: | ||
| for stub in root.rglob("*.pyi"): | ||
| data = stub.read_bytes() | ||
| if not data: | ||
| continue | ||
|
|
||
| # Normalize line endings to LF so Windows runs do not churn tracked files. | ||
| normalized_data = data.replace(b"\r\n", b"\n").replace(b"\r", b"\n") | ||
|
|
||
| split_at = normalized_data.find(b"\n") | ||
| if split_at == -1: | ||
| first_line = normalized_data | ||
| remainder = b"" | ||
| newline = b"" | ||
| else: | ||
| first_line = normalized_data[:split_at] | ||
| remainder = normalized_data[split_at + 1 :] | ||
| newline = b"\n" | ||
|
|
||
| if not first_line.startswith(b"# This file was generated by stubgen-pyx"): | ||
| continue | ||
|
|
||
| normalized_header = first_line.replace(b"\\", b"/") | ||
| updated_data = normalized_header + newline + remainder | ||
| if updated_data != data: | ||
| stub.write_bytes(updated_data) | ||
|
|
||
|
|
||
| def main() -> int: | ||
| env = os.environ.copy() | ||
| env.setdefault("PYTHONUTF8", "1") | ||
| env.setdefault("PYTHONIOENCODING", "utf-8") | ||
|
|
||
| cmd = [ | ||
| "stubgen-pyx", | ||
| "cuda_core/cuda", | ||
| "--continue-on-error", | ||
| "--include-private", | ||
| ] | ||
| result = subprocess.run(cmd, env=env) # noqa: S603 | ||
| if result.returncode != 0: | ||
| return result.returncode | ||
|
|
||
| _normalize_generated_stubs(pathlib.Path("cuda_core/cuda")) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
Uh oh!
There was an error while loading. Please reload this page.