44
55# ruff: noqa: C408
66# Unnecessary `dict` call (rewrite as a literal)
7+ from __future__ import annotations
78
89import argparse
9- import io
1010import json
1111import logging
1212import sys
1313from contextlib import contextmanager
1414from itertools import chain
1515from textwrap import dedent , wrap
1616from typing import (
17+ TYPE_CHECKING ,
1718 Callable ,
18- Dict ,
1919 Generator ,
2020 Iterator ,
21- List ,
2221 NamedTuple ,
2322 Sequence ,
24- Tuple ,
25- Type ,
2623 TypeVar ,
2724)
2825
3431from .plugins import list_from_entry_points as list_plugins_from_entry_points
3532from .remote import RemotePlugin , load_store
3633
34+ if TYPE_CHECKING :
35+ import io
36+
3737_logger = logging .getLogger (__package__ )
3838T = TypeVar ("T" , bound = NamedTuple )
3939
@@ -53,7 +53,7 @@ def critical_logging() -> Generator[None, None, None]:
5353
5454_STDIN = argparse .FileType ("r" )("-" )
5555
56- META : Dict [str , dict ] = {
56+ META : dict [str , dict ] = {
5757 "version" : dict (
5858 flags = ("-V" , "--version" ),
5959 action = "version" ,
@@ -116,15 +116,15 @@ def critical_logging() -> Generator[None, None, None]:
116116
117117
118118class CliParams (NamedTuple ):
119- input_file : List [io .TextIOBase ]
120- plugins : List [PluginWrapper ]
121- tool : List [str ]
119+ input_file : list [io .TextIOBase ]
120+ plugins : list [PluginWrapper ]
121+ tool : list [str ]
122122 store : str
123123 loglevel : int = logging .WARNING
124124 dump_json : bool = False
125125
126126
127- def __meta__ (plugins : Sequence [PluginProtocol ]) -> Dict [str , dict ]:
127+ def __meta__ (plugins : Sequence [PluginProtocol ]) -> dict [str , dict ]:
128128 """'Hyper parameters' to instruct :mod:`argparse` how to create the CLI"""
129129 meta = {k : v .copy () for k , v in META .items ()}
130130 meta ["enable" ]["choices" ] = {p .tool for p in plugins }
@@ -137,8 +137,8 @@ def parse_args(
137137 args : Sequence [str ],
138138 plugins : Sequence [PluginProtocol ],
139139 description : str = "Validate a given TOML file" ,
140- get_parser_spec : Callable [[Sequence [PluginProtocol ]], Dict [str , dict ]] = __meta__ ,
141- params_class : Type [T ] = CliParams , # type: ignore[assignment]
140+ get_parser_spec : Callable [[Sequence [PluginProtocol ]], dict [str , dict ]] = __meta__ ,
141+ params_class : type [T ] = CliParams , # type: ignore[assignment]
142142) -> T :
143143 """Parse command line parameters
144144
@@ -174,7 +174,7 @@ def select_plugins(
174174 plugins : Sequence [Plugins ],
175175 enabled : Sequence [str ] = (),
176176 disabled : Sequence [str ] = (),
177- ) -> List [Plugins ]:
177+ ) -> list [Plugins ]:
178178 available = list (plugins )
179179 if enabled :
180180 available = [p for p in available if p .tool in enabled ]
@@ -200,13 +200,13 @@ def exceptions2exit() -> Generator[None, None, None]:
200200 except _ExceptionGroup as group :
201201 for prefix , ex in group :
202202 print (prefix )
203- _logger .error (str (ex ) + "\n " )
203+ _logger .exception (str (ex ) + "\n " )
204204 raise SystemExit (1 ) from None
205205 except _REGULAR_EXCEPTIONS as ex :
206- _logger .error (str (ex ))
206+ _logger .exception (str (ex ))
207207 raise SystemExit (1 ) from None
208208 except Exception as ex : # pragma: no cover
209- _logger .error (f"{ ex .__class__ .__name__ } : { ex } \n " )
209+ _logger .exception (f"{ ex .__class__ .__name__ } : { ex } \n " )
210210 _logger .debug ("Please check the following information:" , exc_info = True )
211211 raise SystemExit (1 ) from None
212212
@@ -234,7 +234,7 @@ def run(args: Sequence[str] = ()) -> int:
234234 for file in params .input_file :
235235 try :
236236 _run_on_file (validator , params , file )
237- except _REGULAR_EXCEPTIONS as ex :
237+ except _REGULAR_EXCEPTIONS as ex : # noqa: PERF203
238238 exceptions .add (f"Invalid { _format_file (file )} " , ex )
239239
240240 exceptions .raise_if_any ()
@@ -262,7 +262,7 @@ class Formatter(argparse.RawTextHelpFormatter):
262262 # order to create our own formatter, we are left no choice other then overwrite a
263263 # "private" method considered to be an implementation detail.
264264
265- def _split_lines (self , text : str , width : int ) -> List [str ]:
265+ def _split_lines (self , text : str , width : int ) -> list [str ]:
266266 return list (chain .from_iterable (wrap (x , width ) for x in text .splitlines ()))
267267
268268
@@ -289,7 +289,7 @@ def _format_file(file: io.TextIOBase) -> str:
289289
290290
291291class _ExceptionGroup (Exception ):
292- _members : List [ Tuple [str , Exception ]]
292+ _members : list [ tuple [str , Exception ]]
293293
294294 def __init__ (self ) -> None :
295295 self ._members = []
@@ -298,7 +298,7 @@ def __init__(self) -> None:
298298 def add (self , prefix : str , ex : Exception ) -> None :
299299 self ._members .append ((prefix , ex ))
300300
301- def __iter__ (self ) -> Iterator [Tuple [str , Exception ]]:
301+ def __iter__ (self ) -> Iterator [tuple [str , Exception ]]:
302302 return iter (self ._members )
303303
304304 def raise_if_any (self ) -> None :
0 commit comments