66
77from __future__ import annotations
88
9+ import argparse
910import configparser
1011import os
1112import 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 {}, []
0 commit comments