From b17b97bb1a6d0c90b4b45690f32f4f564285f07e Mon Sep 17 00:00:00 2001 From: Dan Halperin Date: Mon, 23 Mar 2026 11:42:10 -0700 Subject: [PATCH] Replace black, isort, autoflake, and flake8 with ruff Consolidate four separate pre-commit linting/formatting tools into ruff. Update .pre-commit-config.yaml, replace [dev] extras, add [tool.ruff] config to pyproject.toml, and delete .flake8. Minor formatting differences from ruff's formatter applied to 5 files. ---- Prompt: ``` What if anything should we do to modernize this package or the github ci? ``` commit-id:bd803e60 --- .flake8 | 4 ---- .pre-commit-config.yaml | 24 +++++------------------- netconan/anonymize_files.py | 5 +++-- netconan/default_pwd_regexes.py | 1 - netconan/ip_anonymization.py | 6 +++--- netconan/netconan.py | 3 +-- netconan/sensitive_item_removal.py | 8 +++++--- pyproject.toml | 9 ++++++++- tests/unit/test_ip_anonymization.py | 3 +-- 9 files changed, 26 insertions(+), 37 deletions(-) delete mode 100644 .flake8 diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 0dabb14..0000000 --- a/.flake8 +++ /dev/null @@ -1,4 +0,0 @@ -[flake8] -filename=*.py, -extend-ignore=E203,E501,D401,W503 -exclude=docs,__pycache__,.eggs,*.egg,build,virtualEnv,virtualEnv3 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ae7242b..702e752 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,25 +1,11 @@ # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks repos: -- repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.4.2 +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.15.7 hooks: - - id: black + - id: ruff-check + args: ["--fix"] exclude: netconan/default_reserved_words.py -- repo: https://github.com/PyCQA/isort - rev: 5.13.2 - hooks: - - id: isort - args: ["--profile=black"] - exclude: netconan/default_reserved_words.py -- repo: https://github.com/PyCQA/autoflake - rev: v2.3.1 - hooks: - - id: autoflake - args: ["--in-place", "--remove-all-unused-imports", "--remove-unused-variables"] + - id: ruff-format exclude: netconan/default_reserved_words.py -- repo: https://github.com/pycqa/flake8 - rev: 7.1.0 - hooks: - - id: flake8 - additional_dependencies: [flake8-docstrings] diff --git a/netconan/anonymize_files.py b/netconan/anonymize_files.py index 18b7913..4a0334a 100644 --- a/netconan/anonymize_files.py +++ b/netconan/anonymize_files.py @@ -224,8 +224,9 @@ def _process_one(file_anonymizer: FileAnonymizer, in_path: str, out_path: str) - _mkdirs(out_path) if os.path.isdir(out_path): raise ValueError( - "Cannot write output file; " - "output file is a directory ({})".format(out_path) + "Cannot write output file; output file is a directory ({})".format( + out_path + ) ) in_io: IO[str] = sys.stdin if use_stdin else open(in_path, "r") diff --git a/netconan/default_pwd_regexes.py b/netconan/default_pwd_regexes.py index 60257a4..91c503b 100644 --- a/netconan/default_pwd_regexes.py +++ b/netconan/default_pwd_regexes.py @@ -31,7 +31,6 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. - # A raw regex rule: (pattern_string, capture_group_index_or_None) RegexRule = tuple[str, int | None] diff --git a/netconan/ip_anonymization.py b/netconan/ip_anonymization.py index b734baa..df2f96e 100644 --- a/netconan/ip_anonymization.py +++ b/netconan/ip_anonymization.py @@ -221,7 +221,8 @@ def __init__( self._preserve_addresses: list[ipaddress.IPv4Network] = [] if preserve_addresses is not None: self._preserve_addresses = [ - ipaddress.ip_network(n) for n in preserve_addresses # type: ignore[misc] + ipaddress.ip_network(n) # type: ignore[misc] + for n in preserve_addresses ] # Make sure the prefixes are also preserved for preserved blocks, so # anonymized addresses outside the block don't accidentally collide @@ -259,8 +260,7 @@ def get_addr_pattern(cls) -> re.Pattern[str]: @classmethod def make_addr(cls, addr_str: str) -> ipaddress.IPv4Address: - """ - Return an IPv4 address from the given string. + """Return an IPv4 address from the given string. If the octets in `addr_str` have leading zeros, such as in 1.2.3.040, those zeros will be ignored (1.2.3.40) -- they will NOT be interpreted diff --git a/netconan/netconan.py b/netconan/netconan.py index 5b1a9b0..0ec9bb8 100644 --- a/netconan/netconan.py +++ b/netconan/netconan.py @@ -223,8 +223,7 @@ def main(argv: list[str] = sys.argv[1:]) -> None: ] ): logging.warning( - "No anonymization options turned on, " - "no output file(s) will be generated." + "No anonymization options turned on, no output file(s) will be generated." ) else: anonymize_files( diff --git a/netconan/sensitive_item_removal.py b/netconan/sensitive_item_removal.py index 906bc8e..329f22f 100644 --- a/netconan/sensitive_item_removal.py +++ b/netconan/sensitive_item_removal.py @@ -424,9 +424,11 @@ def replace_matching_item( output_line = compiled_re.sub( # This is text preceding the password and shouldn't be anonymized - lambda m: (m.group("prefix") if "prefix" in m.groupdict() else "") - + _anonymize_value( - m.group(sensitive_item_num), pwd_lookup, reserved_words, salt + lambda m: ( + (m.group("prefix") if "prefix" in m.groupdict() else "") + + _anonymize_value( + m.group(sensitive_item_num), pwd_lookup, reserved_words, salt + ) ), output_line, ) diff --git a/pyproject.toml b/pyproject.toml index 7f02def..1699463 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,7 @@ dependencies = [ dynamic = ["version"] [project.optional-dependencies] -dev = ["flake8<8.0.0", "flake8-docstrings<2.0.0", "pydocstyle<6.0.0"] +dev = ["ruff>=0.15"] test = [ "pytest>=7", "pytest-cov<6.0.0", @@ -55,6 +55,13 @@ netconan = ["py.typed"] [tool.setuptools.dynamic] version = {attr = "netconan.__version__"} +[tool.ruff] +exclude = ["netconan/default_reserved_words.py"] + +[tool.ruff.lint] +select = ["E", "W", "F", "D", "I"] +ignore = ["E203", "E501", "D203", "D213", "D401", "D413"] + [tool.pytest.ini_options] addopts = "--cov=netconan" testpaths = ["tests"] diff --git a/tests/unit/test_ip_anonymization.py b/tests/unit/test_ip_anonymization.py index ead25df..a148461 100644 --- a/tests/unit/test_ip_anonymization.py +++ b/tests/unit/test_ip_anonymization.py @@ -305,8 +305,7 @@ def test_preserve_address_preserves_prefix(): def _cpl_v4(left, right): - """ - Return the common prefix length for two IPv4 addresses. + """Return the common prefix length for two IPv4 addresses. e.g. _cpl_v4(1.0.0.1, 1.0.0.1) == 32