88import sys
99import sysconfig
1010from abc import ABCMeta , abstractmethod
11+ from collections .abc import Iterable , Iterator , Sequence
1112from contextlib import contextmanager
1213from fnmatch import fnmatch
1314from functools import lru_cache
1415from glob import glob
1516from pathlib import Path
16- from typing import Dict , Iterable , Iterator , List , Optional , Pattern , Sequence , Tuple , Type
17+ from re import Pattern
18+ from typing import Optional
1719
1820from isort import sections
1921from isort .settings import KNOWN_SECTION_MAPPING , Config
@@ -76,7 +78,7 @@ class KnownPatternFinder(BaseFinder):
7678 def __init__ (self , config : Config ) -> None :
7779 super ().__init__ (config )
7880
79- self .known_patterns : List [ Tuple [Pattern [str ], str ]] = []
81+ self .known_patterns : list [ tuple [Pattern [str ], str ]] = []
8082 for placement in reversed (config .sections ):
8183 known_placement = KNOWN_SECTION_MAPPING .get (placement , placement ).lower ()
8284 config_key = f"known_{ known_placement } "
@@ -92,7 +94,7 @@ def __init__(self, config: Config) -> None:
9294 regexp = "^" + known_pattern .replace ("*" , ".*" ).replace ("?" , ".?" ) + "$"
9395 self .known_patterns .append ((re .compile (regexp ), placement ))
9496
95- def _parse_known_pattern (self , pattern : str ) -> List [str ]:
97+ def _parse_known_pattern (self , pattern : str ) -> list [str ]:
9698 """Expand pattern if identified as a directory and return found sub packages"""
9799 if pattern .endswith (os .path .sep ):
98100 patterns = [
@@ -217,7 +219,7 @@ def _get_files_from_dir(self, path: str) -> Iterator[str]:
217219 raise NotImplementedError
218220
219221 @staticmethod
220- def _load_mapping () -> Optional [Dict [str , str ]]:
222+ def _load_mapping () -> Optional [dict [str , str ]]:
221223 """Return list of mappings `package_name -> module_name`
222224
223225 Example:
@@ -228,15 +230,15 @@ def _load_mapping() -> Optional[Dict[str, str]]:
228230 path = os .path .dirname (inspect .getfile (pipreqs ))
229231 path = os .path .join (path , "mapping" )
230232 with open (path ) as f :
231- mappings : Dict [str , str ] = {} # pypi_name: import_name
233+ mappings : dict [str , str ] = {} # pypi_name: import_name
232234 for line in f :
233235 import_name , _ , pypi_name = line .strip ().partition (":" )
234236 mappings [pypi_name ] = import_name
235237 return mappings
236238
237- def _load_names (self ) -> List [str ]:
239+ def _load_names (self ) -> list [str ]:
238240 """Return list of thirdparty modules from requirements"""
239- names : List [str ] = []
241+ names : list [str ] = []
240242 for path in self ._get_files ():
241243 names .extend (self ._normalize_name (name ) for name in self ._get_names (path ))
242244 return names
@@ -296,8 +298,8 @@ def _get_files_from_dir(self, path: str) -> Iterator[str]:
296298
297299 @classmethod
298300 @lru_cache (maxsize = 16 )
299- def _get_files_from_dir_cached (cls , path : str ) -> List [str ]:
300- results : List [str ] = []
301+ def _get_files_from_dir_cached (cls , path : str ) -> list [str ]:
302+ results : list [str ] = []
301303
302304 for fname in os .listdir (path ):
303305 if "requirements" not in fname :
@@ -329,8 +331,8 @@ def _get_names(self, path: str) -> Iterator[str]:
329331
330332 @classmethod
331333 @lru_cache (maxsize = 16 )
332- def _get_names_cached (cls , path : str ) -> List [str ]:
333- result : List [str ] = []
334+ def _get_names_cached (cls , path : str ) -> list [str ]:
335+ result : list [str ] = []
334336
335337 with chdir (os .path .dirname (path )):
336338 requirements = parse_requirements (Path (path ))
@@ -345,7 +347,7 @@ def find(self, module_name: str) -> Optional[str]:
345347
346348
347349class FindersManager :
348- _default_finders_classes : Sequence [Type [BaseFinder ]] = (
350+ _default_finders_classes : Sequence [type [BaseFinder ]] = (
349351 ForcedSeparateFinder ,
350352 LocalFinder ,
351353 KnownPatternFinder ,
@@ -355,26 +357,24 @@ class FindersManager:
355357 )
356358
357359 def __init__ (
358- self , config : Config , finder_classes : Optional [Iterable [Type [BaseFinder ]]] = None
360+ self , config : Config , finder_classes : Optional [Iterable [type [BaseFinder ]]] = None
359361 ) -> None :
360362 self .verbose : bool = config .verbose
361363
362364 if finder_classes is None :
363365 finder_classes = self ._default_finders_classes
364- finders : List [BaseFinder ] = []
366+ finders : list [BaseFinder ] = []
365367 for finder_cls in finder_classes :
366368 try :
367369 finders .append (finder_cls (config ))
368370 except Exception as exception :
369371 # if one finder fails to instantiate isort can continue using the rest
370372 if self .verbose :
371373 print (
372- (
373- f"{ finder_cls .__name__ } encountered an error ({ exception } ) during "
374- "instantiation and cannot be used"
375- )
374+ f"{ finder_cls .__name__ } encountered an error ({ exception } ) during "
375+ "instantiation and cannot be used"
376376 )
377- self .finders : Tuple [BaseFinder , ...] = tuple (finders )
377+ self .finders : tuple [BaseFinder , ...] = tuple (finders )
378378
379379 def find (self , module_name : str ) -> Optional [str ]:
380380 for finder in self .finders :
0 commit comments