2929
3030import confuse
3131
32- from beets import IncludeLazyConfig , config , library , logging , plugins , util
32+ from beets import library , logging , plugins , util
33+ from beets .config import config
3334from beets .dbcore import db
3435from beets .dbcore import query as db_query
35- from beets .ui import commands , core
3636from beets .ui ._common import UserError
3737from beets .ui .colors import color_split , colorize , uncolorize
3838from beets .ui .core import (
5252 should_write ,
5353 show_model_changes ,
5454 split_into_lines ,
55- uncolorize ,
5655)
5756
5857if TYPE_CHECKING :
59- from beets .library .library import Library
58+ from collections .abc import Callable
59+
60+ from beets .config import IncludeLazyConfig
6061
6162
6263# On Windows platforms, use colorama to support "ANSI" terminal colors.
7778 "UserError" ,
7879 "color_split" ,
7980 "colorize" ,
80- "commands" ,
81- "core" ,
8281 "input_" ,
8382 "input_options" ,
8483 "input_select_objects" ,
@@ -130,7 +129,7 @@ def _configure(options: optparse.Values) -> IncludeLazyConfig:
130129 # special handling lets specified plugins get loaded before we
131130 # finish parsing the command line.
132131 if getattr (options , "config" , None ) is not None :
133- overlay_path = options .config
132+ overlay_path : str | None = options .config
134133 del options .config
135134 config .set_file (overlay_path )
136135 else :
@@ -148,7 +147,7 @@ def _configure(options: optparse.Values) -> IncludeLazyConfig:
148147 "overlaying configuration: {}" , util .displayable_path (overlay_path )
149148 )
150149
151- config_path = config .user_config_path ()
150+ config_path : str = config .user_config_path ()
152151 if os .path .isfile (config_path ):
153152 log .debug ("user configuration: {}" , util .displayable_path (config_path ))
154153 else :
@@ -161,10 +160,12 @@ def _configure(options: optparse.Values) -> IncludeLazyConfig:
161160 return config
162161
163162
164- def _ensure_db_directory_exists (path ):
163+ def _ensure_db_directory_exists (
164+ path : util .PathLike ,
165+ ):
165166 if path == b":memory:" : # in memory db
166167 return
167- newpath = os .path .dirname (path )
168+ newpath : bytes | str = os .path .dirname (path )
168169 if not os .path .isdir (newpath ):
169170 if input_yn (
170171 f"The database directory { util .displayable_path (newpath )} does not"
@@ -175,16 +176,17 @@ def _ensure_db_directory_exists(path):
175176
176177def _open_library (config : confuse .LazyConfig ) -> library .Library :
177178 """Create a new library instance from the configuration."""
178- dbpath = util .bytestring_path (config ["library" ].as_filename ())
179+ dbpath : bytes = util .bytestring_path (config ["library" ].as_filename ())
179180 _ensure_db_directory_exists (dbpath )
181+ lib : library .Library
180182 try :
181183 lib = library .Library (
182184 dbpath ,
183185 config ["directory" ].as_filename (),
184186 get_path_formats (),
185187 get_replacements (),
186188 )
187- lib .get_item (0 ) # Test database connection.
189+ _ = lib .get_item (0 ) # Test database connection.
188190 except (sqlite3 .OperationalError , sqlite3 .DatabaseError ) as db_error :
189191 log .debug ("{}" , traceback .format_exc ())
190192 raise UserError (
@@ -199,11 +201,11 @@ def _open_library(config: confuse.LazyConfig) -> library.Library:
199201 return lib
200202
201203
202- def _raw_main (args : list [str ], lib : Library | None = None ) -> None :
204+ def _raw_main (args : list [str ], lib : library . Library | None = None ) -> None :
203205 """A helper function for `main` without top-level exception
204206 handling.
205207 """
206- parser = SubcommandsOptionParser ()
208+ parser : SubcommandsOptionParser = SubcommandsOptionParser ()
207209 parser .add_format_option (flags = ("--format-item" ,), target = library .Item )
208210 parser .add_format_option (flags = ("--format-album" ,), target = library .Album )
209211 _ = parser .add_option (
@@ -227,7 +229,10 @@ def _raw_main(args: list[str], lib: Library | None = None) -> None:
227229 )
228230
229231 def parse_csl_callback (
230- option : optparse .Option , _ , value : str , parser : SubcommandsOptionParser
232+ option : optparse .Option ,
233+ _ : Callable [..., object ],
234+ value : str ,
235+ parser : SubcommandsOptionParser ,
231236 ) -> None :
232237 """Parse a comma-separated list of values."""
233238 setattr (
@@ -236,36 +241,38 @@ def parse_csl_callback(
236241 list (filter (None , value .split ("," ))),
237242 )
238243
239- parser .add_option (
244+ _ = parser .add_option (
240245 "-p" ,
241246 "--plugins" ,
242247 dest = "plugins" ,
243248 action = "callback" ,
244249 callback = parse_csl_callback ,
245250 help = "a comma-separated list of plugins to load" ,
246251 )
247- parser .add_option (
252+ _ = parser .add_option (
248253 "-P" ,
249254 "--disable-plugins" ,
250255 dest = "disabled_plugins" ,
251256 action = "callback" ,
252257 callback = parse_csl_callback ,
253258 help = "a comma-separated list of plugins to disable" ,
254259 )
255- parser .add_option (
260+ _ = parser .add_option (
256261 "-h" ,
257262 "--help" ,
258263 dest = "help" ,
259264 action = "store_true" ,
260265 help = "show this help message and exit" ,
261266 )
262- parser .add_option (
267+ _ = parser .add_option (
263268 "--version" ,
264269 dest = "version" ,
265270 action = "store_true" ,
266271 help = optparse .SUPPRESS_HELP ,
267272 )
268273
274+ options : optparse .Values
275+ subargs : list [str ]
269276 options , subargs = parser .parse_global_options (args )
270277
271278 # Special case for the `config --edit` command: bypass _setup so
@@ -280,14 +287,17 @@ def parse_csl_callback(
280287
281288 return config_edit ()
282289
283- test_lib = bool (lib )
290+ test_lib : bool = bool (lib )
291+ subcommands : list [Subcommand ]
284292 subcommands , lib = _setup (options , lib )
285293 parser .add_subcommand (* subcommands )
286294
295+ subcommand : Subcommand
296+ suboptions : optparse .Values
287297 subcommand , suboptions , subargs = parser .parse_subcommand (subargs )
288298 subcommand .func (lib , suboptions , subargs )
289299
290- plugins .send ("cli_exit" , lib = lib )
300+ _ = plugins .send ("cli_exit" , lib = lib )
291301 if not test_lib :
292302 # Clean up the library unless it came from the test harness.
293303 lib ._close ()
@@ -308,7 +318,7 @@ def main(args: list[str] | None = None) -> None:
308318 try :
309319 _raw_main (args or [])
310320 except UserError as exc :
311- message = exc .args [0 ] if exc .args else None
321+ message : object = exc .args [0 ] if exc .args else None
312322 log .error ("error: {}" , message )
313323 sys .exit (1 )
314324 except util .HumanReadableError as exc :
@@ -329,7 +339,7 @@ def main(args: list[str] | None = None) -> None:
329339 except OSError as exc :
330340 if exc .errno == errno .EPIPE :
331341 # "Broken pipe". End silently.
332- sys .stderr .close ()
342+ _ = sys .stderr .close ()
333343 else :
334344 raise
335345 except KeyboardInterrupt :
0 commit comments