1 """Default formatting class for Flake8."""
2 from __future__
import annotations
4 from flake8
.formatting
import base
5 from flake8
.violation
import Violation
14 "magenta": "\033[35m",
19 COLORS_OFF
= {k
: "" for k
in COLORS
}
22 class SimpleFormatter(base
.BaseFormatter
):
23 """Simple abstraction for Default and Pylint formatter commonality.
25 Sub-classes of this need to define an ``error_format`` attribute in order
26 to succeed. The ``format`` method relies on that attribute and expects the
27 ``error_format`` string to use the old-style formatting strings with named
40 def format(self
, error
: Violation
) -> str |
None:
41 """Format and write error out.
43 If an output filename is specified, write formatted errors to that
44 file. Otherwise, print the formatted error to standard out.
46 return self
.error_format
% {
49 "path": error
.filename
,
50 "row": error
.line_number
,
51 "col": error
.column_number
,
52 **(COLORS
if self
.color
else COLORS_OFF
),
56 class Default(SimpleFormatter
):
57 """Default formatter for Flake8.
59 This also handles backwards compatibility for people specifying a custom
64 "%(bold)s%(path)s%(reset)s"
65 "%(cyan)s:%(reset)s%(row)d%(cyan)s:%(reset)s%(col)d%(cyan)s:%(reset)s "
66 "%(bold)s%(red)s%(code)s%(reset)s %(text)s"
69 def after_init(self
) -> None:
70 """Check for a custom format string."""
71 if self
.options
.format
.lower() != "default":
72 self
.error_format
= self
.options
.format
75 class Pylint(SimpleFormatter
):
76 """Pylint formatter for Flake8."""
78 error_format
= "%(path)s:%(row)d: [%(code)s] %(text)s"
81 class FilenameOnly(SimpleFormatter
):
82 """Only print filenames, e.g., flake8 -q."""
84 error_format
= "%(path)s"
86 def after_init(self
) -> None:
87 """Initialize our set of filenames."""
88 self
.filenames_already_printed
: set[str] = set()
90 def show_source(self
, error
: Violation
) -> str |
None:
91 """Do not include the source code."""
93 def format(self
, error
: Violation
) -> str |
None:
94 """Ensure we only print each error once."""
95 if error
.filename
not in self
.filenames_already_printed
:
96 self
.filenames_already_printed
.add(error
.filename
)
97 return super().format(error
)
102 class Nothing(base
.BaseFormatter
):
103 """Print absolutely nothing."""
105 def format(self
, error
: Violation
) -> str |
None:
108 def show_source(self
, error
: Violation
) -> str |
None:
109 """Do not print the source."""