From 5541fad063aa2298a5d349ab98320c80fe25acf8 Mon Sep 17 00:00:00 2001 From: Omar Sandoval Date: Wed, 11 Aug 2021 14:22:39 -0700 Subject: [PATCH] Fix some flake8 errors Mainly unused imports, unused variables, unnecessary f-strings, and regex literals missing the r prefix. I'm not adding it to the CI linter because it's too noisy, though. Signed-off-by: Omar Sandoval --- docs/exts/drgndoc/docstrings.py | 6 ++--- docs/exts/drgndoc/ext.py | 2 +- docs/exts/drgndoc/namespace.py | 3 +-- docs/exts/drgndoc/visitor.py | 1 - drgn/helpers/__init__.py | 2 +- drgn/helpers/linux/idr.py | 2 +- drgn/helpers/linux/mm.py | 2 +- drgn/helpers/linux/net.py | 1 - drgn/helpers/linux/pid.py | 4 ++-- examples/linux/fs_inodes.py | 1 - libdrgn/build-aux/gen_constants.py | 3 +-- scripts/iwyu.py | 2 +- setup.py | 2 +- tests/__init__.py | 1 - tests/helpers/linux/test_fs.py | 4 ++-- tests/libdrgn.py | 1 - tests/test_dwarf.py | 36 ++++++++++++++++-------------- tests/test_object.py | 1 - tests/test_type.py | 2 -- tools/bpf_inspect.py | 8 +------ vmtest/download.py | 2 +- vmtest/manage.py | 2 +- vmtest/vm.py | 4 ++-- 23 files changed, 39 insertions(+), 53 deletions(-) diff --git a/docs/exts/drgndoc/docstrings.py b/docs/exts/drgndoc/docstrings.py index e6bab536..a07994fc 100644 --- a/docs/exts/drgndoc/docstrings.py +++ b/docs/exts/drgndoc/docstrings.py @@ -5,7 +5,7 @@ import argparse import functools import sys -from typing import Union, cast +from typing import cast from drgndoc.format import Formatter from drgndoc.namespace import Namespace, ResolvedNode @@ -74,7 +74,7 @@ if __name__ == "__main__": if args.header: output_file.write( - f"""\ + """\ /* * Generated by drgndoc.docstrings -H. * @@ -86,7 +86,7 @@ if __name__ == "__main__": """ ) else: - output_file.write(f"/* Generated by drgndoc.docstrings. */\n\n") + output_file.write("/* Generated by drgndoc.docstrings. */\n\n") def aux(resolved: ResolvedNode[Node], name: str) -> None: node = resolved.node diff --git a/docs/exts/drgndoc/ext.py b/docs/exts/drgndoc/ext.py index 6ca33b9d..2d252f8e 100644 --- a/docs/exts/drgndoc/ext.py +++ b/docs/exts/drgndoc/ext.py @@ -42,7 +42,7 @@ file for the C extension itself (drgndoc.docstrings). import os.path import re -from typing import Any, Dict, List, cast +from typing import Any, Dict, cast import docutils.nodes import docutils.parsers.rst.directives diff --git a/docs/exts/drgndoc/namespace.py b/docs/exts/drgndoc/namespace.py index 808c1cb0..9b19f72e 100644 --- a/docs/exts/drgndoc/namespace.py +++ b/docs/exts/drgndoc/namespace.py @@ -2,7 +2,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later import itertools -from typing import Generic, Iterator, List, Mapping, Optional, Sequence, TypeVar, Union +from typing import Generic, Iterator, List, Mapping, Sequence, TypeVar, Union from drgndoc.parse import ( Class, @@ -14,7 +14,6 @@ from drgndoc.parse import ( Node, Variable, ) -from drgndoc.util import dot_join NodeT_co = TypeVar("NodeT_co", bound=Node, covariant=True) diff --git a/docs/exts/drgndoc/visitor.py b/docs/exts/drgndoc/visitor.py index 6cf132ff..b527b22e 100644 --- a/docs/exts/drgndoc/visitor.py +++ b/docs/exts/drgndoc/visitor.py @@ -2,7 +2,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later import ast -import sys from typing import Any, Optional diff --git a/drgn/helpers/__init__.py b/drgn/helpers/__init__.py index 552451c9..b52f7e47 100644 --- a/drgn/helpers/__init__.py +++ b/drgn/helpers/__init__.py @@ -23,7 +23,7 @@ useful for scripts or for implementing other helpers. import enum import typing -from typing import Container, Iterable, List, Tuple +from typing import Container, Iterable from drgn import Type diff --git a/drgn/helpers/linux/idr.py b/drgn/helpers/linux/idr.py index b9497cd8..623fc0ca 100644 --- a/drgn/helpers/linux/idr.py +++ b/drgn/helpers/linux/idr.py @@ -15,7 +15,7 @@ from typing import Iterator, Tuple from _drgn import _linux_helper_idr_find as idr_find from drgn import Object -from drgn.helpers.linux.radixtree import radix_tree_for_each, radix_tree_lookup +from drgn.helpers.linux.radixtree import radix_tree_for_each __all__ = ( "idr_find", diff --git a/drgn/helpers/linux/mm.py b/drgn/helpers/linux/mm.py index 8a5b9d9e..927f0caa 100644 --- a/drgn/helpers/linux/mm.py +++ b/drgn/helpers/linux/mm.py @@ -11,7 +11,7 @@ implemented. """ import operator -from typing import Any, Iterator, List, Optional, Union, overload +from typing import Iterator, List, Optional, Union, overload from _drgn import _linux_helper_read_vm from drgn import IntegerLike, Object, Program, cast diff --git a/drgn/helpers/linux/net.py b/drgn/helpers/linux/net.py index a153cb53..3b23694d 100644 --- a/drgn/helpers/linux/net.py +++ b/drgn/helpers/linux/net.py @@ -15,7 +15,6 @@ from typing import Iterator, Union from drgn import NULL, IntegerLike, Object, Program from drgn.helpers.linux.list import hlist_for_each_entry from drgn.helpers.linux.list_nulls import hlist_nulls_for_each_entry -from drgn.helpers.linux.tcp import sk_tcpstate __all__ = ( "netdev_get_by_index", diff --git a/drgn/helpers/linux/pid.py b/drgn/helpers/linux/pid.py index 9f4d7cab..d2e786ed 100644 --- a/drgn/helpers/linux/pid.py +++ b/drgn/helpers/linux/pid.py @@ -16,8 +16,8 @@ from _drgn import ( _linux_helper_find_task as find_task, _linux_helper_pid_task as pid_task, ) -from drgn import NULL, Object, Program, cast, container_of -from drgn.helpers.linux.idr import idr_find, idr_for_each +from drgn import Object, Program, cast, container_of +from drgn.helpers.linux.idr import idr_for_each from drgn.helpers.linux.list import hlist_for_each_entry __all__ = ( diff --git a/examples/linux/fs_inodes.py b/examples/linux/fs_inodes.py index 856d13f7..6e1a4c19 100755 --- a/examples/linux/fs_inodes.py +++ b/examples/linux/fs_inodes.py @@ -6,7 +6,6 @@ import os import sys -import time from drgn.helpers.linux.fs import for_each_mount, inode_path from drgn.helpers.linux.list import list_for_each_entry diff --git a/libdrgn/build-aux/gen_constants.py b/libdrgn/build-aux/gen_constants.py index b859dfd8..441a559c 100644 --- a/libdrgn/build-aux/gen_constants.py +++ b/libdrgn/build-aux/gen_constants.py @@ -1,7 +1,6 @@ # Copyright (c) Facebook, Inc. and its affiliates. # SPDX-License-Identifier: GPL-3.0-or-later -import os.path import re import sys @@ -63,7 +62,7 @@ out: def gen_constants(input_file, output_file): drgn_h = input_file.read() output_file.write( - f"""\ + """\ /* Generated by libdrgn/build-aux/gen_constants.py. */ #include "drgnpy.h" diff --git a/scripts/iwyu.py b/scripts/iwyu.py index 42eb2a06..85c684d5 100755 --- a/scripts/iwyu.py +++ b/scripts/iwyu.py @@ -122,7 +122,7 @@ def gen_python_mapping_file(mapping_path): # For some reason, include-what-you-mean wants struct _typeobject, but # find-all-symbols only reports PyTypeObject. Add it manually. imp.write( - f' {{"symbol": ["_typeobject", "private", "", "public"]}}, # From cpython/object.h\n' + ' {{"symbol": ["_typeobject", "private", "", "public"]}}, # From cpython/object.h\n' ) imp.write("]\n") diff --git a/setup.py b/setup.py index 802207e4..1f0051fe 100755 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later # setuptools must be imported before distutils (see pypa/setuptools#2230). -import setuptools # isort: skip +import setuptools # isort: skip # noqa: F401 import contextlib from distutils import log diff --git a/tests/__init__.py b/tests/__init__.py index 43367aec..07d307df 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,7 +2,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later import functools -import types from typing import Any, NamedTuple, Optional import unittest diff --git a/tests/helpers/linux/test_fs.py b/tests/helpers/linux/test_fs.py index aff6e1a9..d127c351 100644 --- a/tests/helpers/linux/test_fs.py +++ b/tests/helpers/linux/test_fs.py @@ -51,9 +51,9 @@ class TestFs(LinuxHelperTestCase): with tempfile.TemporaryDirectory(prefix="drgn-tests-") as dir: path1 = os.fsencode(os.path.abspath(os.path.join(dir, "a"))) path2 = os.fsencode(os.path.abspath(os.path.join(dir, "b"))) - with open(path1, "w") as f: + with open(path1, "w"): os.link(path1, path2) - with open(path2, "r") as f: + with open(path2, "r"): inode = path_lookup(self.prog, path1).dentry.d_inode paths = list(inode_paths(inode)) self.assertEqual(len(paths), 2) diff --git a/tests/libdrgn.py b/tests/libdrgn.py index 900eccca..bdf3b693 100644 --- a/tests/libdrgn.py +++ b/tests/libdrgn.py @@ -7,7 +7,6 @@ from enum import auto import os import _drgn -import drgn _drgn_pydll = ctypes.PyDLL(_drgn.__file__) _drgn_cdll = ctypes.CDLL(_drgn.__file__) diff --git a/tests/test_dwarf.py b/tests/test_dwarf.py index cffc8d7f..d266bd55 100644 --- a/tests/test_dwarf.py +++ b/tests/test_dwarf.py @@ -6,7 +6,6 @@ import operator import os.path import re import tempfile -import unittest import drgn from drgn import ( @@ -1025,22 +1024,25 @@ class TestTypes(TestCase): ), ] - point_type = lambda prog: prog.struct_type( - "point", - 8, - ( - TypeMember(prog.int_type("int", 4, True), "x"), - TypeMember(prog.int_type("int", 4, True), "y", 32), - ), - ) - other_point_type = lambda prog: prog.struct_type( - "point", - 8, - ( - TypeMember(prog.int_type("int", 4, True), "a"), - TypeMember(prog.int_type("int", 4, True), "b", 32), - ), - ) + def point_type(prog): + return prog.struct_type( + "point", + 8, + ( + TypeMember(prog.int_type("int", 4, True), "x"), + TypeMember(prog.int_type("int", 4, True), "y", 32), + ), + ) + + def other_point_type(prog): + return prog.struct_type( + "point", + 8, + ( + TypeMember(prog.int_type("int", 4, True), "a"), + TypeMember(prog.int_type("int", 4, True), "b", 32), + ), + ) prog = dwarf_program(dies) for dir in ["", "src", "usr/src", "/usr/src"]: diff --git a/tests/test_object.py b/tests/test_object.py index 4ed895da..811d7838 100644 --- a/tests/test_object.py +++ b/tests/test_object.py @@ -11,7 +11,6 @@ from drgn import ( ObjectAbsentError, OutOfBoundsError, Qualifiers, - Type, TypeMember, cast, reinterpret, diff --git a/tests/test_type.py b/tests/test_type.py index 7ac393e8..435db974 100644 --- a/tests/test_type.py +++ b/tests/test_type.py @@ -1,8 +1,6 @@ # Copyright (c) Facebook, Inc. and its affiliates. # SPDX-License-Identifier: GPL-3.0-or-later -import operator - from drgn import ( Language, Object, diff --git a/tools/bpf_inspect.py b/tools/bpf_inspect.py index 17749405..0c51044c 100755 --- a/tools/bpf_inspect.py +++ b/tools/bpf_inspect.py @@ -2,13 +2,7 @@ # Copyright (c) Facebook, Inc. and its affiliates. # SPDX-License-Identifier: GPL-3.0-or-later -DESCRIPTION = """ -drgn script to list BPF programs or maps and their properties -unavailable via kernel API. -""" - import argparse -import sys from drgn.helpers import enum_type_to_class from drgn.helpers.linux import bpf_map_for_each, bpf_prog_for_each, hlist_for_each_entry @@ -116,7 +110,7 @@ def list_bpf_maps(args): def main(): parser = argparse.ArgumentParser( - description=DESCRIPTION, formatter_class=argparse.RawTextHelpFormatter + description="drgn script to list BPF programs or maps and their properties unavailable via kernel API" ) subparsers = parser.add_subparsers(title="subcommands", dest="subcommand") diff --git a/vmtest/download.py b/vmtest/download.py index 8565d761..b5abf1f5 100644 --- a/vmtest/download.py +++ b/vmtest/download.py @@ -27,7 +27,7 @@ VMTEST_GITHUB_RELEASE = ("osandov", "drgn", "vmtest-assets") def available_kernel_releases( github_release: Dict[str, Any], arch: str ) -> Dict[str, Dict[str, Any]]: - pattern = re.compile(r"kernel-(.*)\." + re.escape(arch) + "\.tar\.zst") + pattern = re.compile(r"kernel-(.*)\." + re.escape(arch) + r"\.tar\.zst") releases = {} for asset in github_release["assets"]: match = pattern.fullmatch(asset["name"]) diff --git a/vmtest/manage.py b/vmtest/manage.py index daddf592..0bbff359 100644 --- a/vmtest/manage.py +++ b/vmtest/manage.py @@ -87,7 +87,7 @@ async def fetch_kernel_tags(kernel_dir: Path, kernel_tags: Sequence[str]) -> Non mainline_tags = [] stable_tags = [] for tag in kernel_tags: - if re.fullmatch("v[0-9]+\.[0-9]+\.[0-9]+", tag): + if re.fullmatch(r"v[0-9]+\.[0-9]+\.[0-9]+", tag): stable_tags.append(tag) else: mainline_tags.append(tag) diff --git a/vmtest/vm.py b/vmtest/vm.py index c3fe15c7..1d9971c1 100644 --- a/vmtest/vm.py +++ b/vmtest/vm.py @@ -162,7 +162,7 @@ class LostVMError(Exception): def run_in_vm(command: str, kernel_dir: Path, build_dir: Path) -> int: match = re.search( - "QEMU emulator version ([0-9]+(?:\.[0-9]+)*)", + r"QEMU emulator version ([0-9]+(?:\.[0-9]+)*)", subprocess.check_output( ["qemu-system-x86_64", "-version"], universal_newlines=True ), @@ -238,7 +238,7 @@ def run_in_vm(command: str, kernel_dir: Path, build_dir: Path) -> int: # fmt: on ], env=env, - ) as qemu: + ): server_sock.settimeout(5) try: sock = server_sock.accept()[0]