Skip to content

Commit d5ecfe1

Browse files
1 parent c6fcd01 commit d5ecfe1

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

pylint/config/option_manager_mixin.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
import optparse # pylint: disable=deprecated-module
1111
import os
1212
import sys
13+
from pathlib import Path
1314
from types import ModuleType
14-
from typing import Dict, List, Optional, TextIO, Tuple
15+
from typing import Dict, List, Optional, TextIO, Tuple, Union
1516

1617
import toml
1718

@@ -269,34 +270,14 @@ def read_config_file(self, config_file=None, verbose=None):
269270
raise OSError(f"The config file {config_file} doesn't exist!")
270271

271272
use_config_file = config_file and os.path.exists(config_file)
272-
if use_config_file: # pylint: disable=too-many-nested-blocks
273+
if use_config_file:
273274
parser = self.cfgfile_parser
274-
275275
if config_file.endswith(".toml"):
276-
with open(config_file, encoding="utf-8") as fp:
277-
content = toml.load(fp)
278-
279-
try:
280-
sections_values = content["tool"]["pylint"]
281-
except KeyError:
282-
pass
283-
else:
284-
for section, values in sections_values.items():
285-
# TOML has rich types, convert values to
286-
# strings as ConfigParser expects.
287-
for option, value in values.items():
288-
if isinstance(value, bool):
289-
values[option] = "yes" if value else "no"
290-
elif isinstance(value, (int, float)):
291-
values[option] = str(value)
292-
elif isinstance(value, list):
293-
values[option] = ",".join(value)
294-
parser._sections[section.upper()] = values
276+
self._parse_toml(config_file, parser)
295277
else:
296278
# Use this encoding in order to strip the BOM marker, if any.
297279
with open(config_file, encoding="utf_8_sig") as fp:
298280
parser.read_file(fp)
299-
300281
# normalize sections'title
301282
for sect, values in list(parser._sections.items()):
302283
if sect.startswith("pylint."):
@@ -311,6 +292,30 @@ def read_config_file(self, config_file=None, verbose=None):
311292
msg = "No config file found, using default configuration"
312293
print(msg, file=sys.stderr)
313294

295+
@staticmethod
296+
def _parse_toml(
297+
config_file: Union[Path, str], parser: configparser.ConfigParser
298+
) -> None:
299+
"""Upgrade the parser from a toml configuration file."""
300+
with open(config_file, encoding="utf-8") as fp:
301+
content = toml.load(fp)
302+
try:
303+
sections_values = content["tool"]["pylint"]
304+
except KeyError:
305+
pass
306+
else:
307+
for section, values in sections_values.items():
308+
# TOML has rich types, convert values to
309+
# strings as ConfigParser expects.
310+
for option, value in values.items():
311+
if isinstance(value, bool):
312+
values[option] = "yes" if value else "no"
313+
elif isinstance(value, (int, float)):
314+
values[option] = str(value)
315+
elif isinstance(value, list):
316+
values[option] = ",".join(value)
317+
parser._sections[section.upper()] = values # type: ignore
318+
314319
def load_config_file(self):
315320
"""Dispatch values previously read from a configuration file to each
316321
options provider)"""

0 commit comments

Comments
 (0)