Skip to content

Commit dd96a34

Browse files
Fix compat.py and api.py parameter annotations to accept bytearray as well.
- This provides parity with the way urllib.parse is typed and the runtime implementation, since bytearray isn't a str and also has a `decode(encoding)` method. - Adjusted compat functions to also accept bytearray for the same reasons. - Adjusted the parameter types of the other functions called in api.py that are part of the `(U|I)RIReference` interfaces to make sure api.py fully type-checks. - TODO: Consider making a typealias for `t.Union[str, bytes, bytearray]` and using that everywhere to avoid verbosity? - TODO: For the **kwargs functions in api.py and `URLMixin.is_valid()`, consider enumerating the possible keyword-only parameters?
1 parent c3b49f2 commit dd96a34

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

src/rfc3986/_mixin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def is_absolute(self):
103103
"""
104104
return bool(misc.ABSOLUTE_URI_MATCHER.match(self.unsplit()))
105105

106-
def is_valid(self, **kwargs):
106+
def is_valid(self, **kwargs: bool) -> bool:
107107
"""Determine if the URI is valid.
108108
109109
.. deprecated:: 1.1.0

src/rfc3986/api.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
This module defines functions and provides access to the public attributes
1818
and classes of rfc3986.
1919
"""
20+
import typing as t
21+
2022
from .iri import IRIReference
2123
from .parseresult import ParseResult
2224
from .uri import URIReference
2325

2426

25-
def uri_reference(uri: str, encoding: str = "utf-8") -> URIReference:
27+
def uri_reference(
28+
uri: t.Union[str, bytes, bytearray],
29+
encoding: str = "utf-8",
30+
) -> URIReference:
2631
"""Parse a URI string into a URIReference.
2732
2833
This is a convenience function. You could achieve the same end by using
@@ -36,7 +41,10 @@ def uri_reference(uri: str, encoding: str = "utf-8") -> URIReference:
3641
return URIReference.from_string(uri, encoding)
3742

3843

39-
def iri_reference(iri: str, encoding: str = "utf-8") -> IRIReference:
44+
def iri_reference(
45+
iri: t.Union[str, bytes, bytearray],
46+
encoding: str = "utf-8",
47+
) -> IRIReference:
4048
"""Parse a IRI string into an IRIReference.
4149
4250
This is a convenience function. You could achieve the same end by using
@@ -50,7 +58,11 @@ def iri_reference(iri: str, encoding: str = "utf-8") -> IRIReference:
5058
return IRIReference.from_string(iri, encoding)
5159

5260

53-
def is_valid_uri(uri: str, encoding: str = "utf-8", **kwargs: bool) -> bool:
61+
def is_valid_uri(
62+
uri: t.Union[str, bytes, bytearray],
63+
encoding: str = "utf-8",
64+
**kwargs: bool,
65+
) -> bool:
5466
"""Determine if the URI given is valid.
5567
5668
This is a convenience function. You could use either
@@ -75,7 +87,10 @@ def is_valid_uri(uri: str, encoding: str = "utf-8", **kwargs: bool) -> bool:
7587
return URIReference.from_string(uri, encoding).is_valid(**kwargs)
7688

7789

78-
def normalize_uri(uri: str, encoding: str = "utf-8") -> str:
90+
def normalize_uri(
91+
uri: t.Union[str, bytes, bytearray],
92+
encoding: str = "utf-8",
93+
) -> str:
7994
"""Normalize the given URI.
8095
8196
This is a convenience function. You could use either
@@ -91,7 +106,10 @@ def normalize_uri(uri: str, encoding: str = "utf-8") -> str:
91106
return normalized_reference.unsplit()
92107

93108

94-
def urlparse(uri: str, encoding: str = "utf-8") -> ParseResult:
109+
def urlparse(
110+
uri: t.Union[str, bytes, bytearray],
111+
encoding: str = "utf-8",
112+
) -> ParseResult:
95113
"""Parse a given URI and return a ParseResult.
96114
97115
This is a partial replacement of the standard library's urlparse function.

src/rfc3986/compat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
@t.overload
2424
def to_str( # noqa: D103
25-
b: t.Union[str, bytes],
25+
b: t.Union[str, bytes, bytearray],
2626
encoding: str = "utf-8",
2727
) -> str:
2828
...
@@ -34,7 +34,7 @@ def to_str(b: None, encoding: str = "utf-8") -> None: # noqa: D103
3434

3535

3636
def to_str(
37-
b: t.Optional[t.Union[str, bytes]],
37+
b: t.Optional[t.Union[str, bytes, bytearray]],
3838
encoding: str = "utf-8",
3939
) -> t.Optional[str]:
4040
"""Ensure that b is text in the specified encoding."""
@@ -45,7 +45,7 @@ def to_str(
4545

4646
@t.overload
4747
def to_bytes( # noqa: D103
48-
s: t.Union[str, bytes],
48+
s: t.Union[str, bytes, bytearray],
4949
encoding: str = "utf-8",
5050
) -> bytes:
5151
...
@@ -57,7 +57,7 @@ def to_bytes(s: None, encoding: str = "utf-8") -> None: # noqa: D103
5757

5858

5959
def to_bytes(
60-
s: t.Optional[t.Union[str, bytes]],
60+
s: t.Optional[t.Union[str, bytes, bytearray]],
6161
encoding: str = "utf-8",
6262
) -> t.Optional[bytes]:
6363
"""Ensure that s is converted to bytes from the encoding."""

src/rfc3986/iri.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
import typing as t
1617
from collections import namedtuple
1718

1819
from . import compat
@@ -80,7 +81,11 @@ def _match_subauthority(self):
8081
return misc.ISUBAUTHORITY_MATCHER.match(self.authority)
8182

8283
@classmethod
83-
def from_string(cls, iri_string, encoding="utf-8"):
84+
def from_string(
85+
cls,
86+
iri_string: t.Union[str, bytes, bytearray],
87+
encoding: str = "utf-8",
88+
):
8489
"""Parse a IRI reference from the given unicode IRI string.
8590
8691
:param str iri_string: Unicode IRI to be parsed into a reference.

src/rfc3986/parseresult.py

Lines changed: 6 additions & 1 deletion
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 urlparse compatibility logic."""
15+
import typing as t
1516
from collections import namedtuple
1617

1718
from . import compat
@@ -155,7 +156,11 @@ def from_parts(
155156

156157
@classmethod
157158
def from_string(
158-
cls, uri_string, encoding="utf-8", strict=True, lazy_normalize=True
159+
cls,
160+
uri_string: t.Union[str, bytes, bytearray],
161+
encoding: str = "utf-8",
162+
strict: bool = True,
163+
lazy_normalize: bool = True,
159164
):
160165
"""Parse a URI from the given unicode URI string.
161166

src/rfc3986/uri.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
import typing as t
1617
from collections import namedtuple
1718

1819
from . import compat
@@ -140,7 +141,11 @@ def normalize(self):
140141
)
141142

142143
@classmethod
143-
def from_string(cls, uri_string, encoding="utf-8"):
144+
def from_string(
145+
cls,
146+
uri_string: t.Union[str, bytes, bytearray],
147+
encoding: str = "utf-8",
148+
):
144149
"""Parse a URI reference from the given unicode URI string.
145150
146151
:param str uri_string: Unicode URI to be parsed into a reference.

0 commit comments

Comments
 (0)