Skip to content

Commit

Permalink
add lintrunner to test-infra (#4319)
Browse files Browse the repository at this point in the history
add lintrunner to test-infra. Specifically we add the formatters and
python specific linters copied over from pytorch core.

Tested by running `lintrunner init`, `lintrunner`, `lintrunner -a`, and
`lintrunner f` locally. (These checks only applied to the copy and
pasted code from this pr).

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at dd05455</samp>

No summary available (Limit exceeded: required to process 74214 tokens,
but only 50000 are allowed per call)
copilot:poem

Co-authored-by: PaliC <>
  • Loading branch information
PaliC authored Jun 23, 2023
1 parent 11df707 commit 5115d8a
Show file tree
Hide file tree
Showing 15 changed files with 2,659 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.vscode
.venv
.idea
.lintbin

*.env
*.zip
Expand Down
310 changes: 310 additions & 0 deletions .lintrunner.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
merge_base_with = "origin/main"

[[linter]]
code = 'FLAKE8'
include_patterns = ['**/*.py']
exclude_patterns = [
'.git/**',
]
command = [
'python3',
'tools/linter/adapters/flake8_linter.py',
'--',
'@{{PATHSFILE}}'
]
init_command = [
'python3',
'tools/linter/adapters/pip_init.py',
'--dry-run={{DRYRUN}}',
'flake8==6.0.0',
'flake8-bugbear==23.3.23',
'flake8-comprehensions==3.12.0',
'flake8-executable==2.1.3',
'flake8-logging-format==0.9.0',
'flake8-pyi==23.3.1',
'flake8-simplify==0.19.3',
'mccabe==0.7.0',
'pycodestyle==2.10.0',
'pyflakes==3.0.1',
]

[[linter]]
code = 'MYPY'
include_patterns = [
'tools/**/*.py',
'tools/**/*.pyi',
'stats/**/*.py',
'stats/**/*.pyi',
'torchci/**/*.py',
'torchci/**/*.pyi',
]
command = [
'python3',
'tools/linter/adapters/mypy_linter.py',
'--config=mypy.ini',
'--',
'@{{PATHSFILE}}'
]
init_command = [
'python3',
'tools/linter/adapters/pip_init.py',
'--dry-run={{DRYRUN}}',
'numpy==1.24.3',
'expecttest==0.1.3',
'mypy==0.960',
'types-requests==2.27.25',
'types-PyYAML==6.0.7',
'types-tabulate==0.8.8',
'types-protobuf==3.19.18',
'types-pkg-resources==0.1.3',
'types-Jinja2==2.11.9',
'junitparser==2.1.1',
'rich==10.9.0',
'pyyaml==6.0',
]

[[linter]]
code = 'TYPEIGNORE'
include_patterns = ['**/*.py', '**/*.pyi']
exclude_patterns = [
]
command = [
'python3',
'tools/linter/adapters/grep_linter.py',
'--pattern=# type:\s*ignore([^\[]|$)',
'--linter-name=TYPEIGNORE',
'--error-name=unqualified type: ignore',
"""--error-description=\
This line has an unqualified `type: ignore`; \
please convert it to `type: ignore[xxxx]`\
""",
'--',
'@{{PATHSFILE}}'
]

[[linter]]
code = 'NOQA'
include_patterns = ['**/*.py', '**/*.pyi']
command = [
'python3',
'tools/linter/adapters/grep_linter.py',
'--pattern=# noqa([^:]|$)',
'--linter-name=NOQA',
'--error-name=unqualified noqa',
"""--error-description=\
This line has an unqualified `noqa`; \
please convert it to `noqa: XXXX`\
""",
'--',
'@{{PATHSFILE}}'
]

[[linter]]
code = 'NEWLINE'
include_patterns=['**']
exclude_patterns=[
'**/*.bat',
'**/*.expect',
'**/*.ipynb',
'**/*.ps1',
'**/*.ptl',
]
command = [
'python3',
'tools/linter/adapters/newlines_linter.py',
'--',
'@{{PATHSFILE}}',
]
is_formatter = true


[[linter]]
code = 'SPACES'
include_patterns = ['**']
exclude_patterns = [
'**/*.diff',
'**/*.patch',
]
command = [
'python3',
'tools/linter/adapters/grep_linter.py',
'--pattern=[[:blank:]]$',
'--linter-name=SPACES',
'--error-name=trailing spaces',
'--replace-pattern=s/[[:blank:]]+$//',
"""--error-description=\
This line has trailing spaces; please remove them.\
""",
'--',
'@{{PATHSFILE}}'
]

[[linter]]
code = 'TABS'
include_patterns = ['**']
exclude_patterns = [
'**/*.svg',
'**/*Makefile',
'**/.gitattributes',
'**/.gitmodules',
'.lintrunner.toml',
]
command = [
'python3',
'tools/linter/adapters/grep_linter.py',
# @lint-ignore TXT2
'--pattern= ',
'--linter-name=TABS',
'--error-name=saw some tabs',
'--replace-pattern=s/\t/ /',
"""--error-description=\
This line has tabs; please replace them with spaces.\
""",
'--',
'@{{PATHSFILE}}'
]

[[linter]]
code = 'PYPIDEP'
include_patterns = ['.github/**']
exclude_patterns = [
'**/*.rst',
'**/*.py',
'**/*.md',
'**/*.diff',
]
command = [
'python3',
'tools/linter/adapters/grep_linter.py',
"""--pattern=\
(pip|pip3|python -m pip|python3 -m pip|python3 -mpip|python -mpip) \
install ([a-zA-Z0-9][A-Za-z0-9\\._\\-]+)([^/=<>~!]+)[A-Za-z0-9\\._\\-\\*\\+\\!]*$\
""",
'--linter-name=PYPIDEP',
'--error-name=unpinned PyPI install',
"""--error-description=\
This line has unpinned PyPi installs; \
please pin them to a specific version: e.g. 'thepackage==1.2'\
""",
'--',
'@{{PATHSFILE}}'
]

[[linter]]
code = 'EXEC'
include_patterns = ['**']
exclude_patterns = [
'third_party/**',
'torch/bin/**',
'**/*.so',
'**/*.py',
'**/*.sh',
'**/*.bash',
'**/git-pre-commit',
'**/git-clang-format',
'**/gradlew',
]
command = [
'python3',
'tools/linter/adapters/exec_linter.py',
'--',
'@{{PATHSFILE}}',
]

[[linter]]
code = 'ACTIONLINT'
include_patterns = [
'.github/workflows/*.yml',
'.github/workflows/*.yaml',
# actionlint does not support composite actions yet
# '.github/actions/**/*.yml',
# '.github/actions/**/*.yaml',
]
command = [
'python3',
'tools/linter/adapters/actionlint_linter.py',
'--binary=.lintbin/actionlint',
'--',
'@{{PATHSFILE}}',
]
init_command = [
'python3',
'tools/linter/adapters/s3_init.py',
'--config-json=tools/linter/adapters/s3_init_config.json',
'--linter=actionlint',
'--dry-run={{DRYRUN}}',
'--output-dir=.lintbin',
'--output-name=actionlint',
]

# Black + usort
[[linter]]
code = 'UFMT'
include_patterns = [
'**/*.py',
]
command = [
'python3',
'tools/linter/adapters/ufmt_linter.py',
'--',
'@{{PATHSFILE}}'
]
exclude_patterns = [
]
init_command = [
'python3',
'tools/linter/adapters/pip_init.py',
'--dry-run={{DRYRUN}}',
'--no-black-binary',
'black==23.3.0',
'ufmt==2.1.0',
'usort==1.0.6',
]
is_formatter = true

[[linter]]
code = 'COPYRIGHT'
include_patterns = ['**']
exclude_patterns = ['.lintrunner.toml']
command = [
'python3',
'tools/linter/adapters/grep_linter.py',
'--pattern=Confidential and proprietary',
'--linter-name=COPYRIGHT',
'--error-name=Confidential Code',
"""--error-description=\
Proprietary and confidential source code\
should not be contributed to PyTorch/test-infra codebase\
""",
'--',
'@{{PATHSFILE}}'
]

[[linter]]
code = 'LINTRUNNER_VERSION'
include_patterns = ['**']
command = [
'python3',
'tools/linter/adapters/lintrunner_version_linter.py'
]

[[linter]]
code = 'RUFF'
include_patterns = ['**/*.py']
exclude_patterns = [
]
command = [
'python3',
'tools/linter/adapters/ruff_linter.py',
'--config=pyproject.toml',
'--show-disable',
'--',
'@{{PATHSFILE}}'
]
init_command = [
'python3',
'tools/linter/adapters/pip_init.py',
'--dry-run={{DRYRUN}}',
'ruff==0.0.269',
]
is_formatter = true
70 changes: 70 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[build-system]
requires = [
"setuptools",
"wheel",
"astunparse",
"numpy",
"ninja",
"pyyaml",
"setuptools",
"cmake",
"typing-extensions",
"requests",
]
# Use legacy backend to import local packages in setup.py
build-backend = "setuptools.build_meta:__legacy__"


[tool.black]
# Uncomment if pyproject.toml worked fine to ensure consistency with flake8
# line-length = 120
target-version = ["py38", "py39", "py310", "py311"]


[tool.ruff]
target-version = "py38"

# NOTE: Synchoronize the ignores with .flake8
ignore = [
# these ignores are from flake8-bugbear; please fix!
"B007", "B008", "B017",
"B018", # Useless expression
"B019", "B020",
"B023", "B024", "B026",
"B028", # No explicit `stacklevel` keyword argument found
"B904", "B905",
"E402",
"C408", # C408 ignored because we like the dict keyword argument syntax
"E501", # E501 is not flexible enough, we're using B950 instead
"E721",
"E731", # Assign lambda expression
"E741",
"EXE001",
"F405",
"F821",
"F841",
# these ignores are from flake8-logging-format; please fix!
"G101", "G201", "G202",
"SIM102", "SIM103", "SIM112", # flake8-simplify code styles
"SIM105", # these ignores are from flake8-simplify. please fix or ignore with commented reason
"SIM108",
"SIM110",
"SIM114", # Combine `if` branches using logical `or` operator
"SIM115",
"SIM116", # Disable Use a dictionary instead of consecutive `if` statements
"SIM117",
"SIM118",
]
line-length = 120
select = [
"B",
"C4",
"G",
"E",
"F",
"SIM1",
"W",
# Not included in flake8
"PLE",
"TRY302",
]
Loading

0 comments on commit 5115d8a

Please sign in to comment.