Skip to content

Commit

Permalink
Correctly handle zero explicit_port
Browse files Browse the repository at this point in the history
  • Loading branch information
George Macon committed Nov 9, 2024
1 parent dfc41db commit 8b183cb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES/1413.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a bug causing ``URL.port`` to return the default port when the given port was zero
-- by :user:`gmacon`.
6 changes: 6 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ def test_port_for_unknown_scheme():
assert url.explicit_port is None


def test_explicit_zero_port():
url = URL("http://example.com:0")
assert url.explicit_port == 0
assert url.port == 0


def test_explicit_port_for_explicit_port():
url = URL("http://example.com:8888")
assert 8888 == url.explicit_port
Expand Down
5 changes: 4 additions & 1 deletion yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,10 @@ def port(self) -> Union[int, None]:
scheme without default port substitution.
"""
return self.explicit_port or self._default_port
port = self.explicit_port
if port is None:
port = self._default_port
return port

@cached_property
def explicit_port(self) -> Union[int, None]:
Expand Down

0 comments on commit 8b183cb

Please sign in to comment.