4
4
5
5
"""Arguments manager class used to handle command-line arguments and options."""
6
6
7
+ from __future__ import annotations
8
+
7
9
import argparse
8
10
import collections
9
11
import configparser
15
17
import textwrap
16
18
import warnings
17
19
from pathlib import Path
18
- from typing import TYPE_CHECKING , Any , Dict , List , Optional , TextIO , Tuple , Union
20
+ from typing import TYPE_CHECKING , Any , TextIO , Union
19
21
20
22
import tomlkit
21
23
58
60
class _ArgumentsManager :
59
61
"""Arguments manager class used to handle command-line arguments and options."""
60
62
61
- def __init__ (self , prog : str , usage : Optional [ str ] = None ) -> None :
63
+ def __init__ (self , prog : str , usage : str | None = None ) -> None :
62
64
self .namespace = argparse .Namespace ()
63
65
"""Namespace for all options."""
64
66
@@ -69,10 +71,10 @@ def __init__(self, prog: str, usage: Optional[str] = None) -> None:
69
71
)
70
72
"""The command line argument parser."""
71
73
72
- self ._argument_groups_dict : Dict [str , argparse ._ArgumentGroup ] = {}
74
+ self ._argument_groups_dict : dict [str , argparse ._ArgumentGroup ] = {}
73
75
"""Dictionary of all the argument groups."""
74
76
75
- self ._option_dicts : Dict [str , OptionDict ] = {}
77
+ self ._option_dicts : dict [str , OptionDict ] = {}
76
78
"""All option dictionaries that have been registered."""
77
79
78
80
# pylint: disable=fixme
@@ -82,20 +84,20 @@ def __init__(self, prog: str, usage: Optional[str] = None) -> None:
82
84
warnings .filterwarnings ("ignore" , category = DeprecationWarning )
83
85
self .reset_parsers (usage or "" )
84
86
# list of registered options providers
85
- self .options_providers : List [ConfigProvider ] = []
87
+ self .options_providers : list [ConfigProvider ] = []
86
88
# dictionary associating option name to checker
87
89
self ._all_options : OrderedDict [str , ConfigProvider ] = collections .OrderedDict ()
88
- self ._short_options : Dict [str , str ] = {}
89
- self ._nocallback_options : Dict [ConfigProvider , str ] = {}
90
- self ._mygroups : Dict [str , optparse .OptionGroup ] = {}
90
+ self ._short_options : dict [str , str ] = {}
91
+ self ._nocallback_options : dict [ConfigProvider , str ] = {}
92
+ self ._mygroups : dict [str , optparse .OptionGroup ] = {}
91
93
# verbosity
92
94
self ._maxlevel : int = 0
93
95
94
96
@property
95
97
def config (self ) -> argparse .Namespace :
96
98
return self .namespace
97
99
98
- def _register_options_provider (self , provider : " _ArgumentsProvider" ) -> None :
100
+ def _register_options_provider (self , provider : _ArgumentsProvider ) -> None :
99
101
"""Register an options provider and load its defaults."""
100
102
for opt , optdict in provider .options :
101
103
self ._option_dicts [opt ] = optdict
@@ -113,7 +115,7 @@ def _register_options_provider(self, provider: "_ArgumentsProvider") -> None:
113
115
self ._load_default_argument_values ()
114
116
115
117
def _add_arguments_to_parser (
116
- self , section : str , section_desc : Optional [ str ] , argument : _Argument
118
+ self , section : str , section_desc : str | None , argument : _Argument
117
119
) -> None :
118
120
"""Add an argument to the correct argument section/group."""
119
121
try :
@@ -210,15 +212,15 @@ def _load_default_argument_values(self) -> None:
210
212
"""Loads the default values of all registered options."""
211
213
self .namespace = self ._arg_parser .parse_args ([], self .namespace )
212
214
213
- def _parse_configuration_file (self , arguments : List [str ]) -> None :
215
+ def _parse_configuration_file (self , arguments : list [str ]) -> None :
214
216
"""Parse the arguments found in a configuration file into the namespace."""
215
217
# pylint: disable-next=fixme
216
218
# TODO: This should parse_args instead of parse_known_args
217
219
self .namespace = self ._arg_parser .parse_known_args (arguments , self .namespace )[0 ]
218
220
219
221
def _parse_command_line_configuration (
220
- self , arguments : Optional [ List [ str ]] = None
221
- ) -> List [str ]:
222
+ self , arguments : list [ str ] | None = None
223
+ ) -> list [str ]:
222
224
"""Parse the arguments found on the command line into the namespace."""
223
225
arguments = sys .argv [1 :] if arguments is None else arguments
224
226
@@ -287,8 +289,8 @@ def register_options_provider(
287
289
def add_option_group (
288
290
self ,
289
291
group_name : str ,
290
- _ : Optional [ str ] ,
291
- options : List [ Tuple [str , OptionDict ]],
292
+ _ : str | None ,
293
+ options : list [ tuple [str , OptionDict ]],
292
294
provider : ConfigProvider ,
293
295
) -> None :
294
296
"""DEPRECATED."""
@@ -325,7 +327,7 @@ def add_option_group(
325
327
def add_optik_option (
326
328
self ,
327
329
provider : ConfigProvider ,
328
- optikcontainer : Union [ optparse .OptionParser , optparse .OptionGroup ] ,
330
+ optikcontainer : optparse .OptionParser | optparse .OptionGroup ,
329
331
opt : str ,
330
332
optdict : OptionDict ,
331
333
) -> None :
@@ -344,7 +346,7 @@ def add_optik_option(
344
346
345
347
def optik_option (
346
348
self , provider : ConfigProvider , opt : str , optdict : OptionDict
347
- ) -> Tuple [ List [str ], OptionDict ]:
349
+ ) -> tuple [ list [str ], OptionDict ]:
348
350
"""DEPRECATED: Get our personal option definition and return a suitable form for
349
351
use with optik/optparse
350
352
"""
@@ -381,7 +383,7 @@ def optik_option(
381
383
return args , optdict
382
384
383
385
def generate_config (
384
- self , stream : Optional [ TextIO ] = None , skipsections : Tuple [str , ...] = ()
386
+ self , stream : TextIO | None = None , skipsections : tuple [str , ...] = ()
385
387
) -> None :
386
388
"""DEPRECATED: Write a configuration file according to the current configuration
387
389
into the given stream or stdout
@@ -450,7 +452,7 @@ def load_provider_defaults(self) -> None:
450
452
provider .load_defaults ()
451
453
452
454
def read_config_file (
453
- self , config_file : Optional [ Path ] = None , verbose : bool = False
455
+ self , config_file : Path | None = None , verbose : bool = False
454
456
) -> None :
455
457
"""DEPRECATED: Read the configuration file but do not load it (i.e. dispatching
456
458
values to each option's provider)
@@ -548,7 +550,7 @@ def load_configuration(self, **kwargs: Any) -> None:
548
550
warnings .filterwarnings ("ignore" , category = DeprecationWarning )
549
551
return self .load_configuration_from_config (kwargs )
550
552
551
- def load_configuration_from_config (self , config : Dict [str , Any ]) -> None :
553
+ def load_configuration_from_config (self , config : dict [str , Any ]) -> None :
552
554
warnings .warn (
553
555
"DEPRECATED: load_configuration_from_config has been deprecated. It will be removed in pylint 3.0." ,
554
556
DeprecationWarning ,
@@ -559,8 +561,8 @@ def load_configuration_from_config(self, config: Dict[str, Any]) -> None:
559
561
provider .set_option (opt , opt_value )
560
562
561
563
def load_command_line_configuration (
562
- self , args : Optional [ List [ str ]] = None
563
- ) -> List [str ]:
564
+ self , args : list [ str ] | None = None
565
+ ) -> list [str ]:
564
566
"""DEPRECATED: Override configuration according to command line parameters.
565
567
566
568
return additional arguments
@@ -580,7 +582,7 @@ def load_command_line_configuration(
580
582
setattr (config , attr , value ) # pragma: no cover # Handled by argparse.
581
583
return args
582
584
583
- def help (self , level : Optional [ int ] = None ) -> str :
585
+ def help (self , level : int | None = None ) -> str :
584
586
"""Return the usage string based on the available options."""
585
587
if level is not None :
586
588
warnings .warn (
0 commit comments