Skip to content

Commit 89adb2f

Browse files
authored
fix: use relative sdk imports (#693)
* fix: use relative sdk imports * Add agent guidance * Add changeset * Use relative integration imports
1 parent 74eac3a commit 89adb2f

5 files changed

Lines changed: 64 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
pypi/posthog: patch
3+
---
4+
5+
Fix internal imports for posthoganalytics mirror

AGENTS.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# AGENTS.md
2+
3+
Guidance for coding agents working in `posthog-python`.
4+
5+
## Repo context
6+
7+
- This repository contains the PostHog Python SDK, published as `posthog`.
8+
- The main runtime package is `posthog/`; tests live under `posthog/test/`.
9+
- The project uses `uv` for local development. See `CONTRIBUTING.md` for setup.
10+
- Keep edits targeted and follow existing patterns. Prefer adding or updating tests near the behavior you change.
11+
12+
## Validation
13+
14+
Useful checks:
15+
16+
```bash
17+
uv run ruff format --check .
18+
uv run ruff check .
19+
uv run mypy --no-site-packages --config-file mypy.ini . | uv run mypy-baseline filter
20+
uv run pytest --verbose --timeout=30
21+
uv run python -W error -c "import posthog"
22+
```
23+
24+
For focused changes, run the smallest relevant `uv run pytest ...` command first.
25+
26+
If public API surface changes, update/check `references/public_api_snapshot.txt` with:
27+
28+
```bash
29+
make public_api_snapshot
30+
make public_api_check
31+
```
32+
33+
## `posthoganalytics` mirror package
34+
35+
This repo also publishes `posthoganalytics`, a generated mirror of `posthog` used by the PostHog app. The mirror is created by copying `posthog/` to `posthoganalytics/` and rewriting absolute imports such as `from posthog.foo import ...` to `from posthoganalytics.foo import ...`.
36+
37+
Important when editing SDK-internal code:
38+
39+
- Prefer relative imports for imports within the SDK package, especially in runtime modules under `posthog/`.
40+
- Good: `from .client import Client`, `from .exception_utils import extract_exception_properties`
41+
- Risky: `from posthog.client import Client`
42+
- Absolute `posthog...` imports inside SDK modules can break the `posthoganalytics` mirror when it is imported inside an application that also has its own `posthog` package/module on `sys.path`.
43+
- Test mirror-sensitive changes by running the normal focused tests and, when relevant, `make prep_local` to generate a local `posthoganalytics` copy for testing in the PostHog app.
44+
- Do not commit generated `posthoganalytics/` directories; they are build/local artifacts.
45+
46+
## Release/build notes
47+
48+
- `make build_release` builds the `posthog` distribution.
49+
- `make build_release_analytics` builds the `posthoganalytics` distribution and temporarily rewrites/copies package files; ensure the working tree is clean before and after running it.
50+
- Release flow publishes both packages; see `RELEASING.md`.

posthog/contexts.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,14 @@ def _default_capture_exceptions(client: Optional["Client"] = None) -> bool:
138138
if client is not None:
139139
return client.enable_exception_autocapture
140140

141-
import posthog
141+
from . import default_client, enable_exception_autocapture
142142

143-
default_client = getattr(posthog, "default_client", None)
144143
if default_client is not None:
145144
client_default = getattr(default_client, "enable_exception_autocapture", None)
146145
if isinstance(client_default, bool):
147146
return client_default
148147

149-
return posthog.enable_exception_autocapture
148+
return enable_exception_autocapture
150149

151150

152151
@contextmanager
@@ -197,7 +196,7 @@ def new_context(
197196
Category:
198197
Contexts
199198
"""
200-
from posthog import capture_exception
199+
from . import capture_exception
201200

202201
current_context = _get_current_context()
203202
resolved_capture_exceptions = (

posthog/integrations/celery.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
import time
7070
from typing import Any, Callable, Optional
7171

72-
from posthog import contexts
73-
from posthog.client import Client
72+
from .. import contexts
73+
from ..client import Client
7474

7575

7676
CONTEXT_DISTINCT_ID_HEADER = "X-POSTHOG-DISTINCT-ID"
@@ -198,9 +198,9 @@ def shutdown(self) -> None:
198198
if self.client:
199199
self.client.flush()
200200
else:
201-
import posthog
201+
from .. import flush
202202

203-
posthog.flush()
203+
flush()
204204

205205
self.uninstrument()
206206
self._shut_down = True

posthog/integrations/django.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import re
22
from typing import TYPE_CHECKING, Optional, cast
33

4-
from posthog import contexts
5-
from posthog.client import Client
4+
from .. import contexts
5+
from ..client import Client
66

77
try:
88
from asgiref.sync import iscoroutinefunction, markcoroutinefunction

0 commit comments

Comments
 (0)