Skip to content

Commit 2708d58

Browse files
committed
Updated type annotations in paths.py
1 parent 5e69263 commit 2708d58

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

domdf_python_tools/paths.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
import stat
5151
from typing import IO, Callable, Optional, Union
5252

53+
PathLike = Union[str, pathlib.Path, os.PathLike]
5354

54-
def append(var: str, filename: Union[str, pathlib.Path, os.PathLike]):
55+
56+
def append(var: str, filename: PathLike):
5557
"""
5658
Append ``var`` to the file ``filename`` in the current directory.
5759
@@ -63,26 +65,23 @@ def append(var: str, filename: Union[str, pathlib.Path, os.PathLike]):
6365
6466
:param var: The value to append to the file
6567
:param filename: The file to append to
66-
:type filename: str or pathlib.Path or os.PathLike
6768
"""
6869

6970
with open(os.path.join(os.getcwd(), filename), 'a') as f:
7071
f.write(var)
7172

7273

7374
def copytree(
74-
src: Union[str, pathlib.Path, os.PathLike],
75-
dst: Union[str, pathlib.Path, os.PathLike],
75+
src: PathLike,
76+
dst: PathLike,
7677
symlinks: bool = False,
7778
ignore: Optional[Callable] = None,
7879
):
7980
"""
8081
Alternative to :func:`shutil.copytree` to work in some situations where it doesn't.
8182
8283
:param src: Source file to copy
83-
:type src: str or pathlib.Path or os.PathLike
8484
:param dst: Destination to copy file to
85-
:type dst: str or pathlib.Path or os.PathLike
8685
:param symlinks: Whether to represent symbolic links in the source as symbolic
8786
links in the destination. If false or omitted, the contents and metadata
8887
of the linked files are copied to the new tree. When symlinks is false,
@@ -101,7 +100,6 @@ def copytree(
101100
:class:`python:shutil.ignore_patterns` can be used to create such a callable
102101
that ignores names based on
103102
glob-style patterns.
104-
:type ignore: ~typing.Callable, optional
105103
"""
106104

107105
for item in os.listdir(src):
@@ -113,7 +111,7 @@ def copytree(
113111
shutil.copy2(s, d)
114112

115113

116-
def delete(filename: Union[str, pathlib.Path, os.PathLike]):
114+
def delete(filename: PathLike):
117115
"""
118116
Delete the file in the current directory.
119117
@@ -124,14 +122,13 @@ def delete(filename: Union[str, pathlib.Path, os.PathLike]):
124122
TODO: make this the file in the given directory, by default the current directory
125123
126124
:param filename: The file to delete
127-
:type filename: str or pathlib.Path or os.PathLike
128125
"""
129126

130127
os.remove(os.path.join(os.getcwd(), filename))
131128

132129

133130
def maybe_make(
134-
directory: Union[str, pathlib.Path, os.PathLike],
131+
directory: PathLike,
135132
mode=0o777,
136133
parents: bool = False,
137134
exist_ok: bool = False
@@ -140,7 +137,6 @@ def maybe_make(
140137
Create a directory at this given path, but only if the directory does not already exist.
141138
142139
:param directory: Directory to create
143-
:type directory: str or pathlib.Path or os.PathLike
144140
:param mode: Combined with the process’ umask value to determine the file mode and access flags
145141
:type mode:
146142
:param parents: If ``False`` (the default), a missing parent raises a :class:`~python:FileNotFoundError`.
@@ -161,15 +157,13 @@ def maybe_make(
161157
directory.mkdir(mode, parents, exist_ok)
162158

163159

164-
def parent_path(path: Union[str, pathlib.Path, os.PathLike]) -> pathlib.Path:
160+
def parent_path(path: PathLike) -> pathlib.Path:
165161
"""
166162
Returns the path of the parent directory for the given file or directory
167163
168164
:param path: Path to find the parent for
169-
:type path: str or pathlib.Path or os.PathLike
170165
171166
:return: The parent directory
172-
:rtype: pathlib.Path
173167
"""
174168

175169
if not isinstance(path, pathlib.Path):
@@ -178,7 +172,7 @@ def parent_path(path: Union[str, pathlib.Path, os.PathLike]) -> pathlib.Path:
178172
return path.parent
179173

180174

181-
def read(filename: Union[str, pathlib.Path, os.PathLike]) -> str:
175+
def read(filename: PathLike) -> str:
182176
"""
183177
Read a file in the current directory (in text mode)
184178
@@ -189,7 +183,6 @@ def read(filename: Union[str, pathlib.Path, os.PathLike]) -> str:
189183
TODO: make this the file in the given directory, by default the current directory
190184
191185
:param filename: The file to read from
192-
:type filename: str or pathlib.Path or os.PathLike
193186
194187
:return: The contents of the file
195188
:rtype: str
@@ -202,20 +195,18 @@ def read(filename: Union[str, pathlib.Path, os.PathLike]) -> str:
202195

203196

204197
def relpath(
205-
path: Union[str, pathlib.Path, os.PathLike],
206-
relative_to: Optional[Union[str, pathlib.Path, os.PathLike]] = None
198+
path: PathLike,
199+
relative_to: Optional[PathLike] = None
207200
) -> pathlib.Path:
208201
"""
209202
Returns the path for the given file or directory relative to the given
210203
directory or, if that would require path traversal, returns the absolute path.
211204
212205
:param path: Path to find the relative path for
213-
:type path: str or pathlib.Path or os.PathLike
214206
:param relative_to: The directory to find the path relative to.
215207
Defaults to the current directory
216-
:type relative_to: str or pathlib.Path or os.PathLike, optional
217208
218-
:rtype: pathlib.Path
209+
:return:
219210
"""
220211

221212
if not isinstance(path, pathlib.Path):
@@ -240,29 +231,27 @@ def relpath(
240231
relpath2 = relpath
241232

242233

243-
def write(var: str, filename: Union[str, pathlib.Path, os.PathLike]):
234+
def write(var: str, filename: PathLike) -> None:
244235
"""
245236
Write a variable to file in the current directory
246237
247238
TODO: make this the file in the given directory, by default the current directory
248239
249240
:param var: The value to write to the file
250241
:param filename: The file to write to
251-
:type filename: str or pathlib.Path or os.PathLike
252242
"""
253243

254244
with open(os.path.join(os.getcwd(), filename), 'w') as f:
255245
f.write(var)
256246

257247

258-
def clean_writer(string: str, fp: IO[str]):
248+
def clean_writer(string: str, fp: IO) -> None:
259249
"""
260-
Write string to fp without trailing spaces
250+
Write string to ``fp`` without trailing spaces
261251
262252
:param string:
263-
:type string:
253+
:type string: str
264254
:param fp:
265-
:type fp:
266255
"""
267256

268257
buffer = []
@@ -278,13 +267,15 @@ def clean_writer(string: str, fp: IO[str]):
278267
fp.write("\n")
279268

280269

281-
def make_executable(filename):
270+
def make_executable(filename: PathLike) -> None:
282271
"""
283272
Make the given file executable
284273
285274
:param filename:
286-
:type filename: str or pathlib.Path
287275
"""
288276

277+
if not isinstance(filename, pathlib.Path):
278+
filename = pathlib.Path(filename)
279+
289280
st = os.stat(str(filename))
290281
os.chmod(str(filename), st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

0 commit comments

Comments
 (0)