Skip to content

fix: use relative sdk imports#693

Merged
hpouillot merged 4 commits into
mainfrom
hp/fix-posthoganalytics-relative-imports
Jun 24, 2026
Merged

fix: use relative sdk imports#693
hpouillot merged 4 commits into
mainfrom
hp/fix-posthoganalytics-relative-imports

Conversation

@hpouillot

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

The posthoganalytics package is built as a mirror of the posthog SDK package. A few SDK-internal runtime imports still used absolute posthog imports, which can resolve to a host application's local posthog package instead of the SDK mirror.

This changes those self-imports to relative imports so both the posthog and posthoganalytics packages resolve their own module state correctly.

💚 How did you test it?

uv run pytest posthog/test/test_contexts.py posthog/test/integrations/test_celery_integration.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

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

This PR was prepared with the pi coding agent. The issue was identified while testing a dependency upgrade in the PostHog monorepo: posthoganalytics.new_context() could import a host application's posthog package through an SDK-internal absolute import. The chosen fix is to keep SDK self-references relative instead of adding compatibility globals to the host application.

@hpouillot hpouillot requested a review from a team as a code owner June 24, 2026 13:19
@hpouillot hpouillot marked this pull request as draft June 24, 2026 13:20
@hpouillot hpouillot force-pushed the hp/fix-posthoganalytics-relative-imports branch from 3954f42 to 36e33fb Compare June 24, 2026 13:22
@hpouillot hpouillot marked this pull request as ready for review June 24, 2026 13:24
@greptile-apps

greptile-apps Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff (1)

  1. posthog/integrations/celery.py, line 72 (link)

    P2 Remaining absolute import not converted

    This file was already modified to fix posthog.flush()from .. import flush, but from posthog import contexts on this same line wasn't converted. The same mirror-resolution risk applies here: if an application's own posthog package is on sys.path, this import can resolve to the wrong module. Based on the AGENTS.md guidance added in this PR and the same pattern fixed elsewhere, this should also be a relative import: from .. import contexts.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "Add changeset" | Re-trigger Greptile

@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-24 13:37:53 UTC
Duration: 530093ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 518ms
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 18018ms
Retry Behavior.Does Not Retry On 400 12005ms
Retry Behavior.Does Not Retry On 401 10007ms
Retry Behavior.Respects Retry After Header 16014ms
Retry Behavior.Implements Backoff 30005ms
Retry Behavior.Retries On 500 13024ms
Retry Behavior.Retries On 502 16011ms
Retry Behavior.Retries On 504 16010ms
Retry Behavior.Max Retries Respected 30018ms
Deduplication.Generates Unique Uuids 7002ms
Deduplication.Preserves Uuid On Retry 16015ms
Deduplication.Preserves Uuid And Timestamp On Retry 23020ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16005ms
Deduplication.No Duplicate Events In Batch 10003ms
Deduplication.Different Events Have Different Uuids 10006ms
Compression.Sends Gzip When Enabled 10008ms
Batch Format.Uses Proper Batch Structure 10006ms
Batch Format.Flush With No Events Sends Nothing 5005ms
Batch Format.Multiple Events Batched Together 10006ms
Error Handling.Does Not Retry On 403 12009ms
Error Handling.Does Not Retry On 413 10005ms
Error Handling.Retries On 408 14016ms

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 10006ms
Request Payload.Groups Round Trip 10008ms
Request Payload.Groups Default To Empty Object 10006ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10007ms
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 10507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9511ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10003ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10509ms

@hpouillot hpouillot merged commit 89adb2f into main Jun 24, 2026
31 checks passed
@hpouillot hpouillot deleted the hp/fix-posthoganalytics-relative-imports branch June 24, 2026 15:46
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