From a8808b97a8867ddb3cb08453d9266d594ee15c90 Mon Sep 17 00:00:00 2001 From: pdmurray Date: Wed, 18 Sep 2024 15:11:08 -0700 Subject: [PATCH 1/3] Add pre-commit and lint workflow --- .github/workflows/lint.yml | 14 +++++++ .pre-commit-config.yaml | 37 +++++++++++++++++++ tensorflow_serving/tools/pip_package/setup.py | 3 ++ 3 files changed, 54 insertions(+) create mode 100644 .github/workflows/lint.yml create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000000..0a1eb0c8e77 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,14 @@ +name: pre-commit + +on: + pull_request: + push: + branches: [master] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + - uses: pre-commit/action@v3.0.1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000000..40612ac573a --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,37 @@ +# pre-commit is a tool to perform a predefined set of tasks manually and/or +# automatically before git commits are made. +# +# Config reference: https://pre-commit.com/#pre-commit-configyaml---top-level +# +# Common tasks +# +# - Register git hooks: pre-commit install +# - Run on all files: pre-commit run --all-files +# +# These pre-commit hooks are run as CI. +# +# NOTE: if it can be avoided, add configs/args in pyproject.toml or below instead of creating a new `.config.file`. +# https://pre-commit.ci/#configuration +ci: + autoupdate_schedule: monthly + autofix_commit_msg: | + [pre-commit.ci] Apply automatic pre-commit fixes + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: end-of-file-fixer + exclude: '\.svg$' + - id: trailing-whitespace + exclude: '\.svg$' + - id: check-json + - id: check-yaml + args: [--allow-multiple-documents, --unsafe] + - id: check-toml + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.5.6 + hooks: + - id: ruff + args: ["--fix"] diff --git a/tensorflow_serving/tools/pip_package/setup.py b/tensorflow_serving/tools/pip_package/setup.py index 9531ceb3be7..b673d71010c 100644 --- a/tensorflow_serving/tools/pip_package/setup.py +++ b/tensorflow_serving/tools/pip_package/setup.py @@ -98,4 +98,7 @@ 'Topic :: Software Development :: Libraries', 'Topic :: Software Development :: Libraries :: Python Modules', ], + extras_require={ + 'lint': ['pre-commit'], + }, ) From 308c0083b30f26828acc2844e85a2c034f8495fa Mon Sep 17 00:00:00 2001 From: smokestacklightnin <125844868+smokestacklightnin@users.noreply.github.com> Date: Sat, 9 Aug 2025 00:02:28 -0700 Subject: [PATCH 2/3] Also ignore patch files in pre-commit --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 40612ac573a..6635c00d042 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,9 +22,9 @@ repos: rev: v4.6.0 hooks: - id: end-of-file-fixer - exclude: '\.svg$' + exclude: '\.(svg|patch)$' - id: trailing-whitespace - exclude: '\.svg$' + exclude: '\.(svg|patch)$' - id: check-json - id: check-yaml args: [--allow-multiple-documents, --unsafe] From 5d9d45bcc6f6ec9ca13513e3054e177f803c87bb Mon Sep 17 00:00:00 2001 From: smokestacklightnin <125844868+smokestacklightnin@users.noreply.github.com> Date: Sat, 9 Aug 2025 00:04:34 -0700 Subject: [PATCH 3/3] Add ruff config file --- ruff.toml | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 ruff.toml diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 00000000000..32e4fdb01fb --- /dev/null +++ b/ruff.toml @@ -0,0 +1,89 @@ +line-length = 88 + +[lint] + +select = [ + # pycodestyle + "E", + "W", + # Pyflakes + "F", + # pyupgrade + "UP", + # flake8-bugbear + "B", + # flake8-simplify + "SIM", + # isort + "I", + # pep8 naming + "N", + # pydocstyle + "D", + # annotations + "ANN", + # debugger + "T10", + # flake8-pytest + "PT", + # flake8-return + "RET", + # flake8-unused-arguments + "ARG", + # flake8-fixme + "FIX", + # flake8-eradicate + "ERA", + # pandas-vet + "PD", + # numpy-specific rules + "NPY", +] + +ignore = [ + "D104", # Missing docstring in public package + "D100", # Missing docstring in public module + "D211", # No blank line before class + "PD901", # Avoid using 'df' for pandas dataframes. Perfectly fine in functions with limited scope + "ANN201", # Missing return type annotation for public function (makes no sense for NoneType return types...) + "ANN101", # Missing type annotation for `self` + "ANN204", # Missing return type annotation for special method + "ANN002", # Missing type annotation for `*args` + "ANN003", # Missing type annotation for `**kwargs` + "D105", # Missing docstring in magic method + "D203", # 1 blank line before after class docstring + "D204", # 1 blank line required after class docstring + "D413", # 1 blank line after parameters + "SIM108", # Simplify if/else to one line; not always clearer + "D206", # Docstrings should be indented with spaces; unnecessary when running ruff-format + "E501", # Line length too long; unnecessary when running ruff-format + "W191", # Indentation contains tabs; unnecessary when running ruff-format + + # FIX AND REMOVE BELOW CODES: + "ANN001", # Missing type annotation for function argument + "ANN102", # Missing type annotation for `cls` in classmethod + "ANN202", # Missing return type annotation for private function + "ANN205", # Missing return type annotation for staticmethod + "ANN206", # Missing return type annotation for classmethod + "D102", # Missing docstring in public method + "D103", # Missing docstring in public function + "D107", # Missing docstring in `__init__` + "E402", # Module level import not at top of file + "ERA001", # Found commented-out code + "F401", # `module` imported but unused + "F405", # Name may be undefined, or defined from star imports + "FIX002", # Line contains TODO, consider resolving the issue + "N802", # Function name should be lowercase + "PT009", # Use a regular `assert` instead of unittest-style `assertEqual` / `assertIsInstance` + "PT027", # Use `pytest.raises` instead of unittest-style `assertRaisesRegex` + "UP028", # Replace `yield` over `for` loop with `yield from` + "UP029", # Unnecessary builtin import +] + +[lint.pyupgrade] +# Preserve types, even if a file imports `from __future__ import annotations`. +# Remove when Python 3.9 is no longer supported +keep-runtime-typing = true + +[lint.pydocstyle] +convention = "google" \ No newline at end of file