summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2021-10-29 09:46:13 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-11-08 10:14:51 +0100
commitabd31cd223f447561009cbe91b22e91618e031d6 (patch)
treedbad8fff1467b72def454e1171d2a3dab233a95b
parentc6fcd01f268243b891cec96c19a6fc93595f4e17 (diff)
downloadpylint-git-abd31cd223f447561009cbe91b22e91618e031d6.tar.gz
Create a function for toml parsing
Handle https://github.com/PyCQA/pylint/pull/4720\#discussion_r742853160
-rw-r--r--pylint/config/option_manager_mixin.py51
1 files changed, 28 insertions, 23 deletions
diff --git a/pylint/config/option_manager_mixin.py b/pylint/config/option_manager_mixin.py
index f18fcfeca..34e0b3042 100644
--- a/pylint/config/option_manager_mixin.py
+++ b/pylint/config/option_manager_mixin.py
@@ -10,8 +10,9 @@ import functools
import optparse # pylint: disable=deprecated-module
import os
import sys
+from pathlib import Path
from types import ModuleType
-from typing import Dict, List, Optional, TextIO, Tuple
+from typing import Dict, List, Optional, TextIO, Tuple, Union
import toml
@@ -269,34 +270,14 @@ class OptionsManagerMixIn:
raise OSError(f"The config file {config_file} doesn't exist!")
use_config_file = config_file and os.path.exists(config_file)
- if use_config_file: # pylint: disable=too-many-nested-blocks
+ if use_config_file:
parser = self.cfgfile_parser
-
if config_file.endswith(".toml"):
- with open(config_file, encoding="utf-8") as fp:
- content = toml.load(fp)
-
- try:
- sections_values = content["tool"]["pylint"]
- except KeyError:
- pass
- else:
- for section, values in sections_values.items():
- # TOML has rich types, convert values to
- # strings as ConfigParser expects.
- for option, value in values.items():
- if isinstance(value, bool):
- values[option] = "yes" if value else "no"
- elif isinstance(value, (int, float)):
- values[option] = str(value)
- elif isinstance(value, list):
- values[option] = ",".join(value)
- parser._sections[section.upper()] = values
+ self._parse_toml(config_file, parser)
else:
# Use this encoding in order to strip the BOM marker, if any.
with open(config_file, encoding="utf_8_sig") as fp:
parser.read_file(fp)
-
# normalize sections'title
for sect, values in list(parser._sections.items()):
if sect.startswith("pylint."):
@@ -311,6 +292,30 @@ class OptionsManagerMixIn:
msg = "No config file found, using default configuration"
print(msg, file=sys.stderr)
+ @staticmethod
+ def _parse_toml(
+ config_file: Union[Path, str], parser: configparser.ConfigParser
+ ) -> None:
+ """Parse and handle errors of a toml configuration file."""
+ with open(config_file, encoding="utf-8") as fp:
+ content = toml.load(fp)
+ try:
+ sections_values = content["tool"]["pylint"]
+ except KeyError:
+ pass
+ else:
+ for section, values in sections_values.items():
+ # TOML has rich types, convert values to
+ # strings as ConfigParser expects.
+ for option, value in values.items():
+ if isinstance(value, bool):
+ values[option] = "yes" if value else "no"
+ elif isinstance(value, (int, float)):
+ values[option] = str(value)
+ elif isinstance(value, list):
+ values[option] = ",".join(value)
+ parser._sections[section.upper()] = values # type: ignore
+
def load_config_file(self):
"""Dispatch values previously read from a configuration file to each
options provider)"""