diff --git a/CHANGES/883.bugfix b/CHANGES/883.bugfix new file mode 100644 index 00000000..464c38b4 --- /dev/null +++ b/CHANGES/883.bugfix @@ -0,0 +1 @@ +Fix URL.build with port="" creating invalid URLs diff --git a/tests/test_url_build.py b/tests/test_url_build.py index 51969fa8..03478990 100644 --- a/tests/test_url_build.py +++ b/tests/test_url_build.py @@ -39,6 +39,9 @@ def test_build_with_port(): u = URL.build(scheme="http", host="127.0.0.1", port=8000) assert str(u) == "http://127.0.0.1:8000" + u = URL.build(scheme="http", host="127.0.0.1", port="", path="/v1") + assert str(u) == "http://127.0.0.1/v1", str(u) + def test_build_with_user(): u = URL.build(scheme="http", host="127.0.0.1", user="foo") diff --git a/yarl/_url.py b/yarl/_url.py index c8f2acb3..685313cf 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -233,6 +233,8 @@ def build( raise ValueError( 'Can\'t mix "authority" with "user", "password", "host" or "port".' ) + if isinstance(port, str) and port == "": + port = None if port and not host: raise ValueError('Can\'t build URL with "port" but without "host".') if query and query_string: