fix: trim whitespace from API keys and host config#525
Merged
marandaneto merged 7 commits intomainfrom Apr 20, 2026
Merged
Conversation
Contributor
Prompt To Fix All With AIThis is a comment left during a code review.
Path: posthog/test/test_client.py
Line: 44-62
Comment:
**Missing "trim from a valid key" case**
The test only exercises the all-whitespace path (`" \n\t "`) where the result is always an empty/None value. The primary purpose of this fix is to strip surrounding whitespace from a *valid* key (e.g., `" phc_mykey "` → `"phc_mykey"`), but that case is never asserted. A parametrised test covering both a valid-but-padded key and the whitespace-only key would express every idea the feature needs to express and would catch a regression where `.strip()` is accidentally removed.
```python
@pytest.mark.parametrize(
"raw_key,expected",
[
(" phc_validkey ", "phc_validkey"), # trim from a valid key
(" \n\t ", ""), # all-whitespace collapses to empty
],
)
def test_trims_api_key_whitespace(raw_key, expected):
...
assert client.api_key == expected
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix: trim whitespace from API keys and h..." | Re-trigger Greptile |
Contributor
posthog-python Compliance ReportDate: 2026-04-20 14:24:22 UTC ✅ All Tests Passed!30/30 tests passed Capture Tests✅ 29/29 tests passed View Details
Feature_Flags Tests✅ 1/1 tests passed View Details
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
This ports the whitespace-trimming fix from
posthog-gotoposthog-python. It trims surrounding spaces and line breaks from the project API key, personal API key, and host before using them so the SDK does not silently send invalid tokens or build broken endpoints from whitespace-polluted configuration.💚 How did you test it?
uv run --extra test pytest posthog/test/test_client.py -k "trims_api_key_whitespace or trims_host_and_personal_api_key_whitespace"uv run --extra test pytest posthog/test/test_request.py -k "routing_to_custom_host or defaults_blank_host_after_trimming_whitespace or trims_whitespace_sensitive_config"uv run ruff format --check .📝 Checklist
If releasing new changes
sampo addto generate a changeset filereleaselabel to the PR