[BE]: enable PLE error codes in ruff and fix bugs (#101079)

Enables PyLint error codes implemented in ruff. These are un-opinionated static analysis checks on Python code that finds common bugs. After running all the PLE error codes that are implemented in ruff, I fixed the bugs, added a few ignores for malformed Python code that is part of our JIT test script, and finally added a few ignores for a false positive on PLE0605 and submitted an issue upstream to fix in ruff https://github.com/charliermarsh/ruff/issues/4345 .

Common bugs found here include analysis for malformed logging format calls, bad string format calls, invalid escape sequences, and more.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/101079
Approved by: https://github.com/malfet
This commit is contained in:
Aaron Gokaslan
2023-05-11 23:57:25 +00:00
committed by PyTorch MergeBot
parent b7bf953bbc
commit 738ba13b35
7 changed files with 9 additions and 8 deletions

View File

@ -64,6 +64,8 @@ select = [
"F",
"SIM1",
"W",
# Not included in flake8
"PLE",
]
[tool.ruff.per-file-ignores]

View File

@ -451,7 +451,7 @@ class TestWith(JitTestCase):
def __init__(self):
self.count = 1
def __enter__(self, incr: int):
def __enter__(self, incr: int): # noqa: PLE0302
self.count += incr
def __exit__(self, type: Any, value: Any, tb: Any):
@ -469,7 +469,7 @@ class TestWith(JitTestCase):
def __enter__(self):
self.count += 1
def __exit__(self, type: Any, value: Any):
def __exit__(self, type: Any, value: Any): # noqa: PLE0302
pass
@torch.jit.script

View File

@ -1562,7 +1562,7 @@ $3 = torch._ops.aten.add.Tensor($1, $2)""")
def test_with_nested_modes(self):
class ErrorA(RuntimeError):
def __init__(self, msg):
return super().__init__(msg)
super().__init__(msg)
class A(TorchDispatchMode):
def __init__(self, msg):

View File

@ -888,9 +888,8 @@ class InstructionTranslatorBase(Checkpointable[InstructionTranslatorGraphState])
log.warning(
"can't resolve package from __spec__ or __package__, "
"falling back on __name__ and __path__",
ImportWarning,
stacklevel=3,
) # type: ignore[call-arg]
)
package = self.f_globals["__name__"]
if "__path__" not in self.f_globals:
package = package.rpartition(".")[0]

View File

@ -78,7 +78,7 @@ if is_available():
rendezvous_iterator: Generator[Tuple[Store, int, int], None, None]
__all__ += ["init_rpc", "BackendType", "TensorPipeRpcBackendOptions"]
__all__ = __all__ + api.__all__ + backend_registry.__all__
__all__ = __all__ + api.__all__ + backend_registry.__all__ # noqa: PLE0605
def init_rpc(
name,

View File

@ -121,7 +121,7 @@ class _lazy_property_and_property(lazy_property, property):
* lazy_property when Distribution validate_args looks
"""
def __init__(self, wrapped):
return property.__init__(self, wrapped)
property.__init__(self, wrapped)
def tril_matrix_to_vec(mat, diag=0):

View File

@ -25,7 +25,7 @@ __all__ = ['set_sharing_strategy', 'get_sharing_strategy',
from multiprocessing import * # noqa: F403
__all__ += multiprocessing.__all__ # type: ignore[attr-defined]
__all__ += multiprocessing.__all__ # noqa: PLE0605 type: ignore[attr-defined]
# This call adds a Linux specific prctl(2) wrapper function to this module.