Skip to content

fix: no-op global client for blank api key#632

Merged
marandaneto merged 3 commits into
mainfrom
fix/noop-empty-api-key
May 28, 2026
Merged

fix: no-op global client for blank api key#632
marandaneto merged 3 commits into
mainfrom
fix/noop-empty-api-key

Conversation

@marandaneto
Copy link
Copy Markdown
Member

@marandaneto marandaneto commented May 28, 2026

💡 Motivation and Context

Module-level setup should be safe when the global API key is unset or blank. Instead of raising during setup, blank API keys now create a disabled no-op global client so module-level calls are ignored consistently.

Changes:

  • Strip the module-level API key before creating the global client.
  • Treat missing, empty, and whitespace-only global API keys as a disabled no-op client.
  • Update the module-level API key/setup documentation to describe the no-op behavior in the return value docs.
  • Add parameterized coverage for unset, empty, and whitespace-only API keys.
  • Add a Sampo patch changeset for the release note.

💚 How did you test it?

  • .venv/bin/python -m pytest posthog/test/test_module.py -q

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

@marandaneto marandaneto requested a review from a team as a code owner May 28, 2026 10:30
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 28, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/__init__.py:1066-1072
The blank-key note is floating between the `Returns:` section and `Category:` as an unanchored paragraph, which looks out of place in Google-style docstrings and may render oddly in generated docs. Folding it into the `Returns:` description keeps all return-value semantics in one place.

```suggestion
    Returns:
        The global ``Client`` instance. If ``api_key`` is missing or blank,
        the client is disabled and module-level calls become no-ops.

    Category:
```

Reviews (1): Last reviewed commit: "chore: add changeset for blank api key n..." | Re-trigger Greptile

Comment thread posthog/__init__.py
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 28, 2026

posthog-python Compliance Report

Date: 2026-05-28 14:00:01 UTC
Duration: 176120ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 516ms
Format Validation.Event Has Uuid 1508ms
Format Validation.Event Has Lib Properties 1506ms
Format Validation.Distinct Id Is String 1508ms
Format Validation.Token Is Present 1506ms
Format Validation.Custom Properties Preserved 1507ms
Format Validation.Event Has Timestamp 1508ms
Retry Behavior.Retries On 503 9519ms
Retry Behavior.Does Not Retry On 400 3505ms
Retry Behavior.Does Not Retry On 401 3508ms
Retry Behavior.Respects Retry After Header 9514ms
Retry Behavior.Implements Backoff 23530ms
Retry Behavior.Retries On 500 7505ms
Retry Behavior.Retries On 502 7512ms
Retry Behavior.Retries On 504 7508ms
Retry Behavior.Max Retries Respected 23532ms
Deduplication.Generates Unique Uuids 1499ms
Deduplication.Preserves Uuid On Retry 7511ms
Deduplication.Preserves Uuid And Timestamp On Retry 14525ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7509ms
Deduplication.No Duplicate Events In Batch 1503ms
Deduplication.Different Events Have Different Uuids 1507ms
Compression.Sends Gzip When Enabled 1507ms
Batch Format.Uses Proper Batch Structure 1507ms
Batch Format.Flush With No Events Sends Nothing 1005ms
Batch Format.Multiple Events Batched Together 1506ms
Error Handling.Does Not Retry On 403 3509ms
Error Handling.Does Not Retry On 413 3508ms
Error Handling.Retries On 408 7514ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1003ms
Request Payload.Flags Request Uses V2 Query Param 1007ms
Request Payload.Flags Request Hits Flags Path Not Decide 1006ms
Request Payload.Flags Request Omits Authorization Header 1007ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1006ms
Request Payload.Groups Default To Empty Object 1007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 1007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1007ms
Request Payload.Disable Geoip Omitted Defaults To False 1006ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1007ms
Request Lifecycle.No Flags Request On Init Alone 503ms
Request Lifecycle.No Flags Request On Normal Capture 1507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1011ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1002ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1509ms

Comment thread posthog/__init__.py Outdated
@marandaneto marandaneto merged commit 034dce2 into main May 28, 2026
28 checks passed
@marandaneto marandaneto deleted the fix/noop-empty-api-key branch May 28, 2026 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants