44import json
55import multiprocessing as mp
66import re
7+ import tomllib
78from argparse import Namespace
89from pathlib import Path
9- from typing import Dict , List , Union
10-
11- import tomllib
1210
1311from cascade_config import CascadeConfig
1412
@@ -36,7 +34,7 @@ def _parse_output_path(configured_path, psm_file_path):
3634 return (Path (psm_file_path ).parent / psm_file_stem ).as_posix ()
3735
3836
39- def _validate_filenames (config : Dict ) -> Dict :
37+ def _validate_filenames (config : dict ) -> dict :
4038 """Validate and infer input/output filenames."""
4139 # psm_file should be provided
4240 if not config ["ms2rescore" ]["psm_file" ]:
@@ -74,7 +72,7 @@ def _validate_filenames(config: Dict) -> Dict:
7472 return config
7573
7674
77- def _validate_processes (config : Dict ) -> Dict :
75+ def _validate_processes (config : dict ) -> dict :
7876 """Validate requested processes with available cpu count."""
7977 n_available = mp .cpu_count ()
8078 if (config ["ms2rescore" ]["processes" ] == - 1 ) or (
@@ -84,7 +82,7 @@ def _validate_processes(config: Dict) -> Dict:
8482 return config
8583
8684
87- def _validate_regular_expressions (config : Dict ) -> Dict :
85+ def _validate_regular_expressions (config : dict ) -> dict :
8886 """Validate regular expressions in configuration."""
8987 for field in [
9088 "psm_id_pattern" ,
@@ -113,7 +111,7 @@ def _validate_regular_expressions(config: Dict) -> Dict:
113111 return config
114112
115113
116- def parse_configurations (configurations : List [ Union [ dict , str , Path , Namespace ]] ) -> Dict :
114+ def parse_configurations (configurations : list [ dict | str | Path | Namespace ]) -> dict :
117115 """
118116 Parse and validate MS²Rescore configuration files and CLI arguments.
119117
@@ -150,19 +148,20 @@ def parse_configurations(configurations: List[Union[dict, str, Path, Namespace]]
150148 continue
151149 if isinstance (config , dict ):
152150 cascade_conf .add_dict (config )
153- elif isinstance (config , str ) or isinstance ( config , Path ):
151+ elif isinstance (config , ( str , Path ) ):
154152 if Path (config ).suffix .lower () == ".json" :
155153 cascade_conf .add_json (config )
156154 elif Path (config ).suffix .lower () == ".toml" :
157- cascade_conf .add_dict (dict (tomllib .load (Path (config ).open ("rb" ))))
155+ with Path (config ).open ("rb" ) as f :
156+ cascade_conf .add_dict (dict (tomllib .load (f )))
158157 else :
159158 raise MS2RescoreConfigurationError (
160159 "Unknown file extension for configuration file. Should be `json` or `toml`."
161160 )
162161 elif isinstance (config , Namespace ):
163162 cascade_conf .add_namespace (config , subkey = "ms2rescore" )
164163 else :
165- raise ValueError (
164+ raise TypeError (
166165 "Configuration should be a dictionary, argparse Namespace, or path to a "
167166 "configuration file."
168167 )
0 commit comments