Skip to content

fix: improve strict public API types#685

Open
marandaneto wants to merge 5 commits into
mainfrom
fix/strict-public-api-types-559
Open

fix: improve strict public API types#685
marandaneto wants to merge 5 commits into
mainfrom
fix/strict-public-api-types-559

Conversation

@marandaneto

@marandaneto marandaneto commented Jun 19, 2026

Copy link
Copy Markdown
Member

💡 Motivation and Context

Strict Pyright users could still hit partially unknown types when accessing public PostHog APIs, even after the issue-specific Posthog.shutdown path was fixed.

Fixes #559.

💚 How did you test it?

  • Compared the SDK event-facing types against the capture /batch/ backend contract in rust/capture/src/v0_request.rs and rust/common/types/src/event.rs. The SDK intentionally keeps some wrapper timestamp types stricter than the backend accepts.
  • python -m mypy --config-file mypy.ini posthog/client.py posthog/__init__.py
  • uv run ruff check posthog/client.py posthog/__init__.py
  • uv run --extra dev python .github/scripts/check_public_api.py
  • uv run --extra test pytest posthog/test/test_feature_flag.py posthog/test/test_feature_flag_result.py posthog/test/test_module.py::TestModule::test_flush
  • .github/scripts/check_strict_types.sh
  • Added a CI Strict type smoke job that runs .github/scripts/check_strict_types.sh. The script installs the package into a temporary downstream venv, runs strict Pyright against representative public API usage, and checks all public client/module function member access for unknown types.

📝 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

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

An AI coding agent investigated the strict typing gaps around #559, identified remaining Pyright Unknown types on public APIs, and made a typing-only fix. The changes focus on explicit public signatures in posthog.client.Client and the module-level wrappers in posthog.__init__, plus a patch changeset for release notes and a strict downstream Pyright smoke check in CI.

@marandaneto marandaneto self-assigned this Jun 19, 2026
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
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:62-67
`FlagValue` is imported without the `as FlagValue` explicit re-export form, so strict Pyright will still raise `reportPrivateImportUsage` when downstream users write `from posthog import FlagValue` to annotate variables holding the return value of `get_feature_flag()` or `get_all_flags()`. Every other intentionally public type in this block uses the `X as X` pattern (`BeforeSendCallback as BeforeSendCallback`, `FeatureFlag as FeatureFlag`); `FlagValue` should match.

```suggestion
from posthog.types import (
    BeforeSendCallback as BeforeSendCallback,
    FeatureFlag as FeatureFlag,
    FlagValue as FlagValue,
    FlagsAndPayloads,
)
```

Reviews (1): Last reviewed commit: "fix: improve strict public API types" | Re-trigger Greptile

Comment thread posthog/__init__.py
Comment thread posthog/__init__.py
Comment thread posthog/__init__.py
Comment thread posthog/__init__.py
Comment thread posthog/client.py Outdated
@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-19 14:10:23 UTC
Duration: 540099ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 10007ms
Format Validation.Event Has Lib Properties 10007ms
Format Validation.Distinct Id Is String 10007ms
Format Validation.Token Is Present 10007ms
Format Validation.Custom Properties Preserved 10007ms
Format Validation.Event Has Timestamp 10007ms
Retry Behavior.Retries On 503 18019ms
Retry Behavior.Does Not Retry On 400 12005ms
Retry Behavior.Does Not Retry On 401 10006ms
Retry Behavior.Respects Retry After Header 16014ms
Retry Behavior.Implements Backoff 32028ms
Retry Behavior.Retries On 500 16000ms
Retry Behavior.Retries On 502 16009ms
Retry Behavior.Retries On 504 16008ms
Retry Behavior.Max Retries Respected 32032ms
Deduplication.Generates Unique Uuids 9994ms
Deduplication.Preserves Uuid On Retry 16014ms
Deduplication.Preserves Uuid And Timestamp On Retry 23019ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16006ms
Deduplication.No Duplicate Events In Batch 10001ms
Deduplication.Different Events Have Different Uuids 10007ms
Compression.Sends Gzip When Enabled 10007ms
Batch Format.Uses Proper Batch Structure 10007ms
Batch Format.Flush With No Events Sends Nothing 5005ms
Batch Format.Multiple Events Batched Together 10005ms
Error Handling.Does Not Retry On 403 12010ms
Error Handling.Does Not Retry On 413 10006ms
Error Handling.Retries On 408 14014ms

Feature_Flags Tests

16/16 tests passed

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

@marandaneto marandaneto force-pushed the fix/strict-public-api-types-559 branch from 79c9d47 to 60c6a4f Compare June 19, 2026 13:40
@marandaneto marandaneto force-pushed the fix/strict-public-api-types-559 branch from 60c6a4f to 35f06ff Compare June 19, 2026 13:42
Comment thread posthog/client.py
Comment thread .github/workflows/ci.yml
Comment thread .github/workflows/ci.yml Outdated
Comment thread .github/workflows/ci.yml Outdated
Comment thread .github/workflows/ci.yml Outdated
@marandaneto marandaneto marked this pull request as ready for review June 19, 2026 14:03
@marandaneto marandaneto requested a review from a team as a code owner June 19, 2026 14:03
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
posthog/__init__.py:6
`ID_TYPES` is newly added to the public API snapshot by this PR and appears in public function signatures, but it is imported without the explicit `X as X` re-export form. Under strict Pyright with `reportPrivateImportUsage: error` (which the new smoke-test config enforces), `from posthog import ID_TYPES` would still fail. The PR fixed exactly this pattern for `FlagValue` (`FlagValue as FlagValue`) and `FeatureFlag` (`FeatureFlag as FeatureFlag`), so `ID_TYPES` should receive the same treatment. Note that the smoke-test only checks `from posthog import FlagValue, Posthog`, so this gap is not currently caught by CI.

```suggestion
from posthog.args import ID_TYPES as ID_TYPES, ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
```

### Issue 2 of 2
.github/scripts/check_strict_types.sh:10
`pyright` is installed at its latest released version on every CI run. A new pyright release with tighter checks (or changed defaults) can silently break the job or, conversely, loosen it. Pinning to a known-good version keeps the check stable and makes upgrades a deliberate action. The version can be reviewed and bumped periodically.

```suggestion
"$tmp/.venv/bin/python" -m pip install --quiet . "pyright==1.1.399"
```

Reviews (2): Last reviewed commit: "address pr review feedback" | Re-trigger Greptile

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.

Improve Python SDK typing for strict pyright/mypy projects

1 participant