Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle zero explicit_port #1413

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
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