Skip to content

Commit

Permalink
Uses ruff to format code (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairhenrique authored Oct 14, 2024
1 parent c86fdc2 commit a954373
Show file tree
Hide file tree
Showing 31 changed files with 59 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ jobs:
- name: Install dependencies
run: uv sync -i https://pypi.org/simple # Ignore download URLs in uv.lock.
- name: Lint with ruff
run: uv run ruff check src/cosmic_ray
run: |
uv run ruff check .
uv run ruff format --check .
- name: Test with pytest
run: uv run pytest tests --run-slow

Expand Down
5 changes: 4 additions & 1 deletion src/cosmic_ray/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Here we manage command-line parsing and launching of the internal machinery that does mutation testing.
"""

import dataclasses
import json
import logging
Expand Down Expand Up @@ -261,7 +262,9 @@ def mutate_and_test(module_path, operator, occurrence, test_command, keep_stdout
"""
with open(os.devnull, "w") as devnull:
with redirect_stdout(sys.stdout if keep_stdout else devnull):
work_result = cosmic_ray.mutating.mutate_and_test(Path(module_path), operator, occurrence, test_command, None)
work_result = cosmic_ray.mutating.mutate_and_test(
Path(module_path), operator, occurrence, test_command, None
)

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

Expand Down
1 change: 1 addition & 0 deletions src/cosmic_ray/commands/execute.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"Implementation of the 'execute' command."

import logging
import os

Expand Down
1 change: 1 addition & 0 deletions src/cosmic_ray/commands/init.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"Implementation of the 'init' command."

import logging
import uuid
from typing import Iterable
Expand Down
3 changes: 1 addition & 2 deletions src/cosmic_ray/commands/new_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Implementation of the 'new-config' command.
"""
"""Implementation of the 'new-config' command."""

import os.path

Expand Down
1 change: 1 addition & 0 deletions src/cosmic_ray/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Configuration module."""

import logging
import sys
from contextlib import contextmanager
Expand Down
1 change: 1 addition & 0 deletions src/cosmic_ray/distribution/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
[cosmic-ray.distributor.http]
worker-urls = ['http://localhost:9876', 'http://localhost:9877']
"""

import asyncio
import logging
from pathlib import Path
Expand Down
4 changes: 2 additions & 2 deletions src/cosmic_ray/mutating.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Support for making mutations to source code.
"""
"""Support for making mutations to source code."""

import contextlib
import difflib
import logging
Expand Down
3 changes: 1 addition & 2 deletions src/cosmic_ray/operators/boolean_replacer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Implementation of the boolean replacement operators.
"""
"""Implementation of the boolean replacement operators."""

import parso.python.tree

Expand Down
2 changes: 2 additions & 0 deletions src/cosmic_ray/operators/break_continue.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

class ReplaceBreakWithContinue(KeywordReplacementOperator):
"Operator which replaces 'break' with 'continue'."

from_keyword = "break"
to_keyword = "continue"


class ReplaceContinueWithBreak(KeywordReplacementOperator):
"Operator which replaces 'continue' with 'break'."

from_keyword = "continue"
to_keyword = "break"
2 changes: 2 additions & 0 deletions src/cosmic_ray/operators/comparison_operator_replacement.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module contains mutation operators which replace one
comparison operator with another.
"""

import itertools
from enum import Enum

Expand All @@ -13,6 +14,7 @@

class ComparisonOperators(Enum):
"All comparison operators that we mutate."

Eq = "=="
NotEq = "!="
Lt = "<"
Expand Down
3 changes: 1 addition & 2 deletions src/cosmic_ray/operators/number_replacer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Implementation of the NumberReplacer operator.
"""
"""Implementation of the NumberReplacer operator."""

import parso

Expand Down
3 changes: 1 addition & 2 deletions src/cosmic_ray/operators/provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Operator-provider plugin for all core cosmic ray operators.
"""
"""Operator-provider plugin for all core cosmic ray operators."""

import itertools

Expand Down
6 changes: 3 additions & 3 deletions src/cosmic_ray/operators/unary_operator_replacement.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Implementation of the unary-operator-replacement operator.
"""
"""Implementation of the unary-operator-replacement operator."""

from enum import Enum
from itertools import permutations
Expand All @@ -12,6 +11,7 @@

class UnaryOperators(Enum):
"All unary operators that we mutate."

UAdd = "+"
USub = "-"
Invert = "~"
Expand Down Expand Up @@ -51,7 +51,7 @@ def mutate(self, node, index):
@classmethod
def examples(cls):
from_code = f"{from_op.value}1"
to_code = from_code[len(from_op.value):]
to_code = from_code[len(from_op.value) :]

if to_op is not UnaryOperators.Nothing:
to_code = to_op.value + to_code
Expand Down
1 change: 1 addition & 0 deletions src/cosmic_ray/operators/variable_inserter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of the variable-inserter operator."""

import random

import parso.python.tree
Expand Down
1 change: 1 addition & 0 deletions src/cosmic_ray/operators/variable_replacer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of the variable-replacement operator."""

from random import randint

from parso.python.tree import ExprStmt, Leaf, Number
Expand Down
5 changes: 2 additions & 3 deletions src/cosmic_ray/plugins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Query and retrieve the various plugins in Cosmic Ray.
"""
"""Query and retrieve the various plugins in Cosmic Ray."""

import logging

Expand Down Expand Up @@ -34,7 +33,7 @@ def get_operator(name):
"""
sep = name.index("/")
provider_name = name[:sep]
operator_name = name[sep + 1:]
operator_name = name[sep + 1 :]

provider = OPERATOR_PROVIDERS[provider_name]
return provider[operator_name]
Expand Down
4 changes: 2 additions & 2 deletions src/cosmic_ray/tools/badge.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Tool for creating badge.
"""
"""Tool for creating badge."""

import os
from logging import getLogger

Expand Down
4 changes: 2 additions & 2 deletions src/cosmic_ray/tools/filters/filter_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""A simple base for creating common types of work-db filters.
"""
"""A simple base for creating common types of work-db filters."""

import argparse
import logging
import sys
Expand Down
1 change: 1 addition & 0 deletions src/cosmic_ray/tools/filters/git.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A filter that uses git to determine when specific files/lines
should be skipped.
"""

import logging
import re
import subprocess
Expand Down
4 changes: 2 additions & 2 deletions src/cosmic_ray/tools/filters/operators_filter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""An filter that removes operators based on regular expressions.
"""
"""An filter that removes operators based on regular expressions."""

import logging
import re
import sys
Expand Down
1 change: 1 addition & 0 deletions src/cosmic_ray/tools/filters/pragma_no_mutate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A filter that uses "# pragma: no mutate" to determine when specific mutations
should be skipped.
"""

import logging
import re
import sys
Expand Down
6 changes: 4 additions & 2 deletions src/cosmic_ray/tools/survival_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
type=click.FloatRange(0, 100),
default=None,
help="Exit with a non-zero code if the survival rate is larger than <max_value> or the calculated confidence interval "
"is above the <max_value> (if --estimate is used). Specified as percentage.",
"is above the <max_value> (if --estimate is used). Specified as percentage.",
)
@click.argument("session-file", type=click.Path(dir_okay=False, readable=True, exists=True))
def format_survival_rate(estimate, confidence, fail_over, session_file):
Expand All @@ -45,7 +45,9 @@ def format_survival_rate(estimate, confidence, fail_over, session_file):
if not num_complete:
conf_int = 0
else:
conf_int = math.sqrt(rate * (100 - rate) / num_complete) * z_score * (1 - math.sqrt(num_complete / num_items))
conf_int = (
math.sqrt(rate * (100 - rate) / num_complete) * z_score * (1 - math.sqrt(num_complete / num_items))
)
min_rate = rate - conf_int
print(f"{min_rate:.2f} {rate:.2f} {rate + conf_int:.2f}")

