-
Notifications
You must be signed in to change notification settings - Fork 142
fix: Host string parsing #1204
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
fix: Host string parsing #1204
Conversation
Changed the test_set_by_netrc function to use 'https://example.ai' instead of 'https://example.cn' for host verification. This aligns the test with the expected domain for SwanLab environment configuration.
THX! |
Moved host suffix removal logic to a dedicated function `remove_host_suffix` in env.py and updated its usage in package.py. This improves code reuse and consistency when handling host strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a bug in host string parsing where rstrip("api")
was incorrectly treating its argument as a character set, causing domains ending with .ai
, .app
, etc. to be truncated. The fix introduces a new remove_host_suffix
function that properly removes exact suffix matches.
- Replaces problematic
rstrip("api")
calls with a newremove_host_suffix
function - Adds comprehensive test coverage for the new suffix removal functionality
- Updates existing test cases to use
.ai
domains to demonstrate the fix
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
swanlab/env.py | Implements new remove_host_suffix function and updates host parsing logic |
swanlab/package.py | Replaces rstrip calls with remove_host_suffix function calls |
test/unit/test_env.py | Adds comprehensive tests for suffix removal and updates existing tests to use .ai domains |
test/unit/test_package.py | Updates test cases but uses incorrect hardcoded slicing approach |
Comments suppressed due to low confidence (2)
test/unit/test_env.py:101
- The test name "test_handles_host_with_blank_suffix" is misleading. The test actually checks a host with trailing whitespace and a suffix that doesn't match. Consider renaming to "test_handles_host_with_whitespace_and_non_matching_suffix" to better reflect what is being tested.
def test_handles_host_with_blank_suffix():
test/unit/test_env.py:104
- This test case doesn't clearly validate the intended behavior. With host="example.com " and suffix="api", the expected result "example.com" suggests whitespace trimming, but the suffix "api" doesn't match the host ending. Consider adding a clearer test case that validates whitespace trimming behavior separately.
assert remove_host_suffix(host, suffix) == "example.com"
Replaces manual host suffix removal with the remove_host_suffix utility in TestSaveKey. Also updates the API_HOST environment variable assignment in TestGetKey for consistency.
Description
Replace
rstrip("api")
with an exact"/api"
suffix removal to avoid stripping valid domain endings like.ai
,.app
, etc.rstrip("api")
treats its argument as a character set and removes all trailinga/p/i
, causing hosts such aswww.example.ai
to becomewww.example.
.The fix usesstr.removesuffix("/api")
(Py≥3.9) with a safe fallback for older Python versions.The patch calls removesuffix("/api") on Python 3.9+ and falls back to simple slicing ([:-4]) on earlier python versions.
New tests demonstrate the incorrect behavior and verify the corrected result.
Closes: #1203
🎯 PRs Should Target Issues
This PR is tied to the bug report above. If a different issue ID is used, update the
Closes:
line accordingly.Changes
rstrip("api")
with an exact"/api"
suffix removal..ai
domain not being truncated"/api"
suffix removal/apip
,/appi
, etc.) left untouched"/api"
stripped when repeated