Skip to content

Commit

Permalink
feat(fixers): add fixer for reraise
Browse files Browse the repository at this point in the history
  • Loading branch information
guilatrova committed Sep 12, 2021
1 parent 1428958 commit 041f67f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tryceratops/fixers/__init__.py
@@ -0,0 +1 @@
from .exception_block import VerboseReraiseFixer
43 changes: 43 additions & 0 deletions src/tryceratops/fixers/exception_block.py
@@ -0,0 +1,43 @@
import re
from abc import ABC, abstractmethod
from typing import List, Tuple

from tryceratops.violations import Violation, codes


class BaseFixer(ABC):
violation_code: Tuple[str, str]

@classmethod
def _filter_violations_in_scope(cls, violations: List[Violation]) -> List[Violation]:
scope_code, _ = cls.violation_code
relevant = [vio for vio in violations if vio.code == scope_code]
return relevant

@classmethod
@abstractmethod
def _perform_fix(cls, violation: Violation):
pass

@classmethod
def fix(cls, violations: List[Violation]):
relevant_violations = cls._filter_violations_in_scope(violations)
for violation in relevant_violations:
cls._perform_fix(violation)


class VerboseReraiseFixer(BaseFixer):
violation_code = codes.VERBOSE_RERAISE

@classmethod
def _perform_fix(cls, violation: Violation):
with open(violation.filename, "r+") as file:
all_lines = file.readlines()

guilty_line = all_lines[violation.line - 1]
new_line = re.sub(r"raise.*", "raise", guilty_line)
all_lines[violation.line - 1] = new_line

file.seek(0)
file.writelines(all_lines)
file.truncate()

0 comments on commit 041f67f

Please sign in to comment.