Expand Down
10 changes: 8 additions & 2 deletions src/cosmic_ray/work_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,11 @@ def pending_work_items(self):
"Iterable of all pending work items. In random order."
with self._session_maker.begin() as session:
completed_job_ids = session.query(WorkResultStorage.job_id)
pending = session.query(WorkItemStorage).where(
~WorkItemStorage.job_id.in_(completed_job_ids)).order_by(func.random())
pending = (
session.query(WorkItemStorage)
.where(~WorkItemStorage.job_id.in_(completed_job_ids))
.order_by(func.random())
)
return tuple(_work_item_from_storage(work_item) for work_item in pending)

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

class WorkItemStorage(Base):
"Database model for WorkItem."

__tablename__ = "work_items"

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

class MutationSpecStorage(Base):
"Database model for MutationSpecs"

__tablename__ = "mutation_specs"
module_path = Column(String)
operator_name = Column(String)
Expand All @@ -231,6 +236,7 @@ class MutationSpecStorage(Base):

class WorkResultStorage(Base):
"Database model for WorkResult."

__tablename__ = "work_results"

worker_outcome = Column(Enum(WorkerOutcome))
Expand Down
5 changes: 3 additions & 2 deletions src/cosmic_ray/work_item.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Classes for describing work and results.
"""
"""Classes for describing work and results."""

import dataclasses
import enum
import pathlib
Expand Down Expand Up @@ -56,6 +56,7 @@ def is_killed(self):
@dataclasses.dataclass(frozen=True)
class MutationSpec:
"Description of a single mutation."

module_path: Path
operator_name: str
occurrence: int
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ def test_baseline_with_pytest_filter(example_project_root, session):
capture_output=True,
)

assert result.returncode == 0
assert result.returncode == 0
3 changes: 1 addition & 2 deletions tests/resources/example_project/adam/adam_1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""adam.adam_1
"""
"""adam.adam_1"""

# pylint: disable=C0111
import operator
Expand Down
3 changes: 1 addition & 2 deletions tests/resources/example_project/adam/adam_2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""adam.adam_2
"""
"""adam.adam_2"""


def trigger_infinite_loop():
Expand Down
4 changes: 2 additions & 2 deletions tests/unittests/operators/test_operator_samples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Tests for the various mutation operators.
"""
"""Tests for the various mutation operators."""

import parso
import pytest

Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def foo():
with module_filepath.open("wb") as f:
f.write(input_code)
ast = get_ast_from_path(module_filepath)
assert ast.get_code().encode('latin-1') == input_code
assert ast.get_code().encode("latin-1") == input_code
4 changes: 2 additions & 2 deletions tests/unittests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Tests for config loading functions.
"""
"""Tests for config loading functions."""

import io

import pytest
Expand Down

0 comments on commit a954373

Please sign in to comment.