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. "
@@ -1041,7 +1041,7 @@ class Environment:
10411041
10421042 def __init__ (
10431043 self ,
1044- search_path : Optional [Sequence [str ]] = None ,
1044+ search_path : Optional [Iterable [str ]] = None ,
10451045 platform : Optional [str ] = get_supported_platform (),
10461046 python : Optional [str ] = PY_MAJOR ,
10471047 ):
@@ -1084,7 +1084,7 @@ def remove(self, dist: "Distribution"):
10841084 """Remove `dist` from the environment"""
10851085 self ._distmap [dist .key ].remove (dist )
10861086
1087- def scan (self , search_path : Optional [Sequence [str ]] = None ):
1087+ def scan (self , search_path : Optional [Iterable [str ]] = None ):
10881088 """Scan `search_path` for distributions usable in this environment
10891089
10901090 Any distributions found are added to the environment.
@@ -1288,7 +1288,7 @@ def extraction_error(self) -> NoReturn:
12881288 err .original_error = old_exc
12891289 raise err
12901290
1291- def get_cache_path (self , archive_name : str , names : Iterable [str ] = ()):
1291+ def get_cache_path (self , archive_name : str , names : Iterable ["StrPath" ] = ()):
12921292 """Return absolute location in cache for `archive_name` and `names`
12931293
12941294 The parent directory of the resulting path will be created if it does
@@ -1340,7 +1340,7 @@ def _warn_unsafe_extraction_path(path):
13401340 ).format (** locals ())
13411341 warnings .warn (msg , UserWarning )
13421342
1343- def postprocess (self , tempname : str , filename : str ):
1343+ def postprocess (self , tempname : "StrOrBytesPath" , filename : "StrOrBytesPath" ):
13441344 """Perform any platform-specific postprocessing of `tempname`
13451345
13461346 This is where Mac header rewrites should be done; other platforms don't
@@ -2465,12 +2465,16 @@ def null_ns_handler(
24652465register_namespace_handler (object , null_ns_handler )
24662466
24672467
2468- def normalize_path (filename : "StrPath" ):
2468+ @overload
2469+ def normalize_path (filename : "StrPath" ) -> str : ...
2470+ @overload
2471+ def normalize_path (filename : "BytesPath" ) -> bytes : ...
2472+ def normalize_path (filename : "StrOrBytesPath" ):
24692473 """Normalize a file/dir name for comparison purposes"""
24702474 return os .path .normcase (os .path .realpath (os .path .normpath (_cygwin_patch (filename ))))
24712475
24722476
2473- def _cygwin_patch (filename : "StrPath " ): # pragma: nocover
2477+ def _cygwin_patch (filename : "StrOrBytesPath " ): # pragma: nocover
24742478 """
24752479 Contrary to POSIX 2008, on Cygwin, getcwd (3) contains
24762480 symlink components. Using
@@ -2481,9 +2485,19 @@ def _cygwin_patch(filename: "StrPath"): # pragma: nocover
24812485 return os .path .abspath (filename ) if sys .platform == 'cygwin' else filename
24822486
24832487
2484- @functools .lru_cache (maxsize = None )
2485- def _normalize_cached (filename ):
2486- return normalize_path (filename )
2488+ if TYPE_CHECKING :
2489+ # https://github.com/python/mypy/issues/16261
2490+ # https://github.com/python/typeshed/issues/6347
2491+ @overload
2492+ def _normalize_cached (filename : "StrPath" ) -> str : ...
2493+ @overload
2494+ def _normalize_cached (filename : "BytesPath" ) -> bytes : ...
2495+ def _normalize_cached (filename : "StrOrBytesPath" ) -> Union [str , bytes ]: ...
2496+ else :
2497+
2498+ @functools .lru_cache (maxsize = None )
2499+ def _normalize_cached (filename ):
2500+ return normalize_path (filename )
24872501
24882502
24892503def _is_egg_path (path ):
@@ -2680,7 +2694,7 @@ def parse_map(
26802694 _data = data .items ()
26812695 else :
26822696 _data = split_sections (data )
2683- maps : Dict [str , Dict [str , " EntryPoint" ]] = {}
2697+ maps : Dict [str , Dict [str , EntryPoint ]] = {}
26842698 for group , lines in _data :
26852699 if group is None :
26862700 if not lines :
@@ -2736,7 +2750,7 @@ def __init__(
27362750 def from_location (
27372751 cls ,
27382752 location : str ,
2739- basename : str ,
2753+ basename : "StrPath" ,
27402754 metadata : _MetadataType = None ,
27412755 ** kw : int , # We could set `precedence` explicitly, but keeping this as `**kw` for full backwards and subclassing compatibility
27422756 ):
@@ -2996,7 +3010,7 @@ def __dir__(self):
29963010 @classmethod
29973011 def from_filename (
29983012 cls ,
2999- filename : str ,
3013+ filename : "StrPath" ,
30003014 metadata : _MetadataType = None ,
30013015 ** kw : int , # We could set `precedence` explicitly, but keeping this as `**kw` for full backwards and subclassing compatibility
30023016 ):
@@ -3332,7 +3346,7 @@ def _find_adapter(registry: Mapping[type, _AdapterType], ob: object):
33323346 raise TypeError (f"Could not find adapter for { registry } and { ob } " )
33333347
33343348
3335- def ensure_directory (path : str ):
3349+ def ensure_directory (path : "StrOrBytesPath" ):
33363350 """Ensure that the parent directory of `path` exists"""
33373351 dirname = os .path .dirname (path )
33383352 os .makedirs (dirname , exist_ok = True )
0 commit comments