From c99fb4f5f67eb12982d479ee1a8efc8a2a219813 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 24 Nov 2026 10:31:21 +0100 Subject: [PATCH 1/8] chore: build with uv, drop tests from wheels, and clean up CI warnings --- .github/workflows/ci.yml | 2 +- .sampo/changesets/standard-build-tooling.md | 5 ++ Makefile | 11 ++-- benchmarks/test_benchmarks.py | 8 +++ benchmarks/workload.py | 2 +- posthog/integrations/django.py | 4 +- posthog/test/test_async_utils.py | 10 +++ posthog/test/test_client.py | 1 + posthog/test/test_contexts.py | 3 + posthog/test/test_failed_flag_records.py | 11 ++++ posthog/test/test_flag_cache_publication.py | 11 ++++ posthog/test/test_flags_v4_response.py | 11 ++++ .../test/test_property_matching_version.py | 11 ++++ pyproject.toml | 10 +-- sdk_compliance_adapter/Dockerfile | 2 +- sdk_compliance_adapter/test_adapter.py | 5 ++ setup.py | 35 ---------- setup_analytics.py | 66 ++++--------------- 18 files changed, 101 insertions(+), 107 deletions(-) create mode 100644 .sampo/changesets/standard-build-tooling.md delete mode 100644 setup.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 55055eb4c..7a340d159 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/.sampo/changesets/standard-build-tooling.md b/.sampo/changesets/standard-build-tooling.md new file mode 100644 index 000000000..3b2e9f0e4 --- /dev/null +++ b/.sampo/changesets/standard-build-tooling.md @@ -0,0 +1,5 @@ +--- +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`. diff --git a/Makefile b/Makefile index 53cc8bb05..123df0b8e 100644 --- a/Makefile +++ b/Makefile @@ -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. @@ -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' {} \; @@ -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" diff --git a/benchmarks/test_benchmarks.py b/benchmarks/test_benchmarks.py index dcfd89b46..23f20fc85 100644 --- a/benchmarks/test_benchmarks.py +++ b/benchmarks/test_benchmarks.py @@ -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 @@ -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 = ( diff --git a/benchmarks/workload.py b/benchmarks/workload.py index c8411f41d..197dac0ce 100644 --- a/benchmarks/workload.py +++ b/benchmarks/workload.py @@ -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"]) diff --git a/posthog/integrations/django.py b/posthog/integrations/django.py index a49527181..ab8de01ae 100644 --- a/posthog/integrations/django.py +++ b/posthog/integrations/django.py @@ -13,9 +13,7 @@ from asgiref.sync import iscoroutinefunction, markcoroutinefunction except ImportError: # Fallback for older Django versions without asgiref - import asyncio - - iscoroutinefunction = asyncio.iscoroutinefunction + from inspect import iscoroutinefunction # No-op fallback for markcoroutinefunction # Older Django versions without asgiref typically don't support async middleware anyway diff --git a/posthog/test/test_async_utils.py b/posthog/test/test_async_utils.py index 1eb7c7837..5f3b3ea74 100644 --- a/posthog/test/test_async_utils.py +++ b/posthog/test/test_async_utils.py @@ -4,6 +4,8 @@ import unittest from unittest import mock +import pytest + from posthog._async_utils import _BackgroundEventLoopRunner, _LoopStartup @@ -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\.\w+' is deprecated:DeprecationWarning" + ) def test_runner_preserves_configured_event_loop_policy(self): runner = _BackgroundEventLoopRunner() original_policy = asyncio.get_event_loop_policy() @@ -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\.\w+' 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() diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index 5381af3db..d76988809 100644 --- a/posthog/test/test_client.py +++ b/posthog/test/test_client.py @@ -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) diff --git a/posthog/test/test_contexts.py b/posthog/test/test_contexts.py index 7efc7e0fc..8887e8a33 100644 --- a/posthog/test/test_contexts.py +++ b/posthog/test/test_contexts.py @@ -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 @@ -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 ( diff --git a/posthog/test/test_failed_flag_records.py b/posthog/test/test_failed_flag_records.py index f17c6ede2..986081bce 100644 --- a/posthog/test/test_failed_flag_records.py +++ b/posthog/test/test_failed_flag_records.py @@ -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(): diff --git a/posthog/test/test_flag_cache_publication.py b/posthog/test/test_flag_cache_publication.py index 69006378d..c28db5472 100644 --- a/posthog/test/test_flag_cache_publication.py +++ b/posthog/test/test_flag_cache_publication.py @@ -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): diff --git a/posthog/test/test_flags_v4_response.py b/posthog/test/test_flags_v4_response.py index ee5c367bf..a650a943b 100644 --- a/posthog/test/test_flags_v4_response.py +++ b/posthog/test/test_flags_v4_response.py @@ -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" diff --git a/posthog/test/test_property_matching_version.py b/posthog/test/test_property_matching_version.py index f6f1f8774..aab486313 100644 --- a/posthog/test/test_property_matching_version.py +++ b/posthog/test/test_property_matching_version.py @@ -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), diff --git a/pyproject.toml b/pyproject.toml index e00d1779d..d77c05d06 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -144,16 +144,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. diff --git a/sdk_compliance_adapter/Dockerfile b/sdk_compliance_adapter/Dockerfile index bfc691dd3..c214e36a9 100644 --- a/sdk_compliance_adapter/Dockerfile +++ b/sdk_compliance_adapter/Dockerfile @@ -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]" diff --git a/sdk_compliance_adapter/test_adapter.py b/sdk_compliance_adapter/test_adapter.py index 6c12e86ee..a6bc83060 100644 --- a/sdk_compliance_adapter/test_adapter.py +++ b/sdk_compliance_adapter/test_adapter.py @@ -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): diff --git a/setup.py b/setup.py deleted file mode 100644 index 4283f5ce0..000000000 --- a/setup.py +++ /dev/null @@ -1,35 +0,0 @@ -import os -import sys - -try: - from setuptools import setup -except ImportError: - from distutils.core import setup - -# Don't import analytics-python module here, since deps may not be installed -sys.path.insert(0, os.path.join(os.path.dirname(__file__), "posthog")) -from version import VERSION # noqa: E402 - -long_description = """ -PostHog is developer-friendly, self-hosted product analytics. -posthog-python is the python package. - -This package requires Python 3.10 or higher. -""" - -# Minimal setup.py for backward compatibility -# Most configuration is now in pyproject.toml -setup( - name="posthog", - version=VERSION, - # Basic fields for backward compatibility - url="https://github.com/posthog/posthog-python", - author="Posthog", - author_email="engineering@posthog.com", - maintainer="PostHog", - maintainer_email="engineering@posthog.com", - license="MIT License", - description="Integrate PostHog into any python application.", - long_description=long_description, - # This will fallback to pyproject.toml for detailed configuration -) diff --git a/setup_analytics.py b/setup_analytics.py index 6cee7a720..a5554c402 100644 --- a/setup_analytics.py +++ b/setup_analytics.py @@ -1,66 +1,28 @@ -import os -import sys -import tomli -import tomli_w -import shutil +"""Rewrite pyproject.toml so the build produces the `posthoganalytics` mirror. -try: - from setuptools import setup -except ImportError: - from distutils.core import setup +Run by `make build_release_analytics` and `make prep_local` after the source has +been copied to `posthoganalytics/`. The original is kept in pyproject.toml.backup. +""" -# Don't import analytics-python module here, since deps may not be installed -sys.path.insert(0, os.path.join(os.path.dirname(__file__), "posthoganalytics")) -from version import VERSION # noqa: E402 +import shutil +import tomli +import tomli_w -# Copy the original pyproject.toml as backup shutil.copy("pyproject.toml", "pyproject.toml.backup") -# Read the original pyproject.toml with open("pyproject.toml", "rb") as f: config = tomli.load(f) -# Override specific values config["project"]["name"] = "posthoganalytics" config["project"]["readme"] = "README_ANALYTICS.md" +config["tool"]["setuptools"]["packages"] = [ + "posthoganalytics" + package.removeprefix("posthog") + for package in config["tool"]["setuptools"]["packages"] +] +config["tool"]["setuptools"]["exclude-package-data"] = { + "posthoganalytics": config["tool"]["setuptools"]["exclude-package-data"]["posthog"] +} -# Rename packages from posthog.* to posthoganalytics.* -if "packages" in config["tool"]["setuptools"]: - new_packages = [] - for package in config["tool"]["setuptools"]["packages"]: - if package == "posthog": - new_packages.append("posthoganalytics") - elif package.startswith("posthog."): - new_packages.append(package.replace("posthog.", "posthoganalytics.", 1)) - else: - new_packages.append(package) - config["tool"]["setuptools"]["packages"] = new_packages - -# Overwrite the original pyproject.toml with open("pyproject.toml", "wb") as f: tomli_w.dump(config, f) - -long_description = """ -PostHog is developer-friendly, self-hosted product analytics. -posthog-python is the python package. - -This package requires Python 3.10 or higher. -""" - -# Minimal setup.py for backward compatibility -# Most configuration is now in pyproject.toml -setup( - name="posthoganalytics", - version=VERSION, - # Basic fields for backward compatibility - url="https://github.com/posthog/posthog-python", - author="Posthog", - author_email="hey@posthog.com", - maintainer="PostHog", - maintainer_email="hey@posthog.com", - license="MIT License", - description="Integrate PostHog into any python application.", - long_description=long_description, - # This will fallback to pyproject.toml for detailed configuration -) From 2ce627dce0e847fe44207fb86214c474b2ff1f0e Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 24 Nov 2026 10:34:12 +0100 Subject: [PATCH 2/8] fix: keep legacy coroutine detection in the Django fallback before Python 3.12 --- posthog/integrations/django.py | 12 ++++- posthog/test/integrations/test_middleware.py | 48 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/posthog/integrations/django.py b/posthog/integrations/django.py index ab8de01ae..265d3333c 100644 --- a/posthog/integrations/django.py +++ b/posthog/integrations/django.py @@ -12,8 +12,16 @@ try: from asgiref.sync import iscoroutinefunction, markcoroutinefunction except ImportError: - # Fallback for older Django versions without asgiref - from inspect import iscoroutinefunction + # Fallback for older Django versions without asgiref. Before Python 3.12, + # only asyncio's check recognizes callables marked with `_is_coroutine`. + import inspect + + if hasattr(inspect, "markcoroutinefunction"): + iscoroutinefunction = inspect.iscoroutinefunction + else: + import asyncio + + iscoroutinefunction = asyncio.iscoroutinefunction # type: ignore[assignment] # No-op fallback for markcoroutinefunction # Older Django versions without asgiref typically don't support async middleware anyway diff --git a/posthog/test/integrations/test_middleware.py b/posthog/test/integrations/test_middleware.py index 336d7e017..466c8812c 100644 --- a/posthog/test/integrations/test_middleware.py +++ b/posthog/test/integrations/test_middleware.py @@ -868,5 +868,53 @@ def sync_get_response(request): self.assertEqual(result, mock_response) +class TestCoroutineDetectionWithoutAsgiref(unittest.TestCase): + 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) + with ( + patch("builtins.__import__", side_effect=import_without_asgiref), + warnings.catch_warnings(), + ): + warnings.simplefilter("error", DeprecationWarning) + return importlib.reload(module).iscoroutinefunction + + def test_detects_async_functions(self): + iscoroutinefunction = self._load_fallback() + + async def view(request): + return request + + self.assertTrue(iscoroutinefunction(view)) + self.assertFalse(iscoroutinefunction(lambda request: request)) + + def test_detects_callables_marked_by_legacy_asgiref(self): + import inspect + + if hasattr(inspect, "markcoroutinefunction"): + self.skipTest("Python 3.12+ marks coroutine callables through inspect") + + class Handler: + def __call__(self, request): + return request + + handler = Handler() + handler._is_coroutine = asyncio.coroutines._is_coroutine + + self.assertTrue(self._load_fallback()(handler)) + + if __name__ == "__main__": unittest.main() From 3c8f64d08d879b94354eb93dfab37502dc4b1c8d Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 24 Nov 2026 10:35:33 +0100 Subject: [PATCH 3/8] fix: detect legacy coroutine markers in the Django fallback on all Python versions --- posthog/integrations/django.py | 17 ++++++++++------- posthog/test/integrations/test_middleware.py | 9 ++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/posthog/integrations/django.py b/posthog/integrations/django.py index 265d3333c..3ccf0b9ce 100644 --- a/posthog/integrations/django.py +++ b/posthog/integrations/django.py @@ -12,16 +12,19 @@ try: from asgiref.sync import iscoroutinefunction, markcoroutinefunction except ImportError: - # Fallback for older Django versions without asgiref. Before Python 3.12, - # only asyncio's check recognizes callables marked with `_is_coroutine`. + # Fallback for older Django versions without asgiref + import asyncio.coroutines import inspect - if hasattr(inspect, "markcoroutinefunction"): - iscoroutinefunction = inspect.iscoroutinefunction - else: - import asyncio + # 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()) - iscoroutinefunction = asyncio.iscoroutinefunction # type: ignore[assignment] + def iscoroutinefunction(func): + return ( + inspect.iscoroutinefunction(func) + or getattr(func, "_is_coroutine", None) is _legacy_coroutine_marker + ) # No-op fallback for markcoroutinefunction # Older Django versions without asgiref typically don't support async middleware anyway diff --git a/posthog/test/integrations/test_middleware.py b/posthog/test/integrations/test_middleware.py index 466c8812c..a446916f5 100644 --- a/posthog/test/integrations/test_middleware.py +++ b/posthog/test/integrations/test_middleware.py @@ -901,17 +901,16 @@ async def view(request): self.assertFalse(iscoroutinefunction(lambda request: request)) def test_detects_callables_marked_by_legacy_asgiref(self): - import inspect - - if hasattr(inspect, "markcoroutinefunction"): - self.skipTest("Python 3.12+ marks coroutine callables through inspect") + 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 = asyncio.coroutines._is_coroutine + handler._is_coroutine = marker self.assertTrue(self._load_fallback()(handler)) From 13de230ca80e19336edf86c37e0b9d28fda6750d Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 24 Nov 2026 10:39:06 +0100 Subject: [PATCH 4/8] fix: keep the Django fallback coroutine check out of the public API snapshot --- posthog/integrations/django.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/posthog/integrations/django.py b/posthog/integrations/django.py index 3ccf0b9ce..1ed38191f 100644 --- a/posthog/integrations/django.py +++ b/posthog/integrations/django.py @@ -20,12 +20,14 @@ # accepts callables marked with `_is_coroutine` by older asgiref versions. _legacy_coroutine_marker = getattr(asyncio.coroutines, "_is_coroutine", object()) - def iscoroutinefunction(func): + 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 def markcoroutinefunction(func): From e32120fb6e46e5c4d169a0ff072c964ddb88b655 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 24 Nov 2026 14:04:19 +0100 Subject: [PATCH 5/8] test: limit the event loop policy warning filter to the APIs the tests use --- posthog/test/test_async_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posthog/test/test_async_utils.py b/posthog/test/test_async_utils.py index 5f3b3ea74..46d46210a 100644 --- a/posthog/test/test_async_utils.py +++ b/posthog/test/test_async_utils.py @@ -231,7 +231,7 @@ def run(): # Python 3.14 deprecates the event loop policy API exercised here. @pytest.mark.filterwarnings( - r"ignore:'asyncio\.\w+' is deprecated:DeprecationWarning" + 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() @@ -258,7 +258,7 @@ async def running_loop(): # Python 3.14 deprecates the event loop policy API exercised here. @pytest.mark.filterwarnings( - r"ignore:'asyncio\.\w+' is deprecated:DeprecationWarning" + 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() From 4b628d33b7d1100e07e7e001ef8025789dcb75a1 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 24 Nov 2026 14:25:12 +0100 Subject: [PATCH 6/8] chore: use the pypi package prefix in the changeset --- .sampo/changesets/standard-build-tooling.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.sampo/changesets/standard-build-tooling.md b/.sampo/changesets/standard-build-tooling.md index 3b2e9f0e4..982c02810 100644 --- a/.sampo/changesets/standard-build-tooling.md +++ b/.sampo/changesets/standard-build-tooling.md @@ -1,5 +1,5 @@ --- -posthog: patch +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`. From 02ab02a997d037a9f9509529130768c265813548 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 24 Nov 2026 14:43:17 +0100 Subject: [PATCH 7/8] chore: keep the author and maintainer names in package metadata --- pyproject.toml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d77c05d06..7895aa308 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" From be807e112d34de367a3954615575c7126de5b06e Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 25 Nov 2026 10:29:03 +0100 Subject: [PATCH 8/8] test: keep deprecation warnings as errors while calling the Django fallback --- posthog/test/integrations/test_middleware.py | 23 +++++++++++--------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/posthog/test/integrations/test_middleware.py b/posthog/test/integrations/test_middleware.py index a446916f5..3cc3b4c01 100644 --- a/posthog/test/integrations/test_middleware.py +++ b/posthog/test/integrations/test_middleware.py @@ -5,6 +5,7 @@ get_tags, ) import unittest +from contextlib import contextmanager from unittest.mock import Mock, patch import asyncio from parameterized import parameterized @@ -869,6 +870,7 @@ def sync_get_response(request): class TestCoroutineDetectionWithoutAsgiref(unittest.TestCase): + @contextmanager def _load_fallback(self): import builtins import importlib @@ -884,21 +886,21 @@ def import_without_asgiref(name, *args, **kwargs): return real_import(name, *args, **kwargs) self.addCleanup(importlib.reload, module) - with ( - patch("builtins.__import__", side_effect=import_without_asgiref), - warnings.catch_warnings(), - ): + # 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) - return importlib.reload(module).iscoroutinefunction + with patch("builtins.__import__", side_effect=import_without_asgiref): + iscoroutinefunction = importlib.reload(module).iscoroutinefunction + yield iscoroutinefunction def test_detects_async_functions(self): - iscoroutinefunction = self._load_fallback() - async def view(request): return request - self.assertTrue(iscoroutinefunction(view)) - self.assertFalse(iscoroutinefunction(lambda request: 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) @@ -912,7 +914,8 @@ def __call__(self, request): handler = Handler() handler._is_coroutine = marker - self.assertTrue(self._load_fallback()(handler)) + with self._load_fallback() as iscoroutinefunction: + self.assertTrue(iscoroutinefunction(handler)) if __name__ == "__main__":