Skip to content

Commit

Permalink
Enable additional ruff rules (#6032)
Browse files Browse the repository at this point in the history
Enable additional ruff rules and apply fixes

---------

Signed-off-by: Justin Chu <justinchu@microsoft.com>
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
justinchuby committed Apr 16, 2024
1 parent 16ee58b commit c459890
Show file tree
Hide file tree
Showing 516 changed files with 1,131 additions and 885 deletions.
7 changes: 4 additions & 3 deletions docs/Operators.md
Expand Up @@ -277,6 +277,7 @@ expect(node, inputs=[x], outputs=[y], name="test_abs")

```python
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import numpy as np

Expand Down Expand Up @@ -5266,7 +5267,7 @@ Other versions of this operator: <a href="Changelog.md#Concat-1">1</a>, <a href=
<summary>concat</summary>

```python
test_cases: Dict[str, Sequence[Any]] = {
test_cases: dict[str, Sequence[Any]] = {
"1d": ([1, 2], [3, 4]),
"2d": ([[1, 2], [3, 4]], [[5, 6], [7, 8]]),
"3d": (
Expand Down Expand Up @@ -14481,7 +14482,7 @@ node = onnx.helper.make_node(
)

trip_count = np.array(5).astype(np.int64)
seq_empty: List[Any] = []
seq_empty: list[Any] = []
seq_res = [x[: int(i)] for i in x]
cond = np.array(1).astype(bool)
expect(
Expand Down Expand Up @@ -14678,7 +14679,7 @@ node = onnx.helper.make_node(
trip_count = np.array(5).astype(np.int64)
cond = np.array(1).astype(bool)
seq_res = compute_loop_outputs(x, [x0], trip_count)
opt_seq_in: List[Any] = [x0]
opt_seq_in: list[Any] = [x0]
expect(
node,
inputs=[trip_count, cond, opt_seq_in],
Expand Down
6 changes: 3 additions & 3 deletions docs/TestCoverage.md
Expand Up @@ -3773,7 +3773,7 @@ There are 1 test cases, listed as following:
<summary>concat</summary>

```python
test_cases: Dict[str, Sequence[Any]] = {
test_cases: dict[str, Sequence[Any]] = {
"1d": ([1, 2], [3, 4]),
"2d": ([[1, 2], [3, 4]], [[5, 6], [7, 8]]),
"3d": (
Expand Down Expand Up @@ -9665,7 +9665,7 @@ node = onnx.helper.make_node(
)

trip_count = np.array(5).astype(np.int64)
seq_empty: List[Any] = []
seq_empty: list[Any] = []
seq_res = [x[: int(i)] for i in x]
cond = np.array(1).astype(bool)
expect(
Expand Down Expand Up @@ -9860,7 +9860,7 @@ node = onnx.helper.make_node(
trip_count = np.array(5).astype(np.int64)
cond = np.array(1).astype(bool)
seq_res = compute_loop_outputs(x, [x0], trip_count)
opt_seq_in: List[Any] = [x0]
opt_seq_in: list[Any] = [x0]
expect(
node,
inputs=[trip_count, cond, opt_seq_in],
Expand Down
4 changes: 3 additions & 1 deletion onnx/__init__.py
Expand Up @@ -134,7 +134,9 @@
# Supported model formats that can be loaded from and saved to
# The literals are formats with built-in support. But we also allow users to
# register their own formats. So we allow str as well.
_SupportedFormat = Union[Literal["protobuf", "textproto", "onnxtxt", "json"], str]
_SupportedFormat = Union[
Literal["protobuf", "textproto", "onnxtxt", "json"], str # noqa: PYI051
]
# Default serialization format
_DEFAULT_FORMAT = "protobuf"

Expand Down
32 changes: 17 additions & 15 deletions onnx/backend/base.py
@@ -1,10 +1,10 @@
# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from collections import namedtuple
from typing import Any, Dict, NewType, Optional, Sequence, Tuple, Type
from typing import Any, NewType, Sequence

import numpy

Expand Down Expand Up @@ -37,11 +37,11 @@ def __init__(self, device: str) -> None:

def namedtupledict(
typename: str, field_names: Sequence[str], *args: Any, **kwargs: Any
) -> Type[Tuple[Any, ...]]:
) -> type[tuple[Any, ...]]:
field_names_map = {n: i for i, n in enumerate(field_names)}
# Some output names are invalid python identifier, e.g. "0"
kwargs.setdefault("rename", True)
data = namedtuple(typename, field_names, *args, **kwargs) # type: ignore
data = namedtuple(typename, field_names, *args, **kwargs) # type: ignore # noqa: PYI024

def getitem(self: Any, key: Any) -> Any:
if isinstance(key, str):
Expand All @@ -58,7 +58,7 @@ class BackendRep:
BackendRep to retrieve the corresponding results.
"""

def run(self, inputs: Any, **kwargs: Any) -> Tuple[Any, ...]:
def run(self, inputs: Any, **kwargs: Any) -> tuple[Any, ...]: # noqa: ARG002
"""Abstract function."""
return (None,)

Expand All @@ -76,23 +76,23 @@ class Backend:

@classmethod
def is_compatible(
cls, model: ModelProto, device: str = "CPU", **kwargs: Any
cls, model: ModelProto, device: str = "CPU", **kwargs: Any # noqa: ARG003
) -> bool:
# Return whether the model is compatible with the backend.
return True

@classmethod
def prepare(
cls, model: ModelProto, device: str = "CPU", **kwargs: Any
) -> Optional[BackendRep]:
cls, model: ModelProto, device: str = "CPU", **kwargs: Any # noqa: ARG003
) -> BackendRep | None:
# TODO Remove Optional from return type
onnx.checker.check_model(model)
return None

@classmethod
def run_model(
cls, model: ModelProto, inputs: Any, device: str = "CPU", **kwargs: Any
) -> Tuple[Any, ...]:
) -> tuple[Any, ...]:
backend = cls.prepare(model, device, **kwargs)
assert backend is not None
return backend.run(inputs)
Expand All @@ -101,11 +101,13 @@ def run_model(
def run_node(
cls,
node: NodeProto,
inputs: Any,
device: str = "CPU",
outputs_info: Optional[Sequence[Tuple[numpy.dtype, Tuple[int, ...]]]] = None,
**kwargs: Dict[str, Any],
) -> Optional[Tuple[Any, ...]]:
inputs: Any, # noqa: ARG003
device: str = "CPU", # noqa: ARG003
outputs_info: ( # noqa: ARG003
Sequence[tuple[numpy.dtype, tuple[int, ...]]] | None
) = None,
**kwargs: dict[str, Any],
) -> tuple[Any, ...] | None:
"""Simple run one operator and return the results.
Args:
Expand All @@ -130,7 +132,7 @@ def run_node(
return None

@classmethod
def supports_device(cls, device: str) -> bool:
def supports_device(cls, device: str) -> bool: # noqa: ARG003
"""Checks whether the backend is compiled with particular device support.
In particular it's used in the testing suite.
"""
Expand Down
8 changes: 4 additions & 4 deletions onnx/backend/sample/ops/__init__.py
@@ -1,22 +1,22 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import importlib
import inspect
import pkgutil
import sys
from types import ModuleType
from typing import Dict


def collect_sample_implementations() -> Dict[str, str]:
dict_: Dict[str, str] = {}
def collect_sample_implementations() -> dict[str, str]:
dict_: dict[str, str] = {}
_recursive_scan(sys.modules[__name__], dict_)
return dict_


def _recursive_scan(package: ModuleType, dict_: Dict[str, str]) -> None:
def _recursive_scan(package: ModuleType, dict_: dict[str, str]) -> None:
pkg_dir = package.__path__ # type: ignore
module_location = package.__name__
for _module_loader, name, ispkg in pkgutil.iter_modules(pkg_dir): # type: ignore
Expand Down
1 change: 1 addition & 0 deletions onnx/backend/sample/ops/abs.py
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import numpy as np

Expand Down
1 change: 1 addition & 0 deletions onnx/backend/test/__init__.py
@@ -1,6 +1,7 @@
# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

__all__ = ["BackendTest"]
# for backward compatibility
Expand Down
4 changes: 2 additions & 2 deletions onnx/backend/test/case/__init__.py
@@ -1,14 +1,14 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import sys
from typing import Dict, List, Tuple

from onnx.backend.test.case.base import Snippets
from onnx.backend.test.case.utils import import_recursive


def collect_snippets() -> Dict[str, List[Tuple[str, str]]]:
def collect_snippets() -> dict[str, list[tuple[str, str]]]:
import_recursive(sys.modules[__name__])
return Snippets
11 changes: 6 additions & 5 deletions onnx/backend/test/case/base.py
@@ -1,16 +1,17 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import inspect
from collections import defaultdict
from textwrap import dedent
from typing import Any, ClassVar, Dict, List, Tuple, Type
from typing import Any, ClassVar

import numpy as np


def process_snippet(op_name: str, name: str, export: Any) -> Tuple[str, str]:
def process_snippet(op_name: str, name: str, export: Any) -> tuple[str, str]:
snippet_name = name[len("export_") :] or op_name.lower()
source_code = dedent(inspect.getsource(export))
# remove the function signature line
Expand All @@ -20,14 +21,14 @@ def process_snippet(op_name: str, name: str, export: Any) -> Tuple[str, str]:
return snippet_name, dedent("\n".join(lines[2:]))


Snippets: Dict[str, List[Tuple[str, str]]] = defaultdict(list)
Snippets: dict[str, list[tuple[str, str]]] = defaultdict(list)


class _Exporter(type):
exports: ClassVar[Dict[str, List[Tuple[str, str]]]] = defaultdict(list)
exports: ClassVar[dict[str, list[tuple[str, str]]]] = defaultdict(list)

def __init__(
cls, name: str, bases: Tuple[Type[Any], ...], dct: Dict[str, Any]
cls, name: str, bases: tuple[type[Any], ...], dct: dict[str, Any]
) -> None:
for k, v in dct.items():
if k.startswith("export"):
Expand Down
7 changes: 4 additions & 3 deletions onnx/backend/test/case/model/__init__.py
@@ -1,9 +1,10 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import sys
from typing import List, Optional, Sequence
from typing import Sequence

import numpy as np

Expand All @@ -18,7 +19,7 @@ def expect(
model: ModelProto,
inputs: Sequence[np.ndarray],
outputs: Sequence[np.ndarray],
name: Optional[str] = None,
name: str | None = None,
) -> None:
name = name or model.graph.name
_SimpleModelTestCases.append(
Expand All @@ -40,7 +41,7 @@ def expect(
BASE_URL = "onnx/backend/test/data/light/light_%s.onnx"


def collect_testcases() -> List[TestCase]:
def collect_testcases() -> list[TestCase]:
"""Collect model test cases defined in python/numpy code."""
real_model_testcases = []

Expand Down
1 change: 1 addition & 0 deletions onnx/backend/test/case/model/expand.py
@@ -1,6 +1,7 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

from typing import Sequence

Expand Down
1 change: 1 addition & 0 deletions onnx/backend/test/case/model/gradient.py
@@ -1,6 +1,7 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import numpy as np

Expand Down
1 change: 1 addition & 0 deletions onnx/backend/test/case/model/shrink.py
@@ -1,6 +1,7 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import numpy as np

Expand Down
1 change: 1 addition & 0 deletions onnx/backend/test/case/model/sign.py
@@ -1,6 +1,7 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import numpy as np

Expand Down
1 change: 1 addition & 0 deletions onnx/backend/test/case/model/single-relu.py
@@ -1,6 +1,7 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion onnx/backend/test/case/model/stringnormalizer.py
@@ -1,7 +1,7 @@
# Copyright (c) ONNX Project Contributors

# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from typing import Sequence

Expand Down

0 comments on commit c459890

Please sign in to comment.