Skip to content

Commit 0f85eb0

Browse files
committed
Update utils.py
1 parent d12ec2a commit 0f85eb0

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

pooch/utils.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,24 @@
2020
import platformdirs
2121
from packaging.version import Version
2222

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+
2335

2436
LOGGER = logging.Logger("pooch")
2537
LOGGER.addHandler(logging.StreamHandler())
2638

2739

28-
def file_hash(*args, **kwargs):
40+
def file_hash(*args, **kwargs) -> Any:
2941
"""
3042
WARNING: Importing this function from pooch.utils is DEPRECATED.
3143
Please import from the top-level namespace (`from pooch import file_hash`)
@@ -55,7 +67,7 @@ def file_hash(*args, **kwargs):
5567
return new_file_hash(*args, **kwargs)
5668

5769

58-
def get_logger():
70+
def get_logger() -> logging.Logger:
5971
r"""
6072
Get the default event logger.
6173
@@ -71,7 +83,7 @@ def get_logger():
7183
return LOGGER
7284

7385

74-
def os_cache(project):
86+
def os_cache(project: str) -> Path:
7587
r"""
7688
Default cache location based on the operating system.
7789
@@ -100,7 +112,7 @@ def os_cache(project):
100112
return Path(platformdirs.user_cache_dir(project))
101113

102114

103-
def check_version(version, fallback="master"):
115+
def check_version(version: str, fallback: str = "master") -> str:
104116
"""
105117
Check if a version is PEP440 compliant and there are no unreleased changes.
106118
@@ -146,7 +158,7 @@ def check_version(version, fallback="master"):
146158
return version
147159

148160

149-
def parse_url(url):
161+
def parse_url(url: str) -> ParsedURL:
150162
"""
151163
Parse a URL into 3 components:
152164
@@ -199,11 +211,6 @@ def parse_url(url):
199211
return {"protocol": protocol, "netloc": netloc, "path": path}
200212

201213

202-
from typing import Optional, Union
203-
204-
FilePath = Union[str, os.PathLike]
205-
FilePathInput = Union[FilePath, list[FilePath], tuple[FilePath]]
206-
207214
def cache_location(
208215
path: FilePathInput, env: Optional[str] = None, version: Optional[str] = None
209216
) -> Path:
@@ -243,7 +250,7 @@ def cache_location(
243250
return Path(path)
244251

245252

246-
def make_local_storage(path, env=None):
253+
def make_local_storage(path: FilePath, env: Optional[str] = None) -> None:
247254
"""
248255
Create the local cache directory and make sure it's writable.
249256
@@ -285,7 +292,7 @@ def make_local_storage(path, env=None):
285292

286293

287294
@contextmanager
288-
def temporary_file(path=None):
295+
def temporary_file(path: Optional[FilePath] = None) -> Generator[str, None, None]:
289296
"""
290297
Create a closed and named temporary file and make sure it's cleaned up.
291298
@@ -305,7 +312,7 @@ def temporary_file(path=None):
305312
The path to the temporary file.
306313
307314
"""
308-
tmp = tempfile.NamedTemporaryFile(delete=False, dir=path)
315+
tmp = tempfile.NamedTemporaryFile(delete=False, dir=path) # type: ignore
309316
# Close the temp file so that it can be opened elsewhere
310317
tmp.close()
311318
try:
@@ -315,7 +322,7 @@ def temporary_file(path=None):
315322
os.remove(tmp.name)
316323

317324

318-
def unique_file_name(url):
325+
def unique_file_name(url: str) -> str:
319326
"""
320327
Create a unique file name based on the given URL.
321328

0 commit comments

Comments
 (0)