Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable pylint checks with Ruff and remove pylint from lintrunner #5589

Merged
merged 7 commits into from
Sep 13, 2023
Merged
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
36 changes: 0 additions & 36 deletions .lintrunner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -182,42 +182,6 @@ init_command = [
]
is_formatter = true

[[linter]]
code = 'PYLINT'
include_patterns = [
'onnx/**/*.py',
'tools/**/*.py',
]
exclude_patterns = [
'onnx/backend/test/**',
'onnx/backend/sample/ops/abs.py', # redefined-builtin
'onnx/checker.py',
'tools/protoc-gen-mypy.py',
'onnx/test/basic_test.py',
'onnx/test/parser_test.py',
'onnx/test/version_converter_test.py',
]
command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'pylint_linter',
'--rcfile=pyproject_pylint.toml',
'--show-disable',
'--',
'@{{PATHSFILE}}'
]
init_command = [
'python',
'-m',
'lintrunner_adapters',
'run',
'pip_init',
'--dry-run={{DRYRUN}}',
'--requirement=requirements-lintrunner.txt',
]

[[linter]]
code = 'EDITORCONFIG-CHECKER'
include_patterns=[
Expand Down
6 changes: 1 addition & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"[python]": {
"editor.tabSize": 4,
// Auto sort imports
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
jcwchen marked this conversation as resolved.
Show resolved Hide resolved
},
"python.formatting.provider": "black",
"python.sortImports.args": [
"--profile",
"black"
],
}
}
2 changes: 1 addition & 1 deletion docs/docsgen/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

# pylint: disable=W0622

# type: ignore
import os
import sys
Expand Down
2 changes: 1 addition & 1 deletion docs/docsgen/source/intro/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ neighbors.
set_model_props(onnx_model, {})

# opsets
del onnx_model.opset_import[:] # pylint: disable=E1101
del onnx_model.opset_import[:]
for dom, value in opsets.items():
op_set = onnx_model.opset_import.add()
op_set.domain = dom
Expand Down
6 changes: 3 additions & 3 deletions docs/docsgen/source/onnx_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def _populate_all_schemas_with_history():


def _get_all_schemas_with_history():
global _all_schemas_with_history # pylint: disable=global-statement
global _all_schemas_with_history
if _all_schemas_with_history is None:
_all_schemas_with_history = _populate_all_schemas_with_history()
return _all_schemas_with_history
Expand Down Expand Up @@ -677,7 +677,7 @@ def get_onnx_example(op_name, domain):
found = textwrap.dedent(found)
lines = found.split("\n")
first = 0
for i in range(len(lines)): # pylint: disable=C0200
for i in range(len(lines)):
if lines[i].startswith("def "):
first = i + 1
found = textwrap.dedent("\n".join(lines[first:]))
Expand Down Expand Up @@ -761,7 +761,7 @@ def render(self, indent=""):
table_dom.append(f" - {col2}")
table_dom.append("")
if indent != "":
for i in range(len(table_dom)): # pylint: disable=C0200
for i in range(len(table_dom)):
table_dom[i] = indent + table_dom[i]
res = "\n".join(table_dom)
return res
Expand Down
12 changes: 6 additions & 6 deletions onnx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def _get_serializer(

def load_model(
f: IO[bytes] | str | os.PathLike,
format: _SupportedFormat | None = None, # pylint: disable=redefined-builtin
format: _SupportedFormat | None = None,
load_external_data: bool = True,
) -> ModelProto:
"""Loads a serialized ModelProto into memory.
Expand Down Expand Up @@ -218,7 +218,7 @@ def load_model(

def load_tensor(
f: IO[bytes] | str | os.PathLike,
format: _SupportedFormat | None = None, # pylint: disable=redefined-builtin
format: _SupportedFormat | None = None,
) -> TensorProto:
"""Loads a serialized TensorProto into memory.

Expand All @@ -237,7 +237,7 @@ def load_tensor(

def load_model_from_string(
s: bytes | str,
format: _SupportedFormat = _DEFAULT_FORMAT, # pylint: disable=redefined-builtin
format: _SupportedFormat = _DEFAULT_FORMAT,
) -> ModelProto:
"""Loads a binary string (bytes) that contains serialized ModelProto.

Expand All @@ -256,7 +256,7 @@ def load_model_from_string(

def load_tensor_from_string(
s: bytes,
format: _SupportedFormat = _DEFAULT_FORMAT, # pylint: disable=redefined-builtin
format: _SupportedFormat = _DEFAULT_FORMAT,
) -> TensorProto:
"""Loads a binary string (bytes) that contains serialized TensorProto.

Expand All @@ -276,7 +276,7 @@ def load_tensor_from_string(
def save_model(
proto: ModelProto | bytes,
f: IO[bytes] | str | os.PathLike,
format: _SupportedFormat | None = None, # pylint: disable=redefined-builtin
format: _SupportedFormat | None = None,
*,
save_as_external_data: bool = False,
all_tensors_to_one_file: bool = True,
Expand Down Expand Up @@ -330,7 +330,7 @@ def save_model(
def save_tensor(
proto: TensorProto,
f: IO[bytes] | str | os.PathLike,
format: _SupportedFormat | None = None, # pylint: disable=redefined-builtin
format: _SupportedFormat | None = None,
) -> None:
"""
Saves the TensorProto to the specified path.
Expand Down
2 changes: 1 addition & 1 deletion onnx/backend/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0
# pylint: disable=W0613


from collections import namedtuple
from typing import Any, Dict, NewType, Optional, Sequence, Tuple, Type
Expand Down
2 changes: 1 addition & 1 deletion onnx/backend/test/case/node/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def merge(
def collect_testcases(op_type: str) -> List[TestCase]:
"""Collect node test cases"""
# only keep those tests related to this operator
global _TargetOpType
global _TargetOpType # noqa: PLW0603
_TargetOpType = op_type

import_recursive(sys.modules[__name__])
Expand Down
1 change: 0 additions & 1 deletion onnx/backend/test/case/node/image_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def _generate_test_data(
tile_sz: int = 5,
) -> tuple[np.ndarray, np.ndarray]:
try:
# pylint: disable=import-outside-toplevel
import PIL.Image
except ImportError:
# Since pillow is not installed to generate test data for the ImageDecoder operator
Expand Down
2 changes: 1 addition & 1 deletion onnx/backend/test/report/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def report_csv(
self, all_ops: List[str], passed: List[Optional[str]], experimental: List[str]
) -> None:
for schema in _all_schemas:
if schema.domain == "" or schema.domain == "ai.onnx":
if schema.domain in {"", "ai.onnx"}:
all_ops.append(schema.name)
if schema.support_level == defs.OpSchema.SupportType.EXPERIMENTAL:
experimental.append(schema.name)
Expand Down
6 changes: 3 additions & 3 deletions onnx/backend/test/stat_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def gen_outlines(f: IO[Any], ml: bool) -> None:
def gen_node_test_coverage(
schemas: Sequence[defs.OpSchema], f: IO[Any], ml: bool
) -> None:
global common_covered
global experimental_covered
global common_covered # noqa: PLW0603
global experimental_covered # noqa: PLW0603
generators = set(
{
"Multinomial",
Expand Down Expand Up @@ -136,7 +136,7 @@ def gen_node_test_coverage(
f.write(f"<summary>{summary}</summary>\n\n")
f.write(f"```python\n{code}\n```\n\n")
f.write("</details>\n")
else:
else: # noqa: PLR5501
if s in generators:
f.write(" (random generator operator)\n")
else:
Expand Down
7 changes: 3 additions & 4 deletions onnx/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#
# SPDX-License-Identifier: Apache-2.0

# pylint: disable=unidiomatic-typecheck

from typing import Dict, List, MutableMapping, Optional, Set, Tuple

Expand Down Expand Up @@ -78,7 +77,7 @@ def _edge_names(graph: GraphProto, exclude: Optional[Set[str]] = None) -> List[s
return result


def merge_graphs( # pylint: disable=too-many-branches,too-many-statements
def merge_graphs(
g1: GraphProto,
g2: GraphProto,
io_map: List[Tuple[str, str]],
Expand Down Expand Up @@ -265,7 +264,7 @@ def merge_graphs( # pylint: disable=too-many-branches,too-many-statements
return g


def merge_models( # pylint: disable=too-many-branches
def merge_models(
m1: ModelProto,
m2: ModelProto,
io_map: List[Tuple[str, str]],
Expand Down Expand Up @@ -411,7 +410,7 @@ def merge_models( # pylint: disable=too-many-branches
return model


def add_prefix_graph( # pylint: disable=too-many-branches
def add_prefix_graph(
graph: GraphProto,
prefix: str,
rename_nodes: Optional[bool] = True,
Expand Down
4 changes: 2 additions & 2 deletions onnx/defs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def onnx_opset_version() -> int:
@property # type: ignore
def _function_proto(self): # type: ignore
func_proto = FunctionProto()
func_proto.ParseFromString(self._function_body) # pylint: disable=protected-access
func_proto.ParseFromString(self._function_body)
return func_proto


Expand All @@ -55,7 +55,7 @@ def _function_proto(self): # type: ignore
@property # type: ignore
def _attribute_default_value(self): # type: ignore
attr = AttributeProto()
attr.ParseFromString(self._default_value) # pylint: disable=protected-access
attr.ParseFromString(self._default_value)
return attr


Expand Down
12 changes: 6 additions & 6 deletions onnx/defs/gen_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def generate_formal_parameter_tags(formal_parameter: OpSchema.FormalParameter) -
return "" if len(tags) == 0 else " (" + ", ".join(tags) + ")"


def display_schema( # pylint: disable=too-many-branches,too-many-statements
def display_schema(
schema: OpSchema, versions: Sequence[OpSchema], changelog: str
) -> str:
s = ""
Expand All @@ -123,7 +123,7 @@ def display_schema( # pylint: disable=too-many-branches,too-many-statements
s += f" of {display_domain(schema.domain)}.\n"
if len(versions) > 1:
# TODO: link to the Changelog.md
s += "\nOther versions of this operator: {}\n".format( # pylint: disable=consider-using-f-string
s += "\nOther versions of this operator: {}\n".format(
", ".join(
display_version_link(
format_name_with_domain(v.domain, v.name),
Expand Down Expand Up @@ -155,7 +155,7 @@ def format_value(value: Any) -> str:
if isinstance(value, float):
formatted = str(np.round(value, 5))
# use default formatting, unless too long.
if len(formatted) > 10:
if len(formatted) > 10: # noqa: PLR2004
formatted = str(f"({value:e})")
return formatted
if isinstance(value, (bytes, bytearray)):
Expand Down Expand Up @@ -234,7 +234,7 @@ class Args(NamedTuple):
changelog: str


def main(args: Args) -> None: # pylint: disable=too-many-branches,too-many-statements
def main(args: Args) -> None:
base_dir = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
)
Expand Down Expand Up @@ -346,7 +346,7 @@ def main(args: Args) -> None: # pylint: disable=too-many-branches,too-many-stat
function_versions = schema.all_function_opset_versions # type: ignore
function_ops.append((n, schema, versions, function_versions))
continue
s = '|{}<a href="#{}">{}</a>{}|{}|\n'.format( # pylint: disable=consider-using-f-string
s = '|{}<a href="#{}">{}</a>{}|{}|\n'.format(
support_level_str(schema.support_level),
format_name_with_domain(domain, n),
format_name_with_domain(domain, n),
Expand All @@ -357,7 +357,7 @@ def main(args: Args) -> None: # pylint: disable=too-many-branches,too-many-stat
if function_ops:
fout.write("|**Function**|**Since version**|**Function version**|\n")
for n, schema, versions, function_versions in function_ops:
s = '|{}<a href="#{}">{}</a>|{}|{}|\n'.format( # pylint: disable=consider-using-f-string
s = '|{}<a href="#{}">{}</a>|{}|{}|\n'.format(
support_level_str(schema.support_level),
format_name_with_domain(domain, n),
format_name_with_domain(domain, n),
Expand Down
14 changes: 7 additions & 7 deletions onnx/gen_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ def process_ifs(lines: Iterable[str], onnx_ml: bool) -> Iterable[str]:
assert in_if == 1
in_if = 2
elif ENDIF_ONNX_ML_REGEX.match(line):
assert in_if == 1 or in_if == 2 # pylint: disable=consider-using-in
assert in_if == 1 or in_if == 2 # noqa: PLR1714, PLR2004
in_if = 0
else:
else: # noqa: PLR5501
if in_if == 0:
yield line
elif in_if == 1 and onnx_ml:
yield line
elif in_if == 2 and not onnx_ml:
elif in_if == 2 and not onnx_ml: # noqa: PLR2004
yield line


Expand Down Expand Up @@ -114,10 +114,10 @@ def translate(source: str, proto: int, onnx_ml: bool, package_name: str) -> str:
lines: Iterable[str] = source.splitlines()
lines = process_ifs(lines, onnx_ml=onnx_ml)
lines = process_package_name(lines, package_name=package_name)
if proto == 3:
if proto == 3: # noqa: PLR2004
lines = convert_to_proto3(lines)
else:
assert proto == 2
assert proto == 2 # noqa: PLR2004
return "\n".join(lines) # TODO: not Windows friendly


Expand All @@ -127,7 +127,7 @@ def qualify(f: str, pardir: Optional[str] = None) -> str:
return os.path.join(pardir, f)


def convert( # pylint: disable=too-many-branches,too-many-statements
def convert(
stem: str,
package_name: str,
output: str,
Expand Down Expand Up @@ -194,7 +194,7 @@ def convert( # pylint: disable=too-many-branches,too-many-statements
pb_py = qualify(f"{stem.replace('-', '_')}_pb.py", pardir=output)
if need_rename:
pb2_py = qualify(f"{proto_base.replace('-', '_')}_pb2.py", pardir=output)
else:
else: # noqa: PLR5501
if do_onnx_ml:
pb2_py = qualify(f"{stem.replace('-', '_')}_ml_pb2.py", pardir=output)
else:
Expand Down
Loading