11from dataclasses import dataclass
2+ from typing import List
23
34from flake8 .formatting .default import Default
45from 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
3660class 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
9291class CustomError :
0 commit comments