Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor: improve host parsing for IPv4/IPv6 and add tests
  • Loading branch information
mvaught committed Jan 30, 2026
commit cadc40dfad09cf3b7292afc0243f05fdb7997b4e
15 changes: 10 additions & 5 deletions src/hyperlink/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1667,11 +1667,16 @@ def to_uri(self):
new_path = _encode_path_parts(
self.path, has_scheme=bool(self.scheme), rooted=False, maximal=True
)
new_host = (
self.host
if not self.host
else idna_encode(self.host, uts46=True).decode("ascii")
)
family, host_text = parse_host(self.host)
if family is not None:
# IPv4 / IPv6 literal
new_host = host_text
else:
new_host = (
self.host
if not self.host
else idna_encode(self.host, uts46=True).decode("ascii")
)
return self.replace(
userinfo=new_userinfo,
host=new_host,
Expand Down
3 changes: 2 additions & 1 deletion src/hyperlink/test/test_hypothesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
except ImportError:
from mock import patch # type: ignore[misc]

from hypothesis import given, settings
from hypothesis import given, settings, HealthCheck
from hypothesis.strategies import SearchStrategy, data

from idna import IDNAError, check_label, encode as idna_encode
Expand Down Expand Up @@ -174,6 +174,7 @@ def test_hostnames_ascii(self, hostname):
)

@given(hostnames(allow_leading_digit=False, allow_idn=False))
@settings(suppress_health_check=[HealthCheck.filter_too_much])
def test_hostnames_ascii_nolead(self, hostname):
# type: (Text) -> None
"""
Expand Down
5 changes: 5 additions & 0 deletions src/hyperlink/test/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ def test_parse(self):

with self.assertRaises(UnicodeDecodeError):
purl3.fragment

def test_ipv6_literal_not_idna_encoded(self) -> None:

url = EncodedURL.from_text("http://[::1]:8080/path")
assert url.to_uri().to_text() == "http://[::1]:8080/path"
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ skip_install = True

deps =
black==21.7b0
click<8.0

setenv =
BLACK_LINT_ARGS=--check
Expand Down
Loading