-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix TOML boolean false values for store_true options #10903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marwinsteiner
wants to merge
2
commits into
pylint-dev:main
Choose a base branch
from
marwinsteiner:fix/toml-boolean-false-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix ``store_true`` boolean config options (like ``exit-zero``) in TOML files not respecting ``false`` values. Setting ``exit-zero = false`` in a TOML config previously behaved the same as ``exit-zero = true``. | ||
|
|
||
| Closes #8460 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import configparser | ||
| import os | ||
| import sys | ||
|
|
@@ -60,7 +61,9 @@ | |
| return False | ||
|
|
||
| @staticmethod | ||
| def parse_toml_file(file_path: Path) -> PylintConfigFileData: | ||
| def parse_toml_file( | ||
| file_path: Path, store_true_options: set[str] | None = None | ||
| ) -> PylintConfigFileData: | ||
| """Parse and handle errors of a toml configuration file. | ||
|
|
||
| Raises ``tomllib.TOMLDecodeError``. | ||
|
|
@@ -77,18 +80,38 @@ | |
| for opt, values in sections_values.items(): | ||
| if isinstance(values, dict): | ||
| for config, value in values.items(): | ||
| value = _parse_rich_type_value(value) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a lot of duplication. Have you explored if we can do this in |
||
| config_content[config] = value | ||
| options += [f"--{config}", value] | ||
| if ( | ||
| isinstance(value, bool) | ||
| and store_true_options | ||
| and config in store_true_options | ||
| ): | ||
| config_content[config] = str(value) | ||
| if value: | ||
| options.append(f"--{config}") | ||
| else: | ||
| value = _parse_rich_type_value(value) | ||
| config_content[config] = value | ||
| options += [f"--{config}", value] | ||
| else: | ||
| values = _parse_rich_type_value(values) | ||
| config_content[opt] = values | ||
| options += [f"--{opt}", values] | ||
| if ( | ||
| isinstance(values, bool) | ||
| and store_true_options | ||
| and opt in store_true_options | ||
| ): | ||
| config_content[opt] = str(values) | ||
| if values: | ||
| options.append(f"--{opt}") | ||
| else: | ||
| values = _parse_rich_type_value(values) | ||
| config_content[opt] = values | ||
| options += [f"--{opt}", values] | ||
| return config_content, options | ||
|
|
||
| @staticmethod | ||
| def parse_config_file( | ||
| file_path: Path | None, verbose: bool | ||
| file_path: Path | None, | ||
| verbose: bool, | ||
| store_true_options: set[str] | None = None, | ||
| ) -> PylintConfigFileData: | ||
| """Parse a config file and return str-str pairs. | ||
|
|
||
|
|
@@ -109,7 +132,7 @@ | |
| print(f"Using config file {file_path}", file=sys.stderr) | ||
|
|
||
| if file_path.suffix == ".toml": | ||
| return _RawConfParser.parse_toml_file(file_path) | ||
| return _RawConfParser.parse_toml_file(file_path, store_true_options) | ||
| return _RawConfParser.parse_ini_file(file_path) | ||
|
|
||
|
|
||
|
|
@@ -122,8 +145,18 @@ | |
|
|
||
| def parse_config_file(self, file_path: Path | None) -> PylintConfigFileData: | ||
| """Parse a config file and return str-str pairs.""" | ||
| # Build a set of store_true option names (bare, without --) | ||
| # so parse_toml_file can handle boolean values correctly. | ||
| store_true_options: set[str] = set() | ||
| for action in self.linter._arg_parser._actions: | ||
| if isinstance(action, argparse._StoreTrueAction): | ||
| for opt_string in action.option_strings: | ||
| store_true_options.add(opt_string.lstrip("-")) | ||
|
|
||
| try: | ||
| return _RawConfParser.parse_config_file(file_path, self.verbose_mode) | ||
| return _RawConfParser.parse_config_file( | ||
| file_path, self.verbose_mode, store_true_options | ||
| ) | ||
| except (configparser.Error, tomllib.TOMLDecodeError) as e: | ||
| self.linter.add_message("config-parse-error", line=0, args=str(e)) | ||
| return {}, [] | ||
3 changes: 3 additions & 0 deletions
3
tests/config/functional/toml/issue_8460/boolean_false_value.result.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "exit_zero": false | ||
| } |
6 changes: 6 additions & 0 deletions
6
tests/config/functional/toml/issue_8460/boolean_false_value.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Test that boolean false values in TOML config are properly respected | ||
| # for store_true options. Setting exit-zero = false should NOT cause | ||
| # pylint to always exit with code 0. | ||
|
|
||
| [tool.pylint.main] | ||
| exit-zero = false |
3 changes: 3 additions & 0 deletions
3
tests/config/functional/toml/issue_8460/boolean_true_value.result.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "exit_zero": true | ||
| } |
5 changes: 5 additions & 0 deletions
5
tests/config/functional/toml/issue_8460/boolean_true_value.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Test that boolean true values in TOML config still work correctly | ||
| # for store_true options. | ||
|
|
||
| [tool.pylint.main] | ||
| exit-zero = true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this optional?