20
20
import platformdirs
21
21
from packaging .version import Version
22
22
23
+ from typing import Optional , Union , Any , TypedDict , Generator
24
+
25
+
26
+ class ParsedURL (TypedDict ):
27
+ protocol : str
28
+ netloc : str
29
+ path : str
30
+
31
+
32
+ FilePath = Union [str , os .PathLike ]
33
+ FilePathInput = Union [FilePath , list [FilePath ], tuple [FilePath ]]
34
+
23
35
24
36
LOGGER = logging .Logger ("pooch" )
25
37
LOGGER .addHandler (logging .StreamHandler ())
26
38
27
39
28
- def file_hash (* args , ** kwargs ):
40
+ def file_hash (* args , ** kwargs ) -> Any :
29
41
"""
30
42
WARNING: Importing this function from pooch.utils is DEPRECATED.
31
43
Please import from the top-level namespace (`from pooch import file_hash`)
@@ -55,7 +67,7 @@ def file_hash(*args, **kwargs):
55
67
return new_file_hash (* args , ** kwargs )
56
68
57
69
58
- def get_logger ():
70
+ def get_logger () -> logging . Logger :
59
71
r"""
60
72
Get the default event logger.
61
73
@@ -71,7 +83,7 @@ def get_logger():
71
83
return LOGGER
72
84
73
85
74
- def os_cache (project ) :
86
+ def os_cache (project : str ) -> Path :
75
87
r"""
76
88
Default cache location based on the operating system.
77
89
@@ -100,7 +112,7 @@ def os_cache(project):
100
112
return Path (platformdirs .user_cache_dir (project ))
101
113
102
114
103
- def check_version (version , fallback = "master" ):
115
+ def check_version (version : str , fallback : str = "master" ) -> str :
104
116
"""
105
117
Check if a version is PEP440 compliant and there are no unreleased changes.
106
118
@@ -146,7 +158,7 @@ def check_version(version, fallback="master"):
146
158
return version
147
159
148
160
149
- def parse_url (url ) :
161
+ def parse_url (url : str ) -> ParsedURL :
150
162
"""
151
163
Parse a URL into 3 components:
152
164
@@ -199,11 +211,6 @@ def parse_url(url):
199
211
return {"protocol" : protocol , "netloc" : netloc , "path" : path }
200
212
201
213
202
- from typing import Optional , Union
203
-
204
- FilePath = Union [str , os .PathLike ]
205
- FilePathInput = Union [FilePath , list [FilePath ], tuple [FilePath ]]
206
-
207
214
def cache_location (
208
215
path : FilePathInput , env : Optional [str ] = None , version : Optional [str ] = None
209
216
) -> Path :
@@ -243,7 +250,7 @@ def cache_location(
243
250
return Path (path )
244
251
245
252
246
- def make_local_storage (path , env = None ):
253
+ def make_local_storage (path : FilePath , env : Optional [ str ] = None ) -> None :
247
254
"""
248
255
Create the local cache directory and make sure it's writable.
249
256
@@ -285,7 +292,7 @@ def make_local_storage(path, env=None):
285
292
286
293
287
294
@contextmanager
288
- def temporary_file (path = None ):
295
+ def temporary_file (path : Optional [ FilePath ] = None ) -> Generator [ str , None , None ] :
289
296
"""
290
297
Create a closed and named temporary file and make sure it's cleaned up.
291
298
@@ -305,7 +312,7 @@ def temporary_file(path=None):
305
312
The path to the temporary file.
306
313
307
314
"""
308
- tmp = tempfile .NamedTemporaryFile (delete = False , dir = path )
315
+ tmp = tempfile .NamedTemporaryFile (delete = False , dir = path ) # type: ignore
309
316
# Close the temp file so that it can be opened elsewhere
310
317
tmp .close ()
311
318
try :
@@ -315,7 +322,7 @@ def temporary_file(path=None):
315
322
os .remove (tmp .name )
316
323
317
324
318
- def unique_file_name (url ) :
325
+ def unique_file_name (url : str ) -> str :
319
326
"""
320
327
Create a unique file name based on the given URL.
321
328
0 commit comments