3636 MutableSequence ,
3737 NamedTuple ,
3838 NoReturn ,
39- Sequence ,
4039 Set ,
4140 Tuple ,
4241 Type ,
4948 Iterable ,
5049 Optional ,
5150 TypeVar ,
51+ overload ,
5252)
5353import zipfile
5454import zipimport
9999from pkg_resources .extern .platformdirs import user_cache_dir as _user_cache_dir
100100
101101if TYPE_CHECKING :
102- from _typeshed import StrPath
102+ from _typeshed import StrPath , StrOrBytesPath , BytesPath
103103
104104warnings .warn (
105105 "pkg_resources is deprecated as an API. "
@@ -1046,7 +1046,7 @@ class Environment:
10461046
10471047 def __init__ (
10481048 self ,
1049- search_path : Optional [Sequence [str ]] = None ,
1049+ search_path : Optional [Iterable [str ]] = None ,
10501050 platform : Optional [str ] = get_supported_platform (),
10511051 python : Optional [str ] = PY_MAJOR ,
10521052 ):
@@ -1089,7 +1089,7 @@ def remove(self, dist: "Distribution"):
10891089 """Remove `dist` from the environment"""
10901090 self ._distmap [dist .key ].remove (dist )
10911091
1092- def scan (self , search_path : Optional [Sequence [str ]] = None ):
1092+ def scan (self , search_path : Optional [Iterable [str ]] = None ):
10931093 """Scan `search_path` for distributions usable in this environment
10941094
10951095 Any distributions found are added to the environment.
@@ -1293,7 +1293,7 @@ def extraction_error(self) -> NoReturn:
12931293 err .original_error = old_exc
12941294 raise err
12951295
1296- def get_cache_path (self , archive_name : str , names : Iterable [str ] = ()):
1296+ def get_cache_path (self , archive_name : str , names : Iterable ["StrPath" ] = ()):
12971297 """Return absolute location in cache for `archive_name` and `names`
12981298
12991299 The parent directory of the resulting path will be created if it does
@@ -1345,7 +1345,7 @@ def _warn_unsafe_extraction_path(path):
13451345 ).format (** locals ())
13461346 warnings .warn (msg , UserWarning )
13471347
1348- def postprocess (self , tempname : str , filename : str ):
1348+ def postprocess (self , tempname : "StrOrBytesPath" , filename : "StrOrBytesPath" ):
13491349 """Perform any platform-specific postprocessing of `tempname`
13501350
13511351 This is where Mac header rewrites should be done; other platforms don't
@@ -2472,12 +2472,16 @@ def null_ns_handler(
24722472register_namespace_handler (object , null_ns_handler )
24732473
24742474
2475- def normalize_path (filename : "StrPath" ):
2475+ @overload
2476+ def normalize_path (filename : "StrPath" ) -> str : ...
2477+ @overload
2478+ def normalize_path (filename : "BytesPath" ) -> bytes : ...
2479+ def normalize_path (filename : "StrOrBytesPath" ):
24762480 """Normalize a file/dir name for comparison purposes"""
24772481 return os .path .normcase (os .path .realpath (os .path .normpath (_cygwin_patch (filename ))))
24782482
24792483
2480- def _cygwin_patch (filename : "StrPath " ): # pragma: nocover
2484+ def _cygwin_patch (filename : "StrOrBytesPath " ): # pragma: nocover
24812485 """
24822486 Contrary to POSIX 2008, on Cygwin, getcwd (3) contains
24832487 symlink components. Using
@@ -2488,9 +2492,19 @@ def _cygwin_patch(filename: "StrPath"): # pragma: nocover
24882492 return os .path .abspath (filename ) if sys .platform == 'cygwin' else filename
24892493
24902494
2491- @functools .lru_cache (maxsize = None )
2492- def _normalize_cached (filename ):
2493- return normalize_path (filename )
2495+ if TYPE_CHECKING :
2496+ # https://github.com/python/mypy/issues/16261
2497+ # https://github.com/python/typeshed/issues/6347
2498+ @overload
2499+ def _normalize_cached (filename : "StrPath" ) -> str : ...
2500+ @overload
2501+ def _normalize_cached (filename : "BytesPath" ) -> bytes : ...
2502+ def _normalize_cached (filename : "StrOrBytesPath" ) -> Union [str , bytes ]: ...
2503+ else :
2504+
2505+ @functools .lru_cache (maxsize = None )
2506+ def _normalize_cached (filename ):
2507+ return normalize_path (filename )
24942508
24952509
24962510def _is_egg_path (path ):
@@ -2743,7 +2757,7 @@ def __init__(
27432757 def from_location (
27442758 cls ,
27452759 location : str ,
2746- basename : str ,
2760+ basename : "StrPath" ,
27472761 metadata : _MetadataType = None ,
27482762 ** kw : int , # We could set `precedence` explicitly, but keeping this as `**kw` for full backwards and subclassing compatibility
27492763 ):
@@ -3003,7 +3017,7 @@ def __dir__(self):
30033017 @classmethod
30043018 def from_filename (
30053019 cls ,
3006- filename : str ,
3020+ filename : "StrPath" ,
30073021 metadata : _MetadataType = None ,
30083022 ** kw : int , # We could set `precedence` explicitly, but keeping this as `**kw` for full backwards and subclassing compatibility
30093023 ):
@@ -3339,7 +3353,7 @@ def _find_adapter(registry: Mapping[type, _AdapterT], ob: object) -> _AdapterT:
33393353 raise TypeError (f"Could not find adapter for { registry } and { ob } " )
33403354
33413355
3342- def ensure_directory (path : str ):
3356+ def ensure_directory (path : "StrOrBytesPath" ):
33433357 """Ensure that the parent directory of `path` exists"""
33443358 dirname = os .path .dirname (path )
33453359 os .makedirs (dirname , exist_ok = True )
0 commit comments