@@ -22,8 +22,9 @@ class DirectUrlValidationError(Exception):
22
22
pass
23
23
24
24
25
- def _get (d , expected_type , key , default = None ):
26
- # type: (Dict[str, Any], Type[T], str, Optional[T]) -> Optional[T]
25
+ def _get (
26
+ d : Dict [str , Any ], expected_type : Type [T ], key : str , default : Optional [T ] = None
27
+ ) -> Optional [T ]:
27
28
"""Get value from dictionary and verify expected type."""
28
29
if key not in d :
29
30
return default
@@ -37,16 +38,16 @@ def _get(d, expected_type, key, default=None):
37
38
return value
38
39
39
40
40
- def _get_required (d , expected_type , key , default = None ):
41
- # type: (Dict[str, Any], Type[T], str, Optional[T]) -> T
41
+ def _get_required (
42
+ d : Dict [str , Any ], expected_type : Type [T ], key : str , default : Optional [T ] = None
43
+ ) -> T :
42
44
value = _get (d , expected_type , key , default )
43
45
if value is None :
44
46
raise DirectUrlValidationError (f"{ key } must have a value" )
45
47
return value
46
48
47
49
48
- def _exactly_one_of (infos ):
49
- # type: (Iterable[Optional[InfoType]]) -> InfoType
50
+ def _exactly_one_of (infos : Iterable [Optional ["InfoType" ]]) -> "InfoType" :
50
51
infos = [info for info in infos if info is not None ]
51
52
if not infos :
52
53
raise DirectUrlValidationError (
@@ -60,8 +61,7 @@ def _exactly_one_of(infos):
60
61
return infos [0 ]
61
62
62
63
63
- def _filter_none (** kwargs ):
64
- # type: (Any) -> Dict[str, Any]
64
+ def _filter_none (** kwargs : Any ) -> Dict [str , Any ]:
65
65
"""Make dict excluding None values."""
66
66
return {k : v for k , v in kwargs .items () if v is not None }
67
67
@@ -71,21 +71,20 @@ class VcsInfo:
71
71
72
72
def __init__ (
73
73
self ,
74
- vcs , # type : str
75
- commit_id , # type : str
76
- requested_revision = None , # type : Optional[str]
77
- resolved_revision = None , # type : Optional[str]
78
- resolved_revision_type = None , # type : Optional[str]
79
- ):
74
+ vcs : str ,
75
+ commit_id : str ,
76
+ requested_revision : Optional [str ] = None ,
77
+ resolved_revision : Optional [str ] = None ,
78
+ resolved_revision_type : Optional [str ] = None ,
79
+ ) -> None :
80
80
self .vcs = vcs
81
81
self .requested_revision = requested_revision
82
82
self .commit_id = commit_id
83
83
self .resolved_revision = resolved_revision
84
84
self .resolved_revision_type = resolved_revision_type
85
85
86
86
@classmethod
87
- def _from_dict (cls , d ):
88
- # type: (Optional[Dict[str, Any]]) -> Optional[VcsInfo]
87
+ def _from_dict (cls , d : Optional [Dict [str , Any ]]) -> Optional ["VcsInfo" ]:
89
88
if d is None :
90
89
return None
91
90
return cls (
@@ -96,8 +95,7 @@ def _from_dict(cls, d):
96
95
resolved_revision_type = _get (d , str , "resolved_revision_type" ),
97
96
)
98
97
99
- def _to_dict (self ):
100
- # type: () -> Dict[str, Any]
98
+ def _to_dict (self ) -> Dict [str , Any ]:
101
99
return _filter_none (
102
100
vcs = self .vcs ,
103
101
requested_revision = self .requested_revision ,
@@ -112,19 +110,17 @@ class ArchiveInfo:
112
110
113
111
def __init__ (
114
112
self ,
115
- hash = None , # type : Optional[str]
116
- ):
113
+ hash : Optional [str ] = None ,
114
+ ) -> None :
117
115
self .hash = hash
118
116
119
117
@classmethod
120
- def _from_dict (cls , d ):
121
- # type: (Optional[Dict[str, Any]]) -> Optional[ArchiveInfo]
118
+ def _from_dict (cls , d : Optional [Dict [str , Any ]]) -> Optional ["ArchiveInfo" ]:
122
119
if d is None :
123
120
return None
124
121
return cls (hash = _get (d , str , "hash" ))
125
122
126
- def _to_dict (self ):
127
- # type: () -> Dict[str, Any]
123
+ def _to_dict (self ) -> Dict [str , Any ]:
128
124
return _filter_none (hash = self .hash )
129
125
130
126
@@ -133,21 +129,19 @@ class DirInfo:
133
129
134
130
def __init__ (
135
131
self ,
136
- editable = False , # type : bool
137
- ):
132
+ editable : bool = False ,
133
+ ) -> None :
138
134
self .editable = editable
139
135
140
136
@classmethod
141
- def _from_dict (cls , d ):
142
- # type: (Optional[Dict[str, Any]]) -> Optional[DirInfo]
137
+ def _from_dict (cls , d : Optional [Dict [str , Any ]]) -> Optional ["DirInfo" ]:
143
138
if d is None :
144
139
return None
145
140
return cls (
146
141
editable = _get_required (d , bool , "editable" , default = False )
147
142
)
148
143
149
- def _to_dict (self ):
150
- # type: () -> Dict[str, Any]
144
+ def _to_dict (self ) -> Dict [str , Any ]:
151
145
return _filter_none (editable = self .editable or None )
152
146
153
147
@@ -158,16 +152,15 @@ class DirectUrl:
158
152
159
153
def __init__ (
160
154
self ,
161
- url , # type : str
162
- info , # type : InfoType
163
- subdirectory = None , # type : Optional[str]
164
- ):
155
+ url : str ,
156
+ info : InfoType ,
157
+ subdirectory : Optional [str ] = None ,
158
+ ) -> None :
165
159
self .url = url
166
160
self .info = info
167
161
self .subdirectory = subdirectory
168
162
169
- def _remove_auth_from_netloc (self , netloc ):
170
- # type: (str) -> str
163
+ def _remove_auth_from_netloc (self , netloc : str ) -> str :
171
164
if "@" not in netloc :
172
165
return netloc
173
166
user_pass , netloc_no_user_pass = netloc .split ("@" , 1 )
@@ -182,8 +175,7 @@ def _remove_auth_from_netloc(self, netloc):
182
175
return netloc_no_user_pass
183
176
184
177
@property
185
- def redacted_url (self ):
186
- # type: () -> str
178
+ def redacted_url (self ) -> str :
187
179
"""url with user:password part removed unless it is formed with
188
180
environment variables as specified in PEP 610, or it is ``git``
189
181
in the case of a git URL.
@@ -195,13 +187,11 @@ def redacted_url(self):
195
187
)
196
188
return surl
197
189
198
- def validate (self ):
199
- # type: () -> None
190
+ def validate (self ) -> None :
200
191
self .from_dict (self .to_dict ())
201
192
202
193
@classmethod
203
- def from_dict (cls , d ):
204
- # type: (Dict[str, Any]) -> DirectUrl
194
+ def from_dict (cls , d : Dict [str , Any ]) -> "DirectUrl" :
205
195
return DirectUrl (
206
196
url = _get_required (d , str , "url" ),
207
197
subdirectory = _get (d , str , "subdirectory" ),
@@ -214,8 +204,7 @@ def from_dict(cls, d):
214
204
),
215
205
)
216
206
217
- def to_dict (self ):
218
- # type: () -> Dict[str, Any]
207
+ def to_dict (self ) -> Dict [str , Any ]:
219
208
res = _filter_none (
220
209
url = self .redacted_url ,
221
210
subdirectory = self .subdirectory ,
@@ -224,10 +213,8 @@ def to_dict(self):
224
213
return res
225
214
226
215
@classmethod
227
- def from_json (cls , s ):
228
- # type: (str) -> DirectUrl
216
+ def from_json (cls , s : str ) -> "DirectUrl" :
229
217
return cls .from_dict (json .loads (s ))
230
218
231
- def to_json (self ):
232
- # type: () -> str
219
+ def to_json (self ) -> str :
233
220
return json .dumps (self .to_dict (), sort_keys = True )
0 commit comments