@@ -29,6 +29,7 @@ def __init__(self, argv: list[str] | None) -> None:
2929 for formatter in _formatting .FORMATTERS :
3030 formatter .set_config_namespace (self .config )
3131
32+ self .enabled_formatters = self .get_enabled_formatters ()
3233 self .check_files (self .config .files )
3334
3435 # pylint: disable-next=inconsistent-return-statements
@@ -70,12 +71,8 @@ def format_file(self, filename: Path) -> bool:
7071 new_tokeninfo = tokeninfo
7172
7273 if _utils .is_docstring (new_tokeninfo , tokens [index - 1 ]):
73- for formatter in _formatting .FORMATTERS :
74- if (
75- "default" in formatter .style
76- or any (i in formatter .style for i in self .config .style )
77- ) and getattr (self .config , formatter .name ):
78- new_tokeninfo = formatter .treat_token (new_tokeninfo )
74+ for _ , formatter in self .enabled_formatters .items ():
75+ new_tokeninfo = formatter .treat_token (new_tokeninfo )
7976 formatted_tokens .append (new_tokeninfo )
8077
8178 if tokeninfo != new_tokeninfo :
@@ -113,6 +110,19 @@ def format_file(self, filename: Path) -> bool:
113110
114111 return is_changed
115112
113+ def get_enabled_formatters (self ) -> dict [str , _formatting .Formatter ]:
114+ """Returns a dict of the enabled formatters."""
115+
116+ enabled = {}
117+ for formatter in _formatting .FORMATTERS :
118+ if (
119+ "default" in formatter .style
120+ or any (i in formatter .style for i in self .config .style )
121+ ) and getattr (self .config , formatter .name ):
122+ enabled [formatter .name ] = formatter
123+
124+ return enabled
125+
116126 def format_files (self , filepaths : list [Path ]) -> bool :
117127 """Format a list of files."""
118128 is_changed = False
0 commit comments