Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .lintrunner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@ command = [
]
is_formatter = true

[[linter]]
code = 'FLAKE8_COMMENT'
include_patterns = ['**/*.py', '**/*.pyi']
exclude_patterns = [
'torch/onnx/_internal/diagnostics/infra/sarif/**'
]
command = [
'python3',
'tools/linter/adapters/flake8_comment_linter.py',
'--',
'@{{PATHSFILE}}',
]
is_formatter = true

[[linter]]
code = 'SPACES'
include_patterns = ['**']
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/dynamo/huggingface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env python3
# Disable the flake warnings for the imports. Flake8 does not provide a way to
# disable just warning for the entire file. Disabling flake8 entirely.
# flake8: noqa

import importlib
import logging
Expand Down Expand Up @@ -46,9 +49,6 @@ def pip_install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])


# Disable the flake warnings for the imports. Flake8 does not provide a way to
# disable just warning for the entire file. Disabling flake8 entirely.
# flake8: noqa
imports = [
"AlbertForPreTraining",
"AutoConfig",
Expand Down
1 change: 0 additions & 1 deletion functorch/examples/dp_cifar10/cifar10_opacus.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def test(args, model, test_loader, device):
return np.mean(top1_acc)


# flake8: noqa: C901
def main():
args = parse_args()

Expand Down
1 change: 0 additions & 1 deletion functorch/examples/dp_cifar10/cifar10_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ def test(args, model, test_loader, device):
return np.mean(top1_acc)


# flake8: noqa: C901
def main():
args = parse_args()

Expand Down
9 changes: 4 additions & 5 deletions test/dynamo/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Owner(s): ["module: dynamo"]
# flake8: noqa
# ruff: noqa: F841
import abc
import collections
Expand Down Expand Up @@ -2772,7 +2773,7 @@ def test_dtypes_no_graphbreaks(self):
"int",
np.intp,
np.int32,
np.uint8
np.uint8,
# np.dtype('int') # XXX: as above
]

Expand Down Expand Up @@ -7578,7 +7579,6 @@ def fn():
@unittest.skipIf(sys.version_info >= (3, 13), "feature landed in 3.13")
def test_get_instruction_source_311(self):
def f():
# flake8: noqa
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kit1980 I was going to fix this one by trying to disable something locally, but if I remove flake8: noqa for the whole file there are around 100 errors. I'd be happy to add in a few # noqa: specific errors to just ignore these, but it seems like a lot of shuffling code around. Which would you prefer I do: move the flake8: noqa to the top with a comment in this specific case, or add in specific # noqa: XXX around the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think I should just move the flake8: noqa to the top without a comment, that's what's in the latest commit I just pushed. If you think that's fine then I'd love for you to take a look!

# fmt: off
# test binary ops
a = ( b ) + c
Expand All @@ -7593,7 +7593,7 @@ def f():
<< (

c # test
\

) # test
)

Expand Down Expand Up @@ -8091,8 +8091,7 @@ def run_fn():

def test_torch_compile_ctx_on_forward_and_training_step(self):
class MyModel(torch.nn.Module):
def forward(self):
...
def forward(self): ...

def training_step(self):
self()
Expand Down
3 changes: 1 addition & 2 deletions test/functorch/test_aotdispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5750,8 +5750,7 @@ def fn(x):

class GradsNoForceContiguousContextManager(ContextDecorator):
def __enter__(self):
# flake8: noqa: TOR901
self.lib = torch.library.Library("_mylib", "FRAGMENT")
self.lib = torch.library.Library("_mylib", "FRAGMENT") # noqa: TOR901
self.d = {
torch.channels_last: 0,
torch.contiguous_format: 0,
Expand Down
112 changes: 112 additions & 0 deletions tools/linter/adapters/flake8_comment_linter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""
FLAKE8_COMMENT: Checks files to make sure # flake8: noqa is only at the top of files
"""

from __future__ import annotations

import argparse
import json
import logging
import re
import sys
import tokenize
from enum import Enum
from io import StringIO
from typing import NamedTuple


LINTER_CODE = "FLAKE8_COMMENT"


class LintSeverity(str, Enum):
ERROR = "error"
WARNING = "warning"
ADVICE = "advice"
DISABLED = "disabled"


class LintMessage(NamedTuple):
path: str | None
line: int | None
char: int | None
code: str
severity: LintSeverity
name: str
original: str | None
replacement: str | None
description: str | None


def check_file(filename: str) -> LintMessage | None:
logging.debug("Checking file %s", filename)

pattern = r"^# flake8:\s*noqa"
is_start_of_file = True

with open(filename, encoding="utf-8") as f:
original = f.read()

for token in tokenize.generate_tokens(StringIO(original).readline):
if (
token.type != tokenize.COMMENT and token.type != tokenize.NL
) and is_start_of_file:
is_start_of_file = False

if (
token.type == tokenize.COMMENT
and not is_start_of_file
and re.search(pattern, token.string)
):
replacement_lines = original.splitlines()
replacement_lines[token.start[0] - 1] = ""
replacement_lines.insert(0, "# flake8: noqa")
return LintMessage(
path=filename,
line=None,
char=None,
code=LINTER_CODE,
severity=LintSeverity.ERROR,
name="mid-file '# flake8: noqa'",
original=original,
replacement="\n".join(replacement_lines),
description="'# flake8: noqa' in the middle of the file ",
)
return None


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="native functions linter",
fromfile_prefix_chars="@",
)
parser.add_argument(
"--verbose",
action="store_true",
help="location of native_functions.yaml",
)
parser.add_argument(
"filenames",
nargs="+",
help="paths to lint",
)

args = parser.parse_args()

logging.basicConfig(
format="<%(threadName)s:%(levelname)s> %(message)s",
level=logging.NOTSET
if args.verbose
else logging.DEBUG
if len(args.filenames) < 1000
else logging.INFO,
stream=sys.stderr,
)

lint_messages = []
for filename in args.filenames:
lint_message = check_file(filename)
if lint_message is not None:
lint_messages.append(lint_message)

for lint_message in lint_messages:
print(json.dumps(lint_message._asdict()), flush=True)
3 changes: 2 additions & 1 deletion torch/_logging/_internal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# mypy: allow-untyped-defs

import functools
import hashlib
import importlib.util
Expand Down Expand Up @@ -655,7 +656,7 @@ def pad_to(s, length=30):

TORCH_LOGS_OUT=/tmp/output.txt will output the logs to /tmp/output.txt as
well. This is useful when the output is long.
""" # flake8: noqa: B950
"""
msg = f"""
TORCH_LOGS Info
{examples}
Expand Down
2 changes: 1 addition & 1 deletion torch/ao/quantization/quantize_pt2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def convert_pt2e(
# for detailed explanation of output quantized model
quantized_model = convert_pt2e(prepared_model)

""" # flake8: noqa
"""
torch._C._log_api_usage_once("quantization_api.quantize_pt2e.convert_pt2e")
if not isinstance(use_reference_representation, bool):
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion torch/jit/_shape_functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# flake8: noqa
# mypy: allow-untyped-defs
import math
from typing import Any, Callable, Dict, List, Optional, Tuple, Union


number = Union[int, float]
# flake8: noqa

###
# There are generated files that depend on this file
Expand Down
2 changes: 1 addition & 1 deletion torch/onnx/_internal/diagnostics/_rules.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# mypy: allow-untyped-defs
# flake8: noqa
"""
GENERATED CODE - DO NOT EDIT DIRECTLY
This file is generated by gen_diagnostics.py.
Expand All @@ -10,7 +11,6 @@
import dataclasses
from typing import Tuple

# flake8: noqa
from torch.onnx._internal.diagnostics import infra


Expand Down
2 changes: 1 addition & 1 deletion torch/onnx/_internal/diagnostics/infra/sarif/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# flake8: noqa
from typing import Final


SARIF_VERSION: Final = "2.1.0"
SARIF_SCHEMA_LINK: Final = "https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/schemas/sarif-schema-2.1.0.json"
# flake8: noqa
4 changes: 2 additions & 2 deletions torch/onnx/_internal/exporter/_analysis.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Compatibility analyzer for PyTorch models."""

# mypy: allow-untyped-defs
# flake8: noqa: B950 We do not need flake8 as it complains line length
"""Compatibility analyzer for PyTorch models."""

from __future__ import annotations

import dataclasses
Expand Down
Loading