1212from textwrap import indent
1313import inifix
1414
15- # TODO: replace this with except* when support for Python 3.10 is dropped
16- from exceptiongroup import catch , BaseExceptionGroup
17-
1815if sys .version_info >= (3 , 12 ):
1916 from typing import override
2017else :
2118 from typing_extensions import override
2219
2320if TYPE_CHECKING :
24- from inifix ._typing import AnyConfig # pyright: ignore[reportPrivateImportUsage]
21+ from inifix ._typing import AnyConfig
2522
2623
2724@click .group ("inifix" )
@@ -45,7 +42,7 @@ def get_cpu_count() -> int:
4542 base_cpu_count = os .process_cpu_count ()
4643 elif hasattr (os , "sched_getaffinity" ):
4744 # this function isn't available on all platforms
48- base_cpu_count = len (os .sched_getaffinity (0 )) # pyright: ignore[reportAttributeAccessIssue]
45+ base_cpu_count = len (os .sched_getaffinity (0 ))
4946 else : # pragma: no cover
5047 # this proxy is good enough in most situations
5148 base_cpu_count = os .cpu_count ()
@@ -107,19 +104,17 @@ def _validate_single_file(file: str, sections: SectionsArg) -> TaskResults:
107104 messages .append (Message (f"Error: could not find { file } " ))
108105 return TaskResults (status , messages )
109106
110- # TODO: rewrite this section with try/except*/else when support for Python 3.10 is dropped
111- def value_error_handler (exc : BaseExceptionGroup [Exception ]) -> None :
112- nonlocal status
107+ # mypy struggles to infer sections.name
108+ sections_name = cast ("Literal['allow', 'forbid', 'require']" , sections .name ) # pyright: ignore[reportUnnecessaryCast] # ty: ignore[redundant-cast]
109+ try :
110+ _ = inifix .load (file , sections = sections_name )
111+ except* ValueError as excgroup :
113112 status = 1
114- exc_repr = "\n " .join (str (e ) for e in exc .exceptions )
113+ exc_repr = "\n " .join (str (e ) for e in excgroup .exceptions )
115114 messages .append (
116115 Message (f"Failed to validate { file } :\n { indent (exc_repr , ' ' )} " )
117116 )
118-
119- with catch ({ValueError : value_error_handler }):
120- # mypy struggles to infer sections.name
121- sections_name = cast ("Literal['allow', 'forbid', 'require']" , sections .name ) # pyright: ignore[reportUnnecessaryCast] # ty: ignore[redundant-cast]
122- _ = inifix .load (file , sections = sections_name )
117+ else :
123118 messages .append (Message (f"Validated { file } " ))
124119
125120 return TaskResults (status , messages )
@@ -195,19 +190,16 @@ def _format_single_file(
195190
196191 validate_baseline : AnyConfig = {}
197192 if not skip_validation :
198- # TODO: rewrite this section with try/except* when support for Python 3.10 is dropped
199- def value_error_handler (exc : BaseExceptionGroup [Exception ]) -> None :
200- nonlocal status
193+ # mypy struggles to infer sections.name
194+ sections_name = cast ("Literal['allow', 'forbid', 'require']" , sections .name ) # pyright: ignore[reportUnnecessaryCast] # ty: ignore[redundant-cast]
195+ try :
196+ validate_baseline = inifix .load (file , sections = sections_name )
197+ except* ValueError as excgroup :
201198 status = 1
202- exc_repr = "\n " .join (str (e ) for e in exc .exceptions )
199+ exc_repr = "\n " .join (str (e ) for e in excgroup .exceptions )
203200 messages .append (
204201 Message (f"Failed to format { file } :\n { indent (exc_repr , ' ' )} " )
205202 )
206-
207- with catch ({ValueError : value_error_handler }):
208- # mypy struggles to infer sections.name
209- sections_name = cast ("Literal['allow', 'forbid', 'require']" , sections .name ) # pyright: ignore[reportUnnecessaryCast] # ty: ignore[redundant-cast]
210- validate_baseline = inifix .load (file , sections = sections_name )
211203 if status != 0 :
212204 return TaskResults (status , messages )
213205
0 commit comments