diff options
author | Tanvi Moharir <74228962+tanvimoharir@users.noreply.github.com> | 2021-11-13 17:36:01 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-13 13:06:01 +0100 |
commit | 1a20bc5c50a7c73abaad8b364f78055d4e60146d (patch) | |
tree | cb00f346a16230cfcb960c4e3a900513e3d09210 | |
parent | 8e15c1dd9d0dc07cb9794353a32cd37d57f2d569 (diff) | |
download | pylint-git-1a20bc5c50a7c73abaad8b364f78055d4e60146d.tar.gz |
Pylint fix for invalid TOML config (#4720)
* Fix crashes during toml configuration parsing
Add test for current 'pyproject.toml' issues. Add a 'bad-configuration-section'
message for bad toml configuration
We can detect bad top level option when reading the toml but we do not catch
all the problem in toml because we don't know what is expected so we can't
recommend. See #5259
Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
17 files changed, 94 insertions, 5 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 64dc3f349..5938788f3 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -529,6 +529,9 @@ contributors: * Marcin Kurczewski (rr-): contributor +* Tanvi Moharir: contributor + - Fix for invalid toml config + * Eisuke Kawashima (e-kwsm): contributor * Daniel van Noord (DanielNoord): contributor @@ -158,6 +158,17 @@ Release date: TBA Closes #5261 +* Crashes when a list is encountered in a toml configuration do not happen anymore. + + Closes #4580 + +* A new ``bad-configuration-section`` checker was added that will emit for misplaced option + in pylint's top level namespace for toml configuration. Top-level dictionaries or option defined + in the wrong section will still silently not be taken into account, which is tracked in a + follow-up issue. + + Follow-up in #5259 + * Added new checker ``useless-with-lock`` to find incorrect usage of with statement and threading module locks. Emitted when ``with threading.Lock():`` is used instead of ``with lock_instance:``. diff --git a/doc/whatsnew/2.12.rst b/doc/whatsnew/2.12.rst index be99f68d9..eaeb1d790 100644 --- a/doc/whatsnew/2.12.rst +++ b/doc/whatsnew/2.12.rst @@ -56,6 +56,13 @@ New checkers Closes #5208 +* A new ``bad-configuration-section`` checker was added that will emit for misplaced option + in pylint's top level namespace for toml configuration. Top-level dictionaries or option defined + in the wrong section will still silently not be taken into account, which is tracked in a + follow-up issue. + + Follow-up in #5259 + Removed checkers ================ @@ -162,4 +169,8 @@ Other Changes Closes #5216 +* Crashes when a list is encountered in a toml configuration do not happen anymore. + + Closes #4580 + * Make yn validator case insensitive, to allow for ``True`` and ``False`` in config files. diff --git a/pylint/config/option_manager_mixin.py b/pylint/config/option_manager_mixin.py index 1871e7fd6..587467366 100644 --- a/pylint/config/option_manager_mixin.py +++ b/pylint/config/option_manager_mixin.py @@ -293,9 +293,8 @@ 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 + self, 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: @@ -305,16 +304,28 @@ class OptionsManagerMixIn: except KeyError: return for section, values in sections_values.items(): + section_name = section.upper() # TOML has rich types, convert values to # strings as ConfigParser expects. + if not isinstance(values, dict): + # This class is a mixin: add_message comes from the `PyLinter` class + self.add_message( # type: ignore + "bad-configuration-section", line=0, args=(section, values) + ) + continue 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 + else: + values[option] = str(value) + for option, value in values.items(): + try: + parser.set(section_name, option, value=value) + except configparser.NoSectionError: + parser.add_section(section_name) + parser.set(section_name, option, value=value) def load_config_file(self): """Dispatch values previously read from a configuration file to each diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 7f4005070..41f72a70f 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -158,6 +158,11 @@ MSGS = { "bad-plugin-value", "Used when a bad value is used in 'load-plugins'.", ), + "E0014": ( + "Out-of-place setting encountered in top level configuration-section '%s' : '%s'", + "bad-configuration-section", + "Used when we detect a setting in the top level of a toml configuration that shouldn't be there.", + ), } diff --git a/tests/config/functional/toml/issue_4580/correct_basic_name_group.toml b/tests/config/functional/toml/issue_4580/correct_basic_name_group.toml new file mode 100644 index 000000000..845f5e8bb --- /dev/null +++ b/tests/config/functional/toml/issue_4580/correct_basic_name_group.toml @@ -0,0 +1,2 @@ +[tool.pylint.basic] +name-group = { "a"="b" } diff --git a/tests/config/functional/toml/issue_4580/correct_import_preferred_module.toml b/tests/config/functional/toml/issue_4580/correct_import_preferred_module.toml new file mode 100644 index 000000000..2f8fdb555 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/correct_import_preferred_module.toml @@ -0,0 +1,2 @@ +[tool.pylint.imports] +preferred-modules = { "a"="b" } diff --git a/tests/config/functional/toml/issue_4580/empty_list.out b/tests/config/functional/toml/issue_4580/empty_list.out new file mode 100644 index 000000000..c25f99b30 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/empty_list.out @@ -0,0 +1,2 @@ +************* Module {abspath} +{relpath}:1:0: E0014: Out-of-place setting encountered in top level configuration-section 'load-plugins' : '[]' (bad-configuration-section) diff --git a/tests/config/functional/toml/issue_4580/empty_list.toml b/tests/config/functional/toml/issue_4580/empty_list.toml new file mode 100644 index 000000000..28bd65c60 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/empty_list.toml @@ -0,0 +1,3 @@ +[tool.pylint] +# load-plugins does not belong in top level section +load-plugins = [] diff --git a/tests/config/functional/toml/issue_4580/invalid_data_for_basic.out b/tests/config/functional/toml/issue_4580/invalid_data_for_basic.out new file mode 100644 index 000000000..2654d87f0 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/invalid_data_for_basic.out @@ -0,0 +1,3 @@ +************* Module {abspath} +{relpath}:1:0: E0014: Out-of-place setting encountered in top level configuration-section 'load-plugins' : '[]' (bad-configuration-section) +{relpath}:1:0: E0014: Out-of-place setting encountered in top level configuration-section 'disable' : 'logging-not-lazy,logging-format-interpolation' (bad-configuration-section) diff --git a/tests/config/functional/toml/issue_4580/invalid_data_for_basic.toml b/tests/config/functional/toml/issue_4580/invalid_data_for_basic.toml new file mode 100644 index 000000000..b2e4fe927 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/invalid_data_for_basic.toml @@ -0,0 +1,4 @@ +[tool.pylint] +# Both disable and load-plugins do not belong in the top level section +load-plugins = [] +disable = "logging-not-lazy,logging-format-interpolation" diff --git a/tests/config/functional/toml/issue_4580/invalid_data_for_import.result.json b/tests/config/functional/toml/issue_4580/invalid_data_for_import.result.json new file mode 100644 index 000000000..4805c7e54 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/invalid_data_for_import.result.json @@ -0,0 +1,5 @@ +{ + "functional_append": { + "disable": [["logging-not-lazy"], ["logging-format-interpolation"]] + } +} diff --git a/tests/config/functional/toml/issue_4580/invalid_data_for_import.toml b/tests/config/functional/toml/issue_4580/invalid_data_for_import.toml new file mode 100644 index 000000000..60af90624 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/invalid_data_for_import.toml @@ -0,0 +1,8 @@ +# Both disable and load-plugins do not belong in the imports section +# TODO This should be fixed in #5259 +[tool.pylint."imports"] +disable = [ + "logging-not-lazy", + "logging-format-interpolation", +] +preferred-modules = { "a"="b" } diff --git a/tests/config/functional/toml/issue_4580/rich_types.result.json b/tests/config/functional/toml/issue_4580/rich_types.result.json new file mode 100644 index 000000000..21938c319 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/rich_types.result.json @@ -0,0 +1,7 @@ +{ + "functional_append": { + "disable": [["logging-not-lazy"], ["logging-format-interpolation"]] + }, + "jobs": 10, + "reports": true +} diff --git a/tests/config/functional/toml/issue_4580/rich_types.toml b/tests/config/functional/toml/issue_4580/rich_types.toml new file mode 100644 index 000000000..ff46db0f5 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/rich_types.toml @@ -0,0 +1,7 @@ +[tool.pylint."messages control"] +disable = [ + "logging-not-lazy", + "logging-format-interpolation", +] +jobs = 10 +reports = true diff --git a/tests/config/functional/toml/issue_4580/top_level_disable.out b/tests/config/functional/toml/issue_4580/top_level_disable.out new file mode 100644 index 000000000..a63759758 --- /dev/null +++ b/tests/config/functional/toml/issue_4580/top_level_disable.out @@ -0,0 +1,2 @@ +************* Module {abspath} +{relpath}:1:0: E0014: Out-of-place setting encountered in top level configuration-section 'disable' : 'logging-not-lazy,logging-format-interpolation' (bad-configuration-section) diff --git a/tests/config/functional/toml/issue_4580/top_level_disable.toml b/tests/config/functional/toml/issue_4580/top_level_disable.toml new file mode 100644 index 000000000..b2f392b7a --- /dev/null +++ b/tests/config/functional/toml/issue_4580/top_level_disable.toml @@ -0,0 +1,3 @@ +[tool.pylint] +# disable does not belong in top level section +disable = "logging-not-lazy,logging-format-interpolation" |