-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre-commit hook to stop linting errors being pushed. (#2632)
Summary: ## Motivation I am so used to relying on `pre-commit` that I have forgotten to run manual linting when making PRs to `botorch`. This adds a simple `pre-commit` config to automate linting. ### Have you read the [Contributing Guidelines on pull requests](https://github.com/pytorch/botorch/blob/main/CONTRIBUTING.md#pull-requests)? Yes Pull Request resolved: #2632 Test Plan: This PR should not affect anything. There are tools to run pre-commit in ci that maintainers could add to the package: https://pre-commit.ci/ ## Related Issues: omnilib/ufmt#251 The pre-commit hook only seems to work for v2.3.0 of ufmt. This issue tracks the error following from ruff_api. Reviewed By: saitcakmak Differential Revision: D66169817 Pulled By: Balandat fbshipit-source-id: a1034d8a882749ee7ff08d4cc92188957073b3c8
- Loading branch information
1 parent
5d37606
commit 3f2e2c7
Showing
13 changed files
with
165 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
repos: | ||
- repo: local | ||
hooks: | ||
- id: check-requirements-versions | ||
name: Check pre-commit formatting versions | ||
entry: python scripts/check_pre_commit_reqs.py | ||
language: python | ||
always_run: true | ||
pass_filenames: false | ||
additional_dependencies: | ||
- PyYAML | ||
|
||
- repo: https://github.com/omnilib/ufmt | ||
rev: v2.8.0 | ||
hooks: | ||
- id: ufmt | ||
additional_dependencies: | ||
- black==24.4.2 | ||
- usort==1.0.8.post1 | ||
- ruff-api==0.1.0 | ||
- stdlibs==2024.1.28 | ||
args: [format] | ||
|
||
- repo: https://github.com/pycqa/flake8 | ||
rev: 7.0.0 | ||
hooks: | ||
- id: flake8 | ||
additional_dependencies: | ||
- flake8-docstrings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import sys | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
|
||
def parse_requirements(filepath): | ||
"""Parse requirements file and return a dict of package versions.""" | ||
versions = {} | ||
with open(filepath) as f: | ||
for line in f: | ||
line = line.strip() | ||
if line and not line.startswith("#"): | ||
# Handle different requirement formats | ||
if "==" in line: | ||
pkg, version = line.split("==") | ||
versions[pkg.strip().lower()] = version.strip() | ||
return versions | ||
|
||
|
||
def parse_precommit_config(filepath): | ||
"""Parse pre-commit config and extract ufmt repo rev and hook dependencies.""" | ||
with open(filepath) as f: | ||
config = yaml.safe_load(f) | ||
|
||
versions = {} | ||
for repo in config["repos"]: | ||
if "https://github.com/omnilib/ufmt" in repo.get("repo", ""): | ||
# Get ufmt version from rev - assumes fixed format: vX.Y.Z | ||
versions["ufmt"] = repo.get("rev", "").replace("v", "") | ||
|
||
# Get dependency versions | ||
for hook in repo["hooks"]: | ||
if hook["id"] == "ufmt": | ||
for dep in hook.get("additional_dependencies", []): | ||
if "==" in dep: | ||
pkg, version = dep.split("==") | ||
versions[pkg.strip().lower()] = version.strip() | ||
break | ||
return versions | ||
|
||
|
||
def main(): | ||
# Find the pre-commit config and requirements files | ||
config_file = Path(".pre-commit-config.yaml") | ||
requirements_file = Path("requirements-fmt.txt") | ||
|
||
if not config_file.exists(): | ||
print(f"Error: Could not find {config_file}") | ||
sys.exit(1) | ||
|
||
if not requirements_file.exists(): | ||
print(f"Error: Could not find {requirements_file}") | ||
sys.exit(1) | ||
|
||
# Parse both files | ||
req_versions = parse_requirements(requirements_file) | ||
config_versions = parse_precommit_config(config_file) | ||
|
||
# Check versions | ||
mismatches = [] | ||
for pkg, req_ver in req_versions.items(): | ||
req_ver = req_versions.get(pkg, None) | ||
config_ver = config_versions.get(pkg, None) | ||
|
||
if req_ver != config_ver: | ||
found_version_str = f"{pkg}: {requirements_file} has {req_ver}," | ||
if pkg == "ufmt": | ||
mismatches.append( | ||
f"{found_version_str} pre-commit config rev has v{config_ver}" | ||
) | ||
else: | ||
mismatches.append( | ||
f"{found_version_str} pre-commit config has {config_ver}" | ||
) | ||
|
||
# Report results | ||
if mismatches: | ||
msg_str = "".join("\n\t" + msg for msg in mismatches) | ||
print( | ||
f"Version mismatches found:{msg_str}" | ||
"\nPlease update the versions in `.pre-commit-config.yaml` to be " | ||
"consistent with those in `requirements-fmt.txt` (source of truth)." | ||
"\nNote: all versions must be pinned exactly ('==X.Y.Z') in both files." | ||
) | ||
sys.exit(1) | ||
else: | ||
print("All versions match!") | ||
sys.exit(0) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters