From 8769fb854d816d223cfb513979e97e23a93bddee Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 9 May 2023 17:24:14 +0000 Subject: [PATCH] [BE] Fix flake8 B027 errors - missing abstractmethod decorator (#100715) Enables B027 and applies fixes by adding abstract method decorators. Autofix generated by ruff master. Pull Request resolved: https://github.com/pytorch/pytorch/pull/100715 Approved by: https://github.com/ezyang --- .flake8 | 2 +- .lintrunner.toml | 2 +- caffe2/python/modeling/net_modifier.py | 3 --- pyproject.toml | 2 +- test/dynamo/test_misc.py | 2 +- torch/ao/quantization/fx/fuse_handler.py | 1 + torch/distributed/checkpoint/_fsspec_filesystem.py | 1 + torch/distributed/checkpoint/filesystem.py | 1 + torch/distributed/elastic/rendezvous/api.py | 1 + torch/distributed/fsdp/wrap.py | 2 -- torch/fx/passes/infra/pass_base.py | 7 ++----- torch/nn/utils/prune.py | 3 --- .../_internal/distributed/rpc/rpc_agent_test_fixture.py | 2 +- 13 files changed, 11 insertions(+), 18 deletions(-) diff --git a/.flake8 b/.flake8 index b5a61410d3e2..ad26691425cd 100644 --- a/.flake8 +++ b/.flake8 @@ -14,7 +14,7 @@ ignore = # to line this up with executable bit EXE001, # these ignores are from flake8-bugbear; please fix! - B007,B008,B017,B019,B020,B023,B024,B026,B027,B028,B903,B904,B905,B906,B907 + B007,B008,B017,B019,B020,B023,B024,B026,B028,B903,B904,B905,B906,B907 # these ignores are from flake8-comprehensions; please fix! C407, # these ignores are from flake8-logging-format; please fix! diff --git a/.lintrunner.toml b/.lintrunner.toml index 05a089a834d6..aa4104c214da 100644 --- a/.lintrunner.toml +++ b/.lintrunner.toml @@ -999,6 +999,6 @@ init_command = [ 'python3', 'tools/linter/adapters/pip_init.py', '--dry-run={{DRYRUN}}', - 'ruff==0.0.262', + 'ruff==0.0.265', ] is_formatter = true diff --git a/caffe2/python/modeling/net_modifier.py b/caffe2/python/modeling/net_modifier.py index c0545fad08f5..55f47f8fbac8 100644 --- a/caffe2/python/modeling/net_modifier.py +++ b/caffe2/python/modeling/net_modifier.py @@ -17,9 +17,6 @@ class NetModifier(metaclass=abc.ABCMeta): modifier(net) """ - def __init__(self): - pass - @abc.abstractmethod def modify_net(self, net, init_net=None, grad_map=None, blob_to_device=None): pass diff --git a/pyproject.toml b/pyproject.toml index 9767221767ac..75237c629bcb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,7 @@ ignore = [ "B019", "B020", "B023", "B024", "B026", "B028", # No explicit `stacklevel` keyword argument found - "B027", "B904", "B905", + "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 diff --git a/test/dynamo/test_misc.py b/test/dynamo/test_misc.py index 4b28e52babab..a26010434ef2 100644 --- a/test/dynamo/test_misc.py +++ b/test/dynamo/test_misc.py @@ -3521,7 +3521,7 @@ def fn(): def test_user_function_variable_supports_type_abcmeta_argument(self): class Foo(metaclass=abc.ABCMeta): @abc.abstractclassmethod - def read(self): + def read(self): # noqa: B027 pass class Bar(Foo): diff --git a/torch/ao/quantization/fx/fuse_handler.py b/torch/ao/quantization/fx/fuse_handler.py index 2706f96fef36..718cc561bfa0 100644 --- a/torch/ao/quantization/fx/fuse_handler.py +++ b/torch/ao/quantization/fx/fuse_handler.py @@ -23,6 +23,7 @@ __all__ = [ class FuseHandler(ABC): """ Base handler class for the fusion patterns """ + @abstractmethod def __init__(self, node: Node): pass diff --git a/torch/distributed/checkpoint/_fsspec_filesystem.py b/torch/distributed/checkpoint/_fsspec_filesystem.py index 904f4bb21363..5726d4aab631 100644 --- a/torch/distributed/checkpoint/_fsspec_filesystem.py +++ b/torch/distributed/checkpoint/_fsspec_filesystem.py @@ -78,6 +78,7 @@ class _TensorLoader(ABC): def add(self, size: int, obj: object): pass + @abstractmethod def start_loading(self): pass diff --git a/torch/distributed/checkpoint/filesystem.py b/torch/distributed/checkpoint/filesystem.py index 9074b16e9357..61571d8602b0 100644 --- a/torch/distributed/checkpoint/filesystem.py +++ b/torch/distributed/checkpoint/filesystem.py @@ -84,6 +84,7 @@ class _TensorLoader(ABC): def add(self, size, obj): pass + @abstractmethod def start_loading(self): pass diff --git a/torch/distributed/elastic/rendezvous/api.py b/torch/distributed/elastic/rendezvous/api.py index 73ba2ad7c34e..2b59be4b5eb6 100644 --- a/torch/distributed/elastic/rendezvous/api.py +++ b/torch/distributed/elastic/rendezvous/api.py @@ -105,6 +105,7 @@ class RendezvousHandler(ABC): allow nodes to join the correct distributed application. """ + @abstractmethod def shutdown(self) -> bool: """Closes all resources that were open for the rendezvous. diff --git a/torch/distributed/fsdp/wrap.py b/torch/distributed/fsdp/wrap.py index 0fbaee2e6124..febe2e394251 100644 --- a/torch/distributed/fsdp/wrap.py +++ b/torch/distributed/fsdp/wrap.py @@ -39,8 +39,6 @@ class _FSDPPolicy(ABC): # The motivation for this abstract base class is to hide the interface # expected by `_recursive_wrap()` from users (i.e. the `recurse` argument). - def __init__(self): - ... @property @abstractmethod diff --git a/torch/fx/passes/infra/pass_base.py b/torch/fx/passes/infra/pass_base.py index cb194a56c68e..dd699ea86cde 100644 --- a/torch/fx/passes/infra/pass_base.py +++ b/torch/fx/passes/infra/pass_base.py @@ -31,9 +31,6 @@ class PassBase(abc.ABC): the PassManager's `passes` attribute. """ - def __init__(self) -> None: - pass - def __call__(self, graph_module: GraphModule) -> Optional[PassResult]: """ Runs the precondition check, the pass itself, and the postcondition check. @@ -55,7 +52,7 @@ class PassBase(abc.ABC): """ pass - def requires(self, graph_module: GraphModule) -> None: + def requires(self, graph_module: GraphModule) -> None: # noqa: B027 """ This function will be called before the pass is run and will check that the given graph module contains the preconditions needed to run the @@ -66,7 +63,7 @@ class PassBase(abc.ABC): """ pass - def ensures(self, graph_module: GraphModule) -> None: + def ensures(self, graph_module: GraphModule) -> None: # noqa: B027 """ This function will be called after the pass is run and will check that the given graph module contains the postconditions needed to run the diff --git a/torch/nn/utils/prune.py b/torch/nn/utils/prune.py index 702c7fa29c98..81e9dcadfd55 100644 --- a/torch/nn/utils/prune.py +++ b/torch/nn/utils/prune.py @@ -17,9 +17,6 @@ class BasePruningMethod(ABC): """ _tensor_name: str - def __init__(self): - pass - def __call__(self, module, inputs): r"""Multiplies the mask (stored in ``module[name + '_mask']``) into the original tensor (stored in ``module[name + '_orig']``) diff --git a/torch/testing/_internal/distributed/rpc/rpc_agent_test_fixture.py b/torch/testing/_internal/distributed/rpc/rpc_agent_test_fixture.py index 5821ae7c10b8..e81961359229 100644 --- a/torch/testing/_internal/distributed/rpc/rpc_agent_test_fixture.py +++ b/torch/testing/_internal/distributed/rpc/rpc_agent_test_fixture.py @@ -35,7 +35,7 @@ class RpcAgentTestFixture(ABC): def rpc_backend_options(self): pass - def setup_fault_injection(self, faulty_messages, messages_to_delay): + def setup_fault_injection(self, faulty_messages, messages_to_delay): # noqa: B027 """Method used by dist_init to prepare the faulty agent. Does nothing for other agents.