1
- # flake8: noqa
2
1
# Author: Aziz Köksal
3
2
import glob
4
3
import os
5
-
6
- try :
7
- from os import supports_fd
8
- except ImportError :
9
- supports_fd = set ()
4
+ from typing import Iterable , Iterator , Union
10
5
11
6
12
7
class Path (str ):
@@ -20,12 +15,12 @@ class Path(str):
20
15
# Separator in the PATH environment variable.
21
16
pathsep = os .pathsep
22
17
23
- def __new__ (cls , * paths ) :
18
+ def __new__ (cls , * paths : str ) -> "Path" :
24
19
if len (paths ):
25
20
return super ().__new__ (cls , os .path .join (* paths ))
26
21
return super ().__new__ (cls )
27
22
28
- def __div__ (self , path ) :
23
+ def __div__ (self , path : str ) -> "Path" :
29
24
"""
30
25
Joins this path with another path.
31
26
@@ -36,7 +31,7 @@ def __div__(self, path):
36
31
37
32
__truediv__ = __div__
38
33
39
- def __rdiv__ (self , path ) :
34
+ def __rdiv__ (self , path : str ) -> "Path" :
40
35
"""
41
36
Joins this path with another path.
42
37
@@ -46,7 +41,7 @@ def __rdiv__(self, path):
46
41
47
42
__rtruediv__ = __rdiv__
48
43
49
- def __idiv__ (self , path ) :
44
+ def __idiv__ (self , path : str ) -> "Path" :
50
45
"""
51
46
Like __div__ but also assigns to the variable.
52
47
@@ -56,52 +51,52 @@ def __idiv__(self, path):
56
51
57
52
__itruediv__ = __idiv__
58
53
59
- def __add__ (self , path ) :
54
+ def __add__ (self , path : str ) -> "Path" :
60
55
"""
61
56
>>> Path('/home/a') + 'bc.d'
62
57
'/home/abc.d'
63
58
"""
64
59
return Path (str (self ) + path )
65
60
66
- def __radd__ (self , path ) :
61
+ def __radd__ (self , path : str ) -> "Path" :
67
62
"""
68
63
>>> '/home/a' + Path('bc.d')
69
64
'/home/abc.d'
70
65
"""
71
66
return Path (path + str (self ))
72
67
73
- def __repr__ (self ):
68
+ def __repr__ (self ) -> str :
74
69
return "Path({inner})" .format (inner = str .__repr__ (self ))
75
70
76
71
@property
77
- def name (self ):
72
+ def name (self ) -> str :
78
73
"""
79
74
'/home/a/bc.d' -> 'bc.d'
80
75
"""
81
76
return os .path .basename (self )
82
77
83
78
@property
84
- def stem (self ):
79
+ def stem (self ) -> str :
85
80
"""
86
81
'/home/a/bc.d' -> 'bc'
87
82
"""
88
83
return Path (os .path .splitext (self )[0 ]).name
89
84
90
85
@property
91
- def suffix (self ):
86
+ def suffix (self ) -> str :
92
87
"""
93
88
'/home/a/bc.d' -> '.d'
94
89
"""
95
90
return Path (os .path .splitext (self )[1 ])
96
91
97
- def resolve (self ):
92
+ def resolve (self ) -> "Path" :
98
93
"""
99
94
Resolves symbolic links.
100
95
"""
101
96
return Path (os .path .realpath (self ))
102
97
103
98
@property
104
- def parent (self ):
99
+ def parent (self ) -> "Path" :
105
100
"""
106
101
Returns the parent directory of this path.
107
102
@@ -111,13 +106,18 @@ def parent(self):
111
106
"""
112
107
return Path (os .path .dirname (self ))
113
108
114
- def exists (self ):
109
+ def exists (self ) -> bool :
115
110
"""
116
111
Returns True if the path exists.
117
112
"""
118
113
return os .path .exists (self )
119
114
120
- def mkdir (self , mode = 0x1FF , exist_ok = False , parents = False ): # 0o777
115
+ def mkdir (
116
+ self ,
117
+ mode : int = 0o777 ,
118
+ exist_ok : bool = False ,
119
+ parents : bool = False ,
120
+ ) -> None :
121
121
"""
122
122
Creates a directory, if it doesn't exist already.
123
123
@@ -131,32 +131,32 @@ def mkdir(self, mode=0x1FF, exist_ok=False, parents=False): # 0o777
131
131
if not exist_ok or not os .path .isdir (self ):
132
132
raise
133
133
134
- def unlink (self ):
134
+ def unlink (self ) -> None :
135
135
"""
136
136
Removes a file.
137
137
"""
138
- return os .remove (self )
138
+ os .remove (self )
139
139
140
- def rmdir (self ):
140
+ def rmdir (self ) -> None :
141
141
"""
142
142
Removes a directory.
143
143
"""
144
- return os .rmdir (self )
144
+ os .rmdir (self )
145
145
146
- def rename (self , to ) :
146
+ def rename (self , to : str ) -> None :
147
147
"""
148
148
Renames a file or directory. May throw an OSError.
149
149
"""
150
- return os .rename (self , to )
150
+ os .rename (self , to )
151
151
152
- def glob (self , pattern ) :
152
+ def glob (self , pattern : str ) -> Iterator [ "Path" ] :
153
153
return (Path (i ) for i in glob .iglob (self .joinpath (pattern )))
154
154
155
- def joinpath (self , * parts ) :
155
+ def joinpath (self , * parts : str ) -> "Path" :
156
156
return Path (self , * parts )
157
157
158
158
# TODO: Remove after removing inheritance from str.
159
- def join (self , * parts ) :
159
+ def join (self , parts : Iterable [ str ]) -> str :
160
160
raise RuntimeError ("Path.join is invalid, use joinpath instead." )
161
161
162
162
def read_bytes (self ) -> bytes :
@@ -167,23 +167,23 @@ def write_bytes(self, content: bytes) -> None:
167
167
with open (self , "wb" ) as f :
168
168
f .write (content )
169
169
170
- def read_text (self ):
170
+ def read_text (self ) -> str :
171
171
with open (self , "r" ) as fp :
172
172
return fp .read ()
173
173
174
- def write_text (self , content ) :
174
+ def write_text (self , content : str ) -> None :
175
175
with open (self , "w" ) as fp :
176
176
fp .write (content )
177
177
178
- def touch (self ):
178
+ def touch (self ) -> None :
179
179
with open (self , "a" ) as fp :
180
- path = fp .fileno () if os .utime in supports_fd else self
181
- os .utime (path , None ) # times is not optional on Python 2.7
180
+ path : Union [ int , str ] = fp .fileno () if os .utime in os . supports_fd else self
181
+ os .utime (path )
182
182
183
- def symlink_to (self , target ) :
183
+ def symlink_to (self , target : str ) -> None :
184
184
os .symlink (target , self )
185
185
186
- def stat (self ):
186
+ def stat (self ) -> os . stat_result :
187
187
return os .stat (self )
188
188
189
189
0 commit comments