1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING, ClassVar
if TYPE_CHECKING:
from pathlib import Path
from mypy.nodes import Node
@dataclass(frozen=True)
class ErrorCode:
"""
This class represents an error code id which can be used to enable and
disable errors in Refurb. The `path` field is used to tell Refurb that a
particular error should only apply to a given path instead of all paths,
which is the default.
"""
id: int
prefix: str = "FURB"
path: Path | None = None
@classmethod
def from_error(cls, err: type[Error]) -> ErrorCode:
return ErrorCode(err.code, err.prefix)
def __str__(self) -> str:
return f"{self.prefix}{self.id}"
@dataclass(frozen=True)
class ErrorCategory:
value: str
path: Path | None = None
ErrorClassifier = ErrorCategory | ErrorCode
@dataclass
class Error:
enabled: ClassVar[bool] = True
name: ClassVar[str | None] = None
prefix: ClassVar[str] = "FURB"
categories: ClassVar[tuple[str, ...]] = ()
code: ClassVar[int]
line: int
column: int
msg: str
filename: str | None = None
line_end: int | None = None
column_end: int | None = None
def __str__(self) -> str:
return f"{self.filename}:{self.line}:{self.column + 1} [{self.prefix}{self.code}]: {self.msg}" # noqa: E501
@classmethod
def from_node(cls, node: Node, msg: str | None = None) -> Error:
return cls(
node.line,
node.column,
line_end=node.end_line,
column_end=node.end_column,
msg=msg or cls.msg,
)
|