11import os
22import textwrap
33from optparse import Values
4- from typing import Any
4+ from typing import Callable
55
66from pip ._internal .cli .base_command import Command
77from pip ._internal .cli .status_codes import ERROR , SUCCESS
@@ -49,45 +49,48 @@ def add_options(self) -> None:
4949
5050 self .parser .insert_option_group (0 , self .cmd_opts )
5151
52- def run (self , options : Values , args : list [str ]) -> int :
53- handlers = {
52+ def handler_map (self ) -> dict [ str , Callable [[ Values , list [str ]], None ]] :
53+ return {
5454 "dir" : self .get_cache_dir ,
5555 "info" : self .get_cache_info ,
5656 "list" : self .list_cache_items ,
5757 "remove" : self .remove_cache_items ,
5858 "purge" : self .purge_cache ,
5959 }
6060
61+ def run (self , options : Values , args : list [str ]) -> int :
62+ handler_map = self .handler_map ()
63+
6164 if not options .cache_dir :
6265 logger .error ("pip cache commands can not function since cache is disabled." )
6366 return ERROR
6467
6568 # Determine action
66- if not args or args [0 ] not in handlers :
69+ if not args or args [0 ] not in handler_map :
6770 logger .error (
6871 "Need an action (%s) to perform." ,
69- ", " .join (sorted (handlers )),
72+ ", " .join (sorted (handler_map )),
7073 )
7174 return ERROR
7275
7376 action = args [0 ]
7477
7578 # Error handling happens here, not in the action-handlers.
7679 try :
77- handlers [action ](options , args [1 :])
80+ handler_map [action ](options , args [1 :])
7881 except PipError as e :
7982 logger .error (e .args [0 ])
8083 return ERROR
8184
8285 return SUCCESS
8386
84- def get_cache_dir (self , options : Values , args : list [Any ]) -> None :
87+ def get_cache_dir (self , options : Values , args : list [str ]) -> None :
8588 if args :
8689 raise CommandError ("Too many arguments" )
8790
8891 logger .info (options .cache_dir )
8992
90- def get_cache_info (self , options : Values , args : list [Any ]) -> None :
93+ def get_cache_info (self , options : Values , args : list [str ]) -> None :
9194 if args :
9295 raise CommandError ("Too many arguments" )
9396
@@ -129,7 +132,7 @@ def get_cache_info(self, options: Values, args: list[Any]) -> None:
129132
130133 logger .info (message )
131134
132- def list_cache_items (self , options : Values , args : list [Any ]) -> None :
135+ def list_cache_items (self , options : Values , args : list [str ]) -> None :
133136 if len (args ) > 1 :
134137 raise CommandError ("Too many arguments" )
135138
@@ -161,7 +164,7 @@ def format_for_abspath(self, files: list[str]) -> None:
161164 if files :
162165 logger .info ("\n " .join (sorted (files )))
163166
164- def remove_cache_items (self , options : Values , args : list [Any ]) -> None :
167+ def remove_cache_items (self , options : Values , args : list [str ]) -> None :
165168 if len (args ) > 1 :
166169 raise CommandError ("Too many arguments" )
167170
@@ -188,7 +191,7 @@ def remove_cache_items(self, options: Values, args: list[Any]) -> None:
188191 logger .verbose ("Removed %s" , filename )
189192 logger .info ("Files removed: %s (%s)" , len (files ), format_size (bytes_removed ))
190193
191- def purge_cache (self , options : Values , args : list [Any ]) -> None :
194+ def purge_cache (self , options : Values , args : list [str ]) -> None :
192195 if args :
193196 raise CommandError ("Too many arguments" )
194197
0 commit comments