1
1
"""Module containing the implementation of the URIMixin class."""
2
+ import typing as t
2
3
import warnings
3
4
4
5
from . import exceptions as exc
5
6
from . import misc
6
7
from . import normalizers
7
8
from . import validators
9
+ from ._typing_compat import Self as _Self
10
+
11
+
12
+ class _AuthorityInfo (t .TypedDict ):
13
+ userinfo : t .Optional [str ]
14
+ host : t .Optional [str ]
15
+ port : t .Optional [str ]
8
16
9
17
10
18
class URIMixin :
11
19
"""Mixin with all shared methods for URIs and IRIs."""
12
20
13
- def authority_info (self ):
21
+ def authority_info (self ) -> _AuthorityInfo :
14
22
"""Return a dictionary with the ``userinfo``, ``host``, and ``port``.
15
23
16
24
If the authority is not valid, it will raise a
@@ -51,11 +59,11 @@ def authority_info(self):
51
59
52
60
return matches
53
61
54
- def _match_subauthority (self ):
62
+ def _match_subauthority (self ) -> t . Optional [ t . Match [ str ]] :
55
63
return misc .SUBAUTHORITY_MATCHER .match (self .authority )
56
64
57
65
@property
58
- def _validator (self ):
66
+ def _validator (self ) -> validators . Validator :
59
67
v = getattr (self , "_cached_validator" , None )
60
68
if v is not None :
61
69
return v
@@ -65,7 +73,7 @@ def _validator(self):
65
73
return self ._cached_validator
66
74
67
75
@property
68
- def host (self ):
76
+ def host (self ) -> t . Optional [ str ] :
69
77
"""If present, a string representing the host."""
70
78
try :
71
79
authority = self .authority_info ()
@@ -74,7 +82,7 @@ def host(self):
74
82
return authority ["host" ]
75
83
76
84
@property
77
- def port (self ):
85
+ def port (self ) -> t . Optional [ str ] :
78
86
"""If present, the port extracted from the authority."""
79
87
try :
80
88
authority = self .authority_info ()
@@ -83,15 +91,15 @@ def port(self):
83
91
return authority ["port" ]
84
92
85
93
@property
86
- def userinfo (self ):
94
+ def userinfo (self ) -> t . Optional [ str ] :
87
95
"""If present, the userinfo extracted from the authority."""
88
96
try :
89
97
authority = self .authority_info ()
90
98
except exc .InvalidAuthority :
91
99
return None
92
100
return authority ["userinfo" ]
93
101
94
- def is_absolute (self ):
102
+ def is_absolute (self ) -> bool :
95
103
"""Determine if this URI Reference is an absolute URI.
96
104
97
105
See http://tools.ietf.org/html/rfc3986#section-4.3 for explanation.
@@ -135,7 +143,7 @@ def is_valid(self, **kwargs: bool) -> bool:
135
143
]
136
144
return all (v (r ) for v , r in validators )
137
145
138
- def authority_is_valid (self , require = False ):
146
+ def authority_is_valid (self , require : bool = False ) -> bool :
139
147
"""Determine if the authority component is valid.
140
148
141
149
.. deprecated:: 1.1.0
@@ -165,7 +173,7 @@ def authority_is_valid(self, require=False):
165
173
require = require ,
166
174
)
167
175
168
- def scheme_is_valid (self , require = False ):
176
+ def scheme_is_valid (self , require : bool = False ) -> bool :
169
177
"""Determine if the scheme component is valid.
170
178
171
179
.. deprecated:: 1.1.0
@@ -184,7 +192,7 @@ def scheme_is_valid(self, require=False):
184
192
)
185
193
return validators .scheme_is_valid (self .scheme , require )
186
194
187
- def path_is_valid (self , require = False ):
195
+ def path_is_valid (self , require : bool = False ) -> bool :
188
196
"""Determine if the path component is valid.
189
197
190
198
.. deprecated:: 1.1.0
@@ -203,7 +211,7 @@ def path_is_valid(self, require=False):
203
211
)
204
212
return validators .path_is_valid (self .path , require )
205
213
206
- def query_is_valid (self , require = False ):
214
+ def query_is_valid (self , require : bool = False ) -> bool :
207
215
"""Determine if the query component is valid.
208
216
209
217
.. deprecated:: 1.1.0
@@ -222,7 +230,7 @@ def query_is_valid(self, require=False):
222
230
)
223
231
return validators .query_is_valid (self .query , require )
224
232
225
- def fragment_is_valid (self , require = False ):
233
+ def fragment_is_valid (self , require : bool = False ) -> bool :
226
234
"""Determine if the fragment component is valid.
227
235
228
236
.. deprecated:: 1.1.0
@@ -241,7 +249,7 @@ def fragment_is_valid(self, require=False):
241
249
)
242
250
return validators .fragment_is_valid (self .fragment , require )
243
251
244
- def normalized_equality (self , other_ref ):
252
+ def normalized_equality (self , other_ref ) -> bool :
245
253
"""Compare this URIReference to another URIReference.
246
254
247
255
:param URIReference other_ref: (required), The reference with which
@@ -251,7 +259,7 @@ def normalized_equality(self, other_ref):
251
259
"""
252
260
return tuple (self .normalize ()) == tuple (other_ref .normalize ())
253
261
254
- def resolve_with (self , base_uri , strict = False ):
262
+ def resolve_with (self , base_uri , strict : bool = False ) -> _Self :
255
263
"""Use an absolute URI Reference to resolve this relative reference.
256
264
257
265
Assuming this is a relative reference that you would like to resolve,
@@ -323,14 +331,14 @@ def resolve_with(self, base_uri, strict=False):
323
331
)
324
332
return target
325
333
326
- def unsplit (self ):
334
+ def unsplit (self ) -> str :
327
335
"""Create a URI string from the components.
328
336
329
337
:returns: The URI Reference reconstituted as a string.
330
338
:rtype: str
331
339
"""
332
340
# See http://tools.ietf.org/html/rfc3986#section-5.3
333
- result_list = []
341
+ result_list : list [ str ] = []
334
342
if self .scheme :
335
343
result_list .extend ([self .scheme , ":" ])
336
344
if self .authority :
@@ -345,12 +353,12 @@ def unsplit(self):
345
353
346
354
def copy_with (
347
355
self ,
348
- scheme = misc .UseExisting ,
349
- authority = misc .UseExisting ,
350
- path = misc .UseExisting ,
351
- query = misc .UseExisting ,
352
- fragment = misc .UseExisting ,
353
- ):
356
+ scheme : t . Optional [ str ] = misc .UseExisting ,
357
+ authority : t . Optional [ str ] = misc .UseExisting ,
358
+ path : t . Optional [ str ] = misc .UseExisting ,
359
+ query : t . Optional [ str ] = misc .UseExisting ,
360
+ fragment : t . Optional [ str ] = misc .UseExisting ,
361
+ ) -> _Self :
354
362
"""Create a copy of this reference with the new components.
355
363
356
364
:param str scheme:
0 commit comments