Skip to content

Commit 9df0355

Browse files
Add parameter annotations to builder.py.
- TODO: Consider changing the insides of the URIBuilder.add_* methods to use `type(self)(...)` instead of `URIBuilder(...)`. That way, subclassing is supported. Would result in the return annotations being Self as well. - Added trailing commas to function parameters in normalizers.py and validators.py for functions with multi-line signatures.
1 parent 719da58 commit 9df0355

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

src/rfc3986/builder.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"""Module containing the logic for the URIBuilder object."""
15+
import typing as t
1516
from urllib.parse import parse_qsl
1617
from urllib.parse import urlencode
1718

@@ -33,13 +34,13 @@ class URIBuilder:
3334

3435
def __init__(
3536
self,
36-
scheme=None,
37-
userinfo=None,
38-
host=None,
39-
port=None,
40-
path=None,
41-
query=None,
42-
fragment=None,
37+
scheme: t.Optional[str] = None,
38+
userinfo: t.Optional[str] = None,
39+
host: t.Optional[str] = None,
40+
port: t.Optional[str] = None,
41+
path: t.Optional[str] = None,
42+
query: t.Optional[str] = None,
43+
fragment: t.Optional[str] = None,
4344
):
4445
"""Initialize our URI builder.
4546
@@ -76,7 +77,7 @@ def __repr__(self):
7677
return formatstr.format(b=self)
7778

7879
@classmethod
79-
def from_uri(cls, reference):
80+
def from_uri(cls, reference: t.Union["uri.URIReference", str]):
8081
"""Initialize the URI builder from another URI.
8182
8283
Takes the given URI reference and creates a new URI builder instance
@@ -95,7 +96,7 @@ def from_uri(cls, reference):
9596
fragment=reference.fragment,
9697
)
9798

98-
def add_scheme(self, scheme):
99+
def add_scheme(self, scheme: str):
99100
"""Add a scheme to our builder object.
100101
101102
After normalizing, this will generate a new URIBuilder instance with
@@ -119,7 +120,7 @@ def add_scheme(self, scheme):
119120
fragment=self.fragment,
120121
)
121122

122-
def add_credentials(self, username, password):
123+
def add_credentials(self, username: str, password: t.Optional[str]):
123124
"""Add credentials as the userinfo portion of the URI.
124125
125126
.. code-block:: python
@@ -152,7 +153,7 @@ def add_credentials(self, username, password):
152153
fragment=self.fragment,
153154
)
154155

155-
def add_host(self, host):
156+
def add_host(self, host: str):
156157
"""Add hostname to the URI.
157158
158159
.. code-block:: python
@@ -172,7 +173,7 @@ def add_host(self, host):
172173
fragment=self.fragment,
173174
)
174175

175-
def add_port(self, port):
176+
def add_port(self, port: t.Union[str, int]):
176177
"""Add port to the URI.
177178
178179
.. code-block:: python
@@ -211,7 +212,7 @@ def add_port(self, port):
211212
fragment=self.fragment,
212213
)
213214

214-
def add_path(self, path):
215+
def add_path(self, path: str):
215216
"""Add a path to the URI.
216217
217218
.. code-block:: python
@@ -238,7 +239,7 @@ def add_path(self, path):
238239
fragment=self.fragment,
239240
)
240241

241-
def extend_path(self, path):
242+
def extend_path(self, path: str):
242243
"""Extend the existing path value with the provided value.
243244
244245
.. versionadded:: 1.5.0
@@ -314,7 +315,7 @@ def extend_query_with(self, query_items):
314315

315316
return self.add_query_from(original_query_items + query_items)
316317

317-
def add_query(self, query):
318+
def add_query(self, query: str):
318319
"""Add a pre-formated query string to the URI.
319320
320321
.. code-block:: python
@@ -334,7 +335,7 @@ def add_query(self, query):
334335
fragment=self.fragment,
335336
)
336337

337-
def add_fragment(self, fragment):
338+
def add_fragment(self, fragment: str):
338339
"""Add a fragment to the URI.
339340
340341
.. code-block:: python
@@ -354,7 +355,7 @@ def add_fragment(self, fragment):
354355
fragment=normalizers.normalize_fragment(fragment),
355356
)
356357

357-
def finalize(self):
358+
def finalize(self) -> "uri.URIReference":
358359
"""Create a URIReference from our builder.
359360
360361
.. code-block:: python

src/rfc3986/normalizers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ def encode_component(uri_component: str, encoding: str) -> str:
156156

157157

158158
def encode_component(
159-
uri_component: t.Optional[str], encoding: str
159+
uri_component: t.Optional[str],
160+
encoding: str,
160161
) -> t.Optional[str]:
161162
"""Encode the specific component in the provided encoding."""
162163
if uri_component is None:

src/rfc3986/validators.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ def ensure_required_components_exist(
273273

274274

275275
def is_valid(
276-
value: t.Optional[str], matcher: t.Pattern[str], require: bool
276+
value: t.Optional[str],
277+
matcher: t.Pattern[str],
278+
require: bool,
277279
) -> bool:
278280
"""Determine if a value is valid based on the provided matcher.
279281
@@ -292,7 +294,9 @@ def is_valid(
292294

293295

294296
def authority_is_valid(
295-
authority: str, host: t.Optional[str] = None, require: bool = False
297+
authority: str,
298+
host: t.Optional[str] = None,
299+
require: bool = False,
296300
) -> bool:
297301
"""Determine if the authority string is valid.
298302
@@ -379,7 +383,8 @@ def query_is_valid(query: t.Optional[str], require: bool = False) -> bool:
379383

380384

381385
def fragment_is_valid(
382-
fragment: t.Optional[str], require: bool = False
386+
fragment: t.Optional[str],
387+
require: bool = False,
383388
) -> bool:
384389
"""Determine if the fragment component is valid.
385390
@@ -413,7 +418,8 @@ def valid_ipv4_host_address(host: str) -> bool:
413418

414419

415420
def subauthority_component_is_valid(
416-
uri: "uri.URIReference", component: str
421+
uri: "uri.URIReference",
422+
component: str,
417423
) -> bool:
418424
"""Determine if the userinfo, host, and port are valid."""
419425
try:
@@ -429,7 +435,7 @@ def subauthority_component_is_valid(
429435
return True
430436

431437
try:
432-
port = int(subauthority_dict["port"])
438+
port = int(subauthority_dict["port"]) # pyright: ignore[reportArgumentType] # Guarded by "except TypeError".
433439
except TypeError:
434440
# If the port wasn't provided it'll be None and int(None) raises a
435441
# TypeError
@@ -439,7 +445,8 @@ def subauthority_component_is_valid(
439445

440446

441447
def ensure_components_are_valid(
442-
uri: "uri.URIReference", validated_components: t.List[str]
448+
uri: "uri.URIReference",
449+
validated_components: t.List[str],
443450
) -> None:
444451
"""Assert that all components are valid in the URI."""
445452
invalid_components: set[str] = set()

0 commit comments

Comments
 (0)