Skip to content

Commit bc4faf4

Browse files
committed
Fix TOML boolean false values not respected for store_true config options
1 parent 0d77e90 commit bc4faf4

7 files changed

Lines changed: 57 additions & 10 deletions

File tree

custom_dict.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ lineset's
191191
linesets
192192
linkers
193193
linter
194+
linter's
194195
linux
195196
listcomp
196197
Logilab
@@ -228,6 +229,7 @@ mymodule
228229
mypy
229230
namedtuple
230231
namespace
232+
natively
231233
newsfile
232234
newstyle
233235
nl

doc/whatsnew/fragments/8460.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
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``.
2+
3+
Closes #8460

pylint/config/config_file_parser.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from __future__ import annotations
88

9+
import argparse
910
import configparser
1011
import os
1112
import sys
@@ -60,7 +61,9 @@ def _ini_file_with_sections(file_path: Path) -> bool:
6061
return False
6162

6263
@staticmethod
63-
def parse_toml_file(file_path: Path) -> PylintConfigFileData:
64+
def parse_toml_file(
65+
file_path: Path, store_true_options: set[str] | None = None
66+
) -> PylintConfigFileData:
6467
"""Parse and handle errors of a toml configuration file.
6568
6669
Raises ``tomllib.TOMLDecodeError``.
@@ -77,18 +80,30 @@ def parse_toml_file(file_path: Path) -> PylintConfigFileData:
7780
for opt, values in sections_values.items():
7881
if isinstance(values, dict):
7982
for config, value in values.items():
80-
value = _parse_rich_type_value(value)
81-
config_content[config] = value
82-
options += [f"--{config}", value]
83+
if isinstance(value, bool) and store_true_options and config in store_true_options:
84+
config_content[config] = str(value)
85+
if value:
86+
options.append(f"--{config}")
87+
else:
88+
value = _parse_rich_type_value(value)
89+
config_content[config] = value
90+
options += [f"--{config}", value]
8391
else:
84-
values = _parse_rich_type_value(values)
85-
config_content[opt] = values
86-
options += [f"--{opt}", values]
92+
if isinstance(values, bool) and store_true_options and opt in store_true_options:
93+
config_content[opt] = str(values)
94+
if values:
95+
options.append(f"--{opt}")
96+
else:
97+
values = _parse_rich_type_value(values)
98+
config_content[opt] = values
99+
options += [f"--{opt}", values]
87100
return config_content, options
88101

89102
@staticmethod
90103
def parse_config_file(
91-
file_path: Path | None, verbose: bool
104+
file_path: Path | None,
105+
verbose: bool,
106+
store_true_options: set[str] | None = None,
92107
) -> PylintConfigFileData:
93108
"""Parse a config file and return str-str pairs.
94109
@@ -109,7 +124,7 @@ def parse_config_file(
109124
print(f"Using config file {file_path}", file=sys.stderr)
110125

111126
if file_path.suffix == ".toml":
112-
return _RawConfParser.parse_toml_file(file_path)
127+
return _RawConfParser.parse_toml_file(file_path, store_true_options)
113128
return _RawConfParser.parse_ini_file(file_path)
114129

115130

@@ -122,8 +137,18 @@ def __init__(self, verbose: bool, linter: PyLinter) -> None:
122137

123138
def parse_config_file(self, file_path: Path | None) -> PylintConfigFileData:
124139
"""Parse a config file and return str-str pairs."""
140+
# Build a set of store_true option names (bare, without --)
141+
# so parse_toml_file can handle boolean values correctly.
142+
store_true_options: set[str] = set()
143+
for action in self.linter._arg_parser._actions:
144+
if isinstance(action, argparse._StoreTrueAction):
145+
for opt_string in action.option_strings:
146+
store_true_options.add(opt_string.lstrip("-"))
147+
125148
try:
126-
return _RawConfParser.parse_config_file(file_path, self.verbose_mode)
149+
return _RawConfParser.parse_config_file(
150+
file_path, self.verbose_mode, store_true_options
151+
)
127152
except (configparser.Error, tomllib.TOMLDecodeError) as e:
128153
self.linter.add_message("config-parse-error", line=0, args=str(e))
129154
return {}, []
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_zero": false
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Test that boolean false values in TOML config are properly respected
2+
# for store_true options. Setting exit-zero = false should NOT cause
3+
# pylint to always exit with code 0.
4+
5+
[tool.pylint.main]
6+
exit-zero = false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_zero": true
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Test that boolean true values in TOML config still work correctly
2+
# for store_true options.
3+
4+
[tool.pylint.main]
5+
exit-zero = true

0 commit comments

Comments
 (0)