Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:

- name: Check public API snapshot
run: |
UV_PROJECT_ENVIRONMENT=$pythonLocation uv run --extra dev --with pytest --with pytest-timeout python .github/scripts/test_check_public_api.py
UV_PROJECT_ENVIRONMENT=$pythonLocation uv run --extra dev --with pytest --with pytest-timeout --with pytest-asyncio python .github/scripts/test_check_public_api.py
make public_api_check

strict-type-smoke:
Expand Down
5 changes: 5 additions & 0 deletions .sampo/changesets/standard-build-tooling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Stop shipping the SDK test suite in the `posthog` and `posthoganalytics` wheels, build releases with `uv build` instead of the deprecated `setup.py` commands, and use `inspect.iscoroutinefunction` in the Django middleware fallback for environments without `asgiref`.
11 changes: 4 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public_api_check:

build_release:
rm -rf dist/*
python setup.py sdist bdist_wheel
uv build --out-dir dist

# Builds the `posthoganalytics` PyPI package, which is a mirror of `posthog`
# published under a different name for internal use by posthog/posthog.
Expand All @@ -40,7 +40,8 @@ build_release_analytics:
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthog\./from posthoganalytics\./g' {} \;
find ./posthoganalytics -name "*.bak" -delete
rm -rf posthog
python setup_analytics.py sdist bdist_wheel
python setup_analytics.py
uv build --out-dir dist
mkdir posthog
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthoganalytics /from posthog /g' {} \;
find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthoganalytics\./from posthog\./g' {} \;
Expand All @@ -65,11 +66,7 @@ prep_local:
cd ../posthog-python-local && find ./posthoganalytics -type f -name "*.py" -exec sed -i.bak -e 's/from posthog\./from posthoganalytics\./g' {} \;
cd ../posthog-python-local && find ./posthoganalytics -name "*.bak" -delete
cd ../posthog-python-local && rm -rf posthog
cd ../posthog-python-local && sed -i.bak 's/from version import VERSION/from posthoganalytics.version import VERSION/' setup_analytics.py
cd ../posthog-python-local && rm setup_analytics.py.bak
cd ../posthog-python-local && sed -i.bak 's/"posthog"/"posthoganalytics"/' setup.py
cd ../posthog-python-local && rm setup.py.bak
cd ../posthog-python-local && python -c "import setup_analytics" 2>/dev/null || true
cd ../posthog-python-local && python setup_analytics.py && rm pyproject.toml.backup
@echo "Local copy created at ../posthog-python-local"
@echo "Install with: pip install -e ../posthog-python-local"

Expand Down
8 changes: 8 additions & 0 deletions benchmarks/test_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tempfile
import time
import unittest
import warnings
from pathlib import Path
from typing import Any
from unittest.mock import patch
Expand Down Expand Up @@ -35,6 +36,13 @@
PYTHON = ROOT / ".venv-benchmark/bin/python"


def setUpModule():
# The suite measures the legacy single-flag getter on purpose.
warnings.filterwarnings(
"ignore", r"`get_feature_flag` is deprecated", DeprecationWarning
)


class BenchmarksTest(unittest.TestCase):
def test_specification_inventory_and_parameters(self):
single_cases = (
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, spec, host, client_class=None):
feature_flags_request_max_retries=0,
thread=1,
on_error=lambda error, _batch: self.errors.append(str(error)),
**({"personal_api_key": "benchmark-local-only"} if self.flags else {}),
**({"secret_key": "benchmark-local-only"} if self.flags else {}),
)
self.pool = (
ThreadPoolExecutor(max_workers=spec["concurrency"])
Expand Down
15 changes: 13 additions & 2 deletions posthog/integrations/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@
from asgiref.sync import iscoroutinefunction, markcoroutinefunction
except ImportError:
# Fallback for older Django versions without asgiref
import asyncio
import asyncio.coroutines
import inspect

iscoroutinefunction = asyncio.iscoroutinefunction
# Same check as the deprecated asyncio.iscoroutinefunction, which also
# accepts callables marked with `_is_coroutine` by older asgiref versions.
_legacy_coroutine_marker = getattr(asyncio.coroutines, "_is_coroutine", object())

def _iscoroutinefunction(func):
return (
inspect.iscoroutinefunction(func)
or getattr(func, "_is_coroutine", None) is _legacy_coroutine_marker
)

iscoroutinefunction = _iscoroutinefunction

# No-op fallback for markcoroutinefunction
# Older Django versions without asgiref typically don't support async middleware anyway
Expand Down
50 changes: 50 additions & 0 deletions posthog/test/integrations/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
get_tags,
)
import unittest
from contextlib import contextmanager
from unittest.mock import Mock, patch
import asyncio
from parameterized import parameterized
Expand Down Expand Up @@ -868,5 +869,54 @@ def sync_get_response(request):
self.assertEqual(result, mock_response)


class TestCoroutineDetectionWithoutAsgiref(unittest.TestCase):
@contextmanager
def _load_fallback(self):
import builtins
import importlib
import warnings

import posthog.integrations.django as module

real_import = builtins.__import__

def import_without_asgiref(name, *args, **kwargs):
if name.startswith("asgiref"):
raise ImportError(name)
return real_import(name, *args, **kwargs)

self.addCleanup(importlib.reload, module)
# Python 3.14 warns when asyncio.iscoroutinefunction is called, not when
# it is imported, so the filter must stay active during the calls.
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
with patch("builtins.__import__", side_effect=import_without_asgiref):
iscoroutinefunction = importlib.reload(module).iscoroutinefunction
yield iscoroutinefunction

def test_detects_async_functions(self):
async def view(request):
return request

with self._load_fallback() as iscoroutinefunction:
self.assertTrue(iscoroutinefunction(view))
self.assertFalse(iscoroutinefunction(lambda request: request))

def test_detects_callables_marked_by_legacy_asgiref(self):
marker = getattr(asyncio.coroutines, "_is_coroutine", None)
if marker is None:
self.skipTest("this Python has no legacy coroutine marker")

class Handler:
def __call__(self, request):
return request

handler = Handler()
handler._is_coroutine = marker

with self._load_fallback() as iscoroutinefunction:
self.assertTrue(iscoroutinefunction(handler))


if __name__ == "__main__":
unittest.main()
10 changes: 10 additions & 0 deletions posthog/test/test_async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import unittest
from unittest import mock

import pytest

from posthog._async_utils import _BackgroundEventLoopRunner, _LoopStartup


Expand Down Expand Up @@ -227,6 +229,10 @@ def run():
self.assertEqual(len(run_errors), 1)
self.assertIsNone(awaitable.cr_frame)

# Python 3.14 deprecates the event loop policy API exercised here.
@pytest.mark.filterwarnings(
r"ignore:'asyncio\.(get_event_loop_policy|set_event_loop_policy|DefaultEventLoopPolicy)' is deprecated:DeprecationWarning"
)
def test_runner_preserves_configured_event_loop_policy(self):
runner = _BackgroundEventLoopRunner()
original_policy = asyncio.get_event_loop_policy()
Expand All @@ -250,6 +256,10 @@ async def running_loop():
finally:
asyncio.set_event_loop_policy(original_policy)

# Python 3.14 deprecates the event loop policy API exercised here.
@pytest.mark.filterwarnings(
r"ignore:'asyncio\.(get_event_loop_policy|set_event_loop_policy|DefaultEventLoopPolicy)' is deprecated:DeprecationWarning"
)
def test_runner_uses_context_aware_fallback_for_read_only_policy_loop(self):
runner = _BackgroundEventLoopRunner()
original_policy = asyncio.get_event_loop_policy()
Expand Down
1 change: 1 addition & 0 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3320,6 +3320,7 @@ def reenter_join():
self.assertTrue(loop.is_closed())
self.assertTrue(client._join_cleanup_complete)

@pytest.mark.filterwarnings("ignore:This process .* is multi-threaded")
def test_async_cache_provider_process_executor_remains_supported(self):
client = Client(FAKE_TEST_API_KEY, flush_interval=0.01)

Expand Down
3 changes: 3 additions & 0 deletions posthog/test/test_contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import unittest
from unittest.mock import MagicMock, patch

import pytest

import posthog
from posthog.async_client import AsyncPosthog
from posthog.client import Client
Expand Down Expand Up @@ -301,6 +303,7 @@ def test_context_inheritance_non_fresh_context(self):
hasattr(os, "fork") and hasattr(os, "register_at_fork"),
"requires os.fork and os.register_at_fork",
)
@pytest.mark.filterwarnings("ignore:This process .* is multi-threaded")
def test_fork_clears_context_in_child_and_preserves_parent(self):
def context_state():
return (
Expand Down
11 changes: 11 additions & 0 deletions posthog/test/test_failed_flag_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
from posthog.client import Client
from posthog.types import normalize_flags_response

# This module covers the legacy single-flag getters; warning emission itself is
# asserted in test_evaluate_flags.py.
pytestmark = [
pytest.mark.filterwarnings(
r"ignore:`(feature_enabled|get_feature_flag|get_feature_flag_payload)` is deprecated:DeprecationWarning"
),
pytest.mark.filterwarnings(
r"ignore:send_feature_flag_events is deprecated in get_feature_flag_payload:DeprecationWarning"
),
]


@pytest.fixture
def client():
Expand Down
11 changes: 11 additions & 0 deletions posthog/test/test_flag_cache_publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
from posthog.test.test_utils import FAKE_TEST_API_KEY, FakeRedis
from posthog.utils import FlagCache, FlagCacheEntry, RedisFlagCache

# This module covers the legacy single-flag getters; warning emission itself is
# asserted in test_evaluate_flags.py.
pytestmark = [
pytest.mark.filterwarnings(
r"ignore:`(feature_enabled|get_feature_flag|get_feature_flag_payload)` is deprecated:DeprecationWarning"
),
pytest.mark.filterwarnings(
r"ignore:send_feature_flag_events is deprecated in get_feature_flag_payload:DeprecationWarning"
),
]


@pytest.fixture(params=["memory", "redis"])
def client(request):
Expand Down
11 changes: 11 additions & 0 deletions posthog/test/test_flags_v4_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
to_flags_and_payloads,
)

# This module covers the legacy single-flag getters; warning emission itself is
# asserted in test_evaluate_flags.py.
pytestmark = [
pytest.mark.filterwarnings(
r"ignore:`(feature_enabled|get_feature_flag|get_feature_flag_payload)` is deprecated:DeprecationWarning"
),
pytest.mark.filterwarnings(
r"ignore:send_feature_flag_events is deprecated in get_feature_flag_payload:DeprecationWarning"
),
]

FIXTURES = Path(__file__).with_name("fixtures") / "feature_flag_rules_v2"
DISTINCT_ID = "user-1"

Expand Down
11 changes: 11 additions & 0 deletions posthog/test/test_property_matching_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
)
from posthog.test.test_utils import FAKE_TEST_API_KEY

# This module covers the legacy single-flag getters; warning emission itself is
# asserted in test_evaluate_flags.py.
pytestmark = [
pytest.mark.filterwarnings(
r"ignore:`(feature_enabled|get_feature_flag|get_feature_flag_payload)` is deprecated:DeprecationWarning"
),
pytest.mark.filterwarnings(
r"ignore:send_feature_flag_events is deprecated in get_feature_flag_payload:DeprecationWarning"
),
]


ROWS = [
(False, "banana", True, False),
Expand Down
20 changes: 11 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ build-backend = "setuptools.build_meta"
name = "posthog"
version = "8.2.1"
description = "Integrate PostHog into any python application."
authors = [{ name = "PostHog", email = "engineering@posthog.com" }]
maintainers = [{ name = "PostHog", email = "engineering@posthog.com" }]
authors = [
{ name = "Posthog" },
{ name = "PostHog", email = "engineering@posthog.com" },
]
maintainers = [
{ name = "PostHog" },
{ name = "PostHog", email = "engineering@posthog.com" },
]
license = "MIT"
readme = "README.md"
requires-python = ">=3.10"
Expand Down Expand Up @@ -144,16 +150,12 @@ packages = [
"posthog.ai.otel",
"posthog.mcp",
"posthog.tracing",
"posthog.test",
"posthog.test.ai",
"posthog.test.ai.openai_agents",
"posthog.test.ai.claude_agent_sdk",
"posthog.test.ai.otel",
"posthog.test.mcp",
"posthog.test.tracing",
"posthog.integrations",
]

[tool.setuptools.exclude-package-data]
posthog = ["test/**"]

[tool.uv]
exclude-newer = "7 days"
# certifi 2026.10.19 drops the compromised eMudhra root and is newer than the 7-day cooldown.
Expand Down
2 changes: 1 addition & 1 deletion sdk_compliance_adapter/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /app

# Copy the SDK source code
COPY posthog/ /app/sdk/posthog/
COPY setup.py pyproject.toml README.md LICENSE /app/sdk/
COPY pyproject.toml README.md LICENSE /app/sdk/

# Install the SDK from source, with zstd for the zstd compliance job
RUN cd /app/sdk && pip install --no-cache-dir -e ".[zstd]"
Expand Down
5 changes: 5 additions & 0 deletions sdk_compliance_adapter/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import posthog.request
from posthog.request import APIError, GetResponse

# The harness protocol exposes the legacy single-flag getters.
pytestmark = pytest.mark.filterwarnings(
r"ignore:`(get_feature_flag|get_feature_flag_payload)` is deprecated:DeprecationWarning"
)


@pytest.fixture
def adapter(monkeypatch):
Expand Down
35 changes: 0 additions & 35 deletions setup.py

This file was deleted.

Loading
Loading