Skip to content

Commit 77f020b

Browse files
committed
move parsing the error messages to the formatter since that's where the returned values are used
1 parent 90ce5a5 commit 77f020b

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## [UNRELEASED]
8+
### Fixed
9+
10+
- fix parsing error messages when they are set in a configuration file
811

912
## [0.1.0] - 2023-02-19
1013
### Added

flake8_custom_error_messages/plugin.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass
2+
from typing import List
23

34
from flake8.formatting.default import Default
45
from flake8.options.manager import OptionManager
@@ -25,12 +26,35 @@ def format(self, error: Violation):
2526
return super().format(error)
2627

2728
def get_custom_error(self, code: str) -> "CustomError":
28-
for custom_error in self.options.error_messages:
29+
for custom_error in self.custom_errors:
2930
if code.startswith(custom_error.code):
3031
return custom_error
3132

3233
def after_init(self):
33-
pass
34+
error_messages = self.options.error_messages
35+
if not error_messages:
36+
msgs = []
37+
elif isinstance(error_messages, str):
38+
# parsed from the config file, so we need to convert it to a list
39+
# flake8 does not support parsing values containing spaces from config
40+
# files
41+
msgs = [
42+
line.strip()
43+
for line in error_messages.splitlines()
44+
if line.strip()
45+
]
46+
else:
47+
# when parsed from the command line, the value is already a list
48+
msgs = error_messages
49+
50+
# split the error code from the custom error message
51+
errors = []
52+
for msg in msgs:
53+
code = msg.split()[0]
54+
text = msg.replace(code, "", 1).strip()
55+
errors.append(CustomError(code, text))
56+
57+
self.custom_errors: List[CustomError] = errors
3458

3559

3660
class Plugin:
@@ -62,31 +86,6 @@ def add_options(cls, options_manager: OptionManager):
6286
nargs="+",
6387
)
6488

65-
@classmethod
66-
def parse_options(cls, options):
67-
if not options.error_messages:
68-
msgs = []
69-
elif isinstance(options.error_messages, str):
70-
# parsed from the config file, so we need to convert it to a list
71-
# flake8 does not support parsing values containing spaces from config
72-
# files
73-
msgs = [
74-
line.strip()
75-
for line in options.error_messages.splitlines()
76-
if line.strip()
77-
]
78-
else:
79-
# when parsed from the command line, the value is already a list
80-
msgs = options.error_messages
81-
82-
# split the error code from the custom error message
83-
errors = []
84-
for msg in msgs:
85-
code = msg.split()[0]
86-
text = msg.replace(code, "", 1).strip()
87-
errors.append(CustomError(code, text))
88-
cls.error_messages = errors
89-
9089

9190
@dataclass
9291
class CustomError:

0 commit comments

Comments
 (0)