Skip to content

Commit a954373

Browse files
authored
Uses ruff to format code (#556)
1 parent c86fdc2 commit a954373

31 files changed

+59
-40
lines changed

.github/workflows/python-package.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ jobs:
2828
- name: Install dependencies
2929
run: uv sync -i https://pypi.org/simple # Ignore download URLs in uv.lock.
3030
- name: Lint with ruff
31-
run: uv run ruff check src/cosmic_ray
31+
run: |
32+
uv run ruff check .
33+
uv run ruff format --check .
3234
- name: Test with pytest
3335
run: uv run pytest tests --run-slow
3436

src/cosmic_ray/cli.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
Here we manage command-line parsing and launching of the internal machinery that does mutation testing.
44
"""
5+
56
import dataclasses
67
import json
78
import logging
@@ -261,7 +262,9 @@ def mutate_and_test(module_path, operator, occurrence, test_command, keep_stdout
261262
"""
262263
with open(os.devnull, "w") as devnull:
263264
with redirect_stdout(sys.stdout if keep_stdout else devnull):
264-
work_result = cosmic_ray.mutating.mutate_and_test(Path(module_path), operator, occurrence, test_command, None)
265+
work_result = cosmic_ray.mutating.mutate_and_test(
266+
Path(module_path), operator, occurrence, test_command, None
267+
)
265268

266269
sys.stdout.write(json.dumps(dataclasses.asdict(work_result)))
267270

src/cosmic_ray/commands/execute.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"Implementation of the 'execute' command."
2+
23
import logging
34
import os
45

src/cosmic_ray/commands/init.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"Implementation of the 'init' command."
2+
23
import logging
34
import uuid
45
from typing import Iterable

src/cosmic_ray/commands/new_config.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""Implementation of the 'new-config' command.
2-
"""
1+
"""Implementation of the 'new-config' command."""
32

43
import os.path
54

src/cosmic_ray/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Configuration module."""
2+
23
import logging
34
import sys
45
from contextlib import contextmanager

src/cosmic_ray/distribution/http.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
[cosmic-ray.distributor.http]
1717
worker-urls = ['http://localhost:9876', 'http://localhost:9877']
1818
"""
19+
1920
import asyncio
2021
import logging
2122
from pathlib import Path

src/cosmic_ray/mutating.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""Support for making mutations to source code.
2-
"""
1+
"""Support for making mutations to source code."""
2+
33
import contextlib
44
import difflib
55
import logging

src/cosmic_ray/operators/boolean_replacer.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""Implementation of the boolean replacement operators.
2-
"""
1+
"""Implementation of the boolean replacement operators."""
32

43
import parso.python.tree
54

src/cosmic_ray/operators/break_continue.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
class ReplaceBreakWithContinue(KeywordReplacementOperator):
99
"Operator which replaces 'break' with 'continue'."
10+
1011
from_keyword = "break"
1112
to_keyword = "continue"
1213

1314

1415
class ReplaceContinueWithBreak(KeywordReplacementOperator):
1516
"Operator which replaces 'continue' with 'break'."
17+
1618
from_keyword = "continue"
1719
to_keyword = "break"

src/cosmic_ray/operators/comparison_operator_replacement.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""This module contains mutation operators which replace one
22
comparison operator with another.
33
"""
4+
45
import itertools
56
from enum import Enum
67

@@ -13,6 +14,7 @@
1314

1415
class ComparisonOperators(Enum):
1516
"All comparison operators that we mutate."
17+
1618
Eq = "=="
1719
NotEq = "!="
1820
Lt = "<"

src/cosmic_ray/operators/number_replacer.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""Implementation of the NumberReplacer operator.
2-
"""
1+
"""Implementation of the NumberReplacer operator."""
32

43
import parso
54

src/cosmic_ray/operators/provider.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""Operator-provider plugin for all core cosmic ray operators.
2-
"""
1+
"""Operator-provider plugin for all core cosmic ray operators."""
32

43
import itertools
54

src/cosmic_ray/operators/unary_operator_replacement.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""Implementation of the unary-operator-replacement operator.
2-
"""
1+
"""Implementation of the unary-operator-replacement operator."""
32

43
from enum import Enum
54
from itertools import permutations
@@ -12,6 +11,7 @@
1211

1312
class UnaryOperators(Enum):
1413
"All unary operators that we mutate."
14+
1515
UAdd = "+"
1616
USub = "-"
1717
Invert = "~"
@@ -51,7 +51,7 @@ def mutate(self, node, index):
5151
@classmethod
5252
def examples(cls):
5353
from_code = f"{from_op.value}1"
54-
to_code = from_code[len(from_op.value):]
54+
to_code = from_code[len(from_op.value) :]
5555

5656
if to_op is not UnaryOperators.Nothing:
5757
to_code = to_op.value + to_code

src/cosmic_ray/operators/variable_inserter.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Implementation of the variable-inserter operator."""
2+
23
import random
34

45
import parso.python.tree

src/cosmic_ray/operators/variable_replacer.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Implementation of the variable-replacement operator."""
2+
23
from random import randint
34

45
from parso.python.tree import ExprStmt, Leaf, Number

src/cosmic_ray/plugins.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""Query and retrieve the various plugins in Cosmic Ray.
2-
"""
1+
"""Query and retrieve the various plugins in Cosmic Ray."""
32

43
import logging
54

@@ -34,7 +33,7 @@ def get_operator(name):
3433
"""
3534
sep = name.index("/")
3635
provider_name = name[:sep]
37-
operator_name = name[sep + 1:]
36+
operator_name = name[sep + 1 :]
3837

3938
provider = OPERATOR_PROVIDERS[provider_name]
4039
return provider[operator_name]

src/cosmic_ray/tools/badge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""Tool for creating badge.
2-
"""
1+
"""Tool for creating badge."""
2+
33
import os
44
from logging import getLogger
55

src/cosmic_ray/tools/filters/filter_app.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""A simple base for creating common types of work-db filters.
2-
"""
1+
"""A simple base for creating common types of work-db filters."""
2+
33
import argparse
44
import logging
55
import sys

src/cosmic_ray/tools/filters/git.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""A filter that uses git to determine when specific files/lines
22
should be skipped.
33
"""
4+
45
import logging
56
import re
67
import subprocess

src/cosmic_ray/tools/filters/operators_filter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""An filter that removes operators based on regular expressions.
2-
"""
1+
"""An filter that removes operators based on regular expressions."""
2+
33
import logging
44
import re
55
import sys

src/cosmic_ray/tools/filters/pragma_no_mutate.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""A filter that uses "# pragma: no mutate" to determine when specific mutations
22
should be skipped.
33
"""
4+
45
import logging
56
import re
67
import sys

src/cosmic_ray/tools/survival_rate.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
type=click.FloatRange(0, 100),
2626
default=None,
2727
help="Exit with a non-zero code if the survival rate is larger than <max_value> or the calculated confidence interval "
28-
"is above the <max_value> (if --estimate is used). Specified as percentage.",
28+
"is above the <max_value> (if --estimate is used). Specified as percentage.",
2929
)
3030
@click.argument("session-file", type=click.Path(dir_okay=False, readable=True, exists=True))
3131
def format_survival_rate(estimate, confidence, fail_over, session_file):
@@ -45,7 +45,9 @@ def format_survival_rate(estimate, confidence, fail_over, session_file):
4545
if not num_complete:
4646
conf_int = 0
4747
else:
48-
conf_int = math.sqrt(rate * (100 - rate) / num_complete) * z_score * (1 - math.sqrt(num_complete / num_items))
48+
conf_int = (
49+
math.sqrt(rate * (100 - rate) / num_complete) * z_score * (1 - math.sqrt(num_complete / num_items))
50+
)
4951
min_rate = rate - conf_int
5052
print(f"{min_rate:.2f} {rate:.2f} {rate + conf_int:.2f}")
5153

src/cosmic_ray/work_db.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,11 @@ def pending_work_items(self):
165165
"Iterable of all pending work items. In random order."
166166
with self._session_maker.begin() as session:
167167
completed_job_ids = session.query(WorkResultStorage.job_id)
168-
pending = session.query(WorkItemStorage).where(
169-
~WorkItemStorage.job_id.in_(completed_job_ids)).order_by(func.random())
168+
pending = (
169+
session.query(WorkItemStorage)
170+
.where(~WorkItemStorage.job_id.in_(completed_job_ids))
171+
.order_by(func.random())
172+
)
170173
return tuple(_work_item_from_storage(work_item) for work_item in pending)
171174

172175
@property
@@ -208,6 +211,7 @@ def use_db(path, mode=WorkDB.Mode.create):
208211

209212
class WorkItemStorage(Base):
210213
"Database model for WorkItem."
214+
211215
__tablename__ = "work_items"
212216

213217
job_id = Column(String, primary_key=True)
@@ -216,6 +220,7 @@ class WorkItemStorage(Base):
216220

217221
class MutationSpecStorage(Base):
218222
"Database model for MutationSpecs"
223+
219224
__tablename__ = "mutation_specs"
220225
module_path = Column(String)
221226
operator_name = Column(String)
@@ -231,6 +236,7 @@ class MutationSpecStorage(Base):
231236

232237
class WorkResultStorage(Base):
233238
"Database model for WorkResult."
239+
234240
__tablename__ = "work_results"
235241

236242
worker_outcome = Column(Enum(WorkerOutcome))

src/cosmic_ray/work_item.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""Classes for describing work and results.
2-
"""
1+
"""Classes for describing work and results."""
2+
33
import dataclasses
44
import enum
55
import pathlib
@@ -56,6 +56,7 @@ def is_killed(self):
5656
@dataclasses.dataclass(frozen=True)
5757
class MutationSpec:
5858
"Description of a single mutation."
59+
5960
module_path: Path
6061
operator_name: str
6162
occurrence: int

tests/e2e/test_e2e.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,4 @@ def test_baseline_with_pytest_filter(example_project_root, session):
109109
capture_output=True,
110110
)
111111

112-
assert result.returncode == 0
112+
assert result.returncode == 0

tests/resources/example_project/adam/adam_1.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""adam.adam_1
2-
"""
1+
"""adam.adam_1"""
32

43
# pylint: disable=C0111
54
import operator

tests/resources/example_project/adam/adam_2.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""adam.adam_2
2-
"""
1+
"""adam.adam_2"""
32

43

54
def trigger_infinite_loop():

tests/unittests/operators/test_operator_samples.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""Tests for the various mutation operators.
2-
"""
1+
"""Tests for the various mutation operators."""
2+
33
import parso
44
import pytest
55

tests/unittests/test_ast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ def foo():
1818
with module_filepath.open("wb") as f:
1919
f.write(input_code)
2020
ast = get_ast_from_path(module_filepath)
21-
assert ast.get_code().encode('latin-1') == input_code
21+
assert ast.get_code().encode("latin-1") == input_code

tests/unittests/test_config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""Tests for config loading functions.
2-
"""
1+
"""Tests for config loading functions."""
2+
33
import io
44

55
import pytest

0 commit comments

Comments
 (0)