Skip to content

Commit

Permalink
Add linter setup copied from torchtune
Browse files Browse the repository at this point in the history
run 'pre-commit' command locally to invoke these linters, before
committing
  • Loading branch information
wconstab committed Feb 2, 2024
1 parent 705e3e0 commit 57faf9f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[flake8]
# Suggested config from pytorch that we can adapt
select = B,C,E,F,N,P,T4,W,B9,TOR0,TOR1,TOR2
max-line-length = 120
# C408 ignored because we like the dict keyword argument syntax
# E501 is not flexible enough, we're using B950 instead
# N812 ignored because import torch.nn.functional as F is PyTorch convention
# N817 ignored because importing using acronyms is convention (DistributedDataParallel as DDP)
# E731 allow usage of assigning lambda expressions
ignore =
E203,E305,E402,E501,E721,E741,F405,F821,F841,F999,W503,W504,C408,E302,W291,E303,N812,N817,E731
# shebang has extra meaning in fbcode lints, so I think it's not worth trying
# to line this up with executable bit
EXE001,
# these ignores are from flake8-bugbear; please fix!
B007,B008,
optional-ascii-coding = True
exclude =
./.git,
./docs
./build
./scripts,
./venv,
*.pyi
.pre-commit-config.yaml
*.md
.flake8
39 changes: 39 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Lint

on:
pull_request:

concurrency:
group: lint-${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.run_number || github.ref }}
cancel-in-progress: true

defaults:
run:
shell: bash -l -eo pipefail {0}

jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11']
steps:
- name: Check out repo
uses: actions/checkout@v3
- name: Setup python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Update pip
run: python -m pip install --upgrade pip
- name: Install lint utilities
run: |
python -m pip install pre-commit
pre-commit install-hooks
- id: file_changes
uses: trilom/file-changes-action@v1.2.4
with:
prNumber: ${{ github.event.number }}
output: ' '
- name: Lint modified files
run: pre-commit run --files ${{ steps.file_changes.outputs.files }}
51 changes: 51 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
exclude: 'build'

default_language_version:
python: python3

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 6306a48f7dae5861702d573c9c247e4e9498e867
hooks:
- id: trailing-whitespace
- id: check-ast
- id: check-merge-conflict
- id: no-commit-to-branch
args: ['--branch=main']
- id: check-added-large-files
args: ['--maxkb=500']
- id: end-of-file-fixer
exclude: '^(.*\.svg)$'

- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.4
hooks:
- id: insert-license
files: \.py$
args:
- --license-filepath
- docs/license_header.txt

- repo: https://github.com/pycqa/flake8
rev: 34cbf8ef3950f43d09b85e2e45c15ae5717dc37b
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear == 22.4.25
- pep8-naming == 0.12.1
- torchfix
args: ['--config=.flake8']

- repo: https://github.com/omnilib/ufmt
rev: v2.3.0
hooks:
- id: ufmt
additional_dependencies:
- black == 22.12.0
- usort == 1.0.5

- repo: https://github.com/jsh9/pydoclint
rev: d88180a8632bb1602a4d81344085cf320f288c5a
hooks:
- id: pydoclint
args: [--config=pyproject.toml]
3 changes: 3 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest
pytest-cov
pre-commit
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tool.pydoclint]
style = 'google'
check-return-types = 'False'

[tool.pytest.ini_options]
addopts = ["--showlocals"] # show local variables in tracebacks

0 comments on commit 57faf9f

Please sign in to comment.