Skip to content

Commit 1efe782

Browse files
committed
Tighten up typing on resolve_path
Hard to be non-concrete types with the approach we are using here
1 parent 474dd0a commit 1efe782

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/dxtbx/serialize/filename.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
import glob
44
import os
5-
from typing import AnyStr, TypeVar, overload
5+
from pathlib import Path
6+
from typing import TypeVar
67

78
from dxtbx.sequence_filenames import template_string_to_glob_expr
89

9-
T = TypeVar("T", AnyStr, os.PathLike)
10+
T = TypeVar("T", str, Path)
1011

1112

12-
@overload
13-
def resolve_path(path: T, directory: AnyStr | os.PathLike | None) -> T:
14-
pass
15-
16-
17-
def resolve_path(path: T, directory: AnyStr | os.PathLike | None = None) -> T:
13+
def resolve_path(path: T, directory: str | os.PathLike | None = None) -> T:
1814
"""Resolve a file path.
1915
2016
First expand any environment and user variables. Then create the absolute
@@ -25,17 +21,16 @@ def resolve_path(path: T, directory: AnyStr | os.PathLike | None = None) -> T:
2521
links
2622
2723
Returns:
28-
str: The absolute path to the file to read, if accessible, otherwise
29-
return the original path..
24+
The absolute path to the file to read, if accessible, otherwise
25+
return the original path.
3026
"""
3127

3228
assert path, "Believe this is overly defensive, check if we ever rely on"
3329
if not path:
34-
return ""
35-
path = str(path)
36-
trial_path = os.path.expanduser(os.path.expandvars(path))
30+
return path
31+
trial_path = os.path.expanduser(os.path.expandvars(str(path)))
3732
if directory and not os.path.isabs(trial_path):
38-
trial_path = os.path.join(str(directory), trial_path)
33+
trial_path = os.path.join(os.fspath(directory), trial_path)
3934
trial_path = os.path.abspath(trial_path)
4035
if glob.glob(template_string_to_glob_expr(trial_path)):
4136
return type(path)(trial_path)

0 commit comments

Comments
 (0)