Skip to content

Commit 70d13a6

Browse files
committed
Fix TOML boolean false values not respected for store_true config options
When a TOML config file sets a store_true option like exit-zero = false, _parse_rich_type_value converts it to the string "False" and passes ["--exit-zero", "False"] to argparse. Since store_true actions don't consume a following argument, argparse treats the flag's mere presence as True, ignoring the "False" value entirely. Add _fix_store_true_options to _ConfigurationFileParser that inspects the linter's argparse actions to identify store_true options, then: - For true values: emits just the flag (no trailing value string) - For false values: omits the flag entirely so the default is used Closes #8460
1 parent 0d77e90 commit 70d13a6

6 files changed

Lines changed: 56 additions & 1 deletion

File tree

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: 36 additions & 1 deletion
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
@@ -120,10 +121,44 @@ def __init__(self, verbose: bool, linter: PyLinter) -> None:
120121
self.verbose_mode = verbose
121122
self.linter = linter
122123

124+
def _fix_store_true_options(self, options: list[str]) -> list[str]:
125+
"""Fix boolean values for store_true argparse actions.
126+
127+
TOML files can express boolean values natively (true/false). When these
128+
are converted to strings and passed as ["--flag", "True"/"False"],
129+
argparse store_true actions ignore the value and always set True just
130+
from the flag's presence. This method removes the stringified value for
131+
store_true options, and drops the flag entirely when the value is False.
132+
"""
133+
# Build a set of store_true option names from the linter's arg parser
134+
store_true_options: set[str] = set()
135+
for action in self.linter._arg_parser._actions:
136+
if isinstance(action, argparse._StoreTrueAction):
137+
store_true_options.update(action.option_strings)
138+
139+
fixed: list[str] = []
140+
i = 0
141+
while i < len(options):
142+
opt = options[i]
143+
if opt in store_true_options and i + 1 < len(options):
144+
value = options[i + 1].lower()
145+
if value in ("true", "1", "yes"):
146+
fixed.append(opt)
147+
# When False, omit the flag entirely so argparse uses the default.
148+
i += 2 # Skip the value in either case
149+
else:
150+
fixed.append(opt)
151+
i += 1
152+
return fixed
153+
123154
def parse_config_file(self, file_path: Path | None) -> PylintConfigFileData:
124155
"""Parse a config file and return str-str pairs."""
125156
try:
126-
return _RawConfParser.parse_config_file(file_path, self.verbose_mode)
157+
config_content, options = _RawConfParser.parse_config_file(
158+
file_path, self.verbose_mode
159+
)
160+
options = self._fix_store_true_options(options)
161+
return config_content, options
127162
except (configparser.Error, tomllib.TOMLDecodeError) as e:
128163
self.linter.add_message("config-parse-error", line=0, args=str(e))
129164
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)