diff --git a/src/hyperlink/_url.py b/src/hyperlink/_url.py index 8797b5c..6e86804 100644 --- a/src/hyperlink/_url.py +++ b/src/hyperlink/_url.py @@ -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, diff --git a/src/hyperlink/test/test_hypothesis.py b/src/hyperlink/test/test_hypothesis.py index 776ed7b..a16eb6f 100644 --- a/src/hyperlink/test/test_hypothesis.py +++ b/src/hyperlink/test/test_hypothesis.py @@ -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 @@ -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 """ diff --git a/src/hyperlink/test/test_parse.py b/src/hyperlink/test/test_parse.py index 66b0270..2e48ffe 100644 --- a/src/hyperlink/test/test_parse.py +++ b/src/hyperlink/test/test_parse.py @@ -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" diff --git a/tox.ini b/tox.ini index 8865d17..4cbd78a 100644 --- a/tox.ini +++ b/tox.ini @@ -94,6 +94,7 @@ skip_install = True deps = black==21.7b0 + click<8.0 setenv = BLACK_LINT_ARGS=--check