Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(notebooks): remove the legacy python node annotation (#96536)

Sinan Kucukkoseler1db08c1ac78a
diff --git a/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr b/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr
index 8d145274bfb2..188069d6ed38 100644
--- a/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr
+++ b/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr
@@ -1649,14 +1649,6 @@
LIMIT 2
'''
# ---
-# name: TestNotebooks.test_updates_notebook.39
- '''
- SELECT COUNT(*) AS "__count"
- FROM "posthog_activitylog"
- WHERE ("posthog_activitylog"."scope" = 'Notebook'
- AND "posthog_activitylog"."team_id" = 99999)
- '''
-# ---
# name: TestNotebooks.test_updates_notebook.4
'''
SELECT "posthog_organizationmembership"."id",
@@ -1712,69 +1704,6 @@
LIMIT 21
'''
# ---
-# name: TestNotebooks.test_updates_notebook.40
- '''
- SELECT "posthog_activitylog"."id",
- "posthog_activitylog"."team_id",
- "posthog_activitylog"."organization_id",
- "posthog_activitylog"."user_id",
- "posthog_activitylog"."was_impersonated",
- "posthog_activitylog"."is_system",
- "posthog_activitylog"."client",
- "posthog_activitylog"."ip_address",
- "posthog_activitylog"."activity",
- "posthog_activitylog"."item_id",
- "posthog_activitylog"."scope",
- "posthog_activitylog"."detail",
- "posthog_activitylog"."created_at",
- "posthog_user"."id",
- "posthog_user"."password",
- "posthog_user"."last_login",
- "posthog_user"."first_name",
- "posthog_user"."last_name",
- "posthog_user"."is_staff",
- "posthog_user"."date_joined",
- "posthog_user"."uuid",
- "posthog_user"."current_organization_id",
- "posthog_user"."current_team_id",
- "posthog_user"."email",
- "posthog_user"."pending_email",
- "posthog_user"."distinct_id",
- "posthog_user"."is_email_verified",
- "posthog_user"."credentials_reviewed_at",
- "posthog_user"."requested_password_reset_at",
- "posthog_user"."requested_2fa_reset_at",
- "posthog_user"."has_seen_product_intro_for",
- "posthog_user"."strapi_id",
- "posthog_user"."is_active",
- "posthog_user"."role_at_organization",
- "posthog_user"."theme_mode",
- "posthog_user"."partial_notification_settings",
- "posthog_user"."anonymize_data",
- "posthog_user"."allow_impersonation",
- "posthog_user"."toolbar_mode",
- "posthog_user"."hedgehog_config",
- "posthog_user"."allow_sidebar_suggestions",
- "posthog_user"."shortcut_position",
- "posthog_user"."passkeys_enabled_for_2fa",
- "posthog_user"."hide_mcp_hints",
- "posthog_user"."ui_configuration",
- "posthog_user"."onboarding_skipped_at",
- "posthog_user"."onboarding_skipped_reason",
- "posthog_user"."onboarding_skipped_organization_id",
- "posthog_user"."onboarding_delegated_to_invite_id",
- "posthog_user"."onboarding_delegated_to_organization_id",
- "posthog_user"."onboarding_delegation_accepted_at",
- "posthog_user"."events_column_config",
- "posthog_user"."email_opt_in"
- FROM "posthog_activitylog"
- LEFT OUTER JOIN "posthog_user" ON ("posthog_activitylog"."user_id" = "posthog_user"."id")
- WHERE ("posthog_activitylog"."scope" = 'Notebook'
- AND "posthog_activitylog"."team_id" = 99999)
- ORDER BY "posthog_activitylog"."created_at" DESC
- LIMIT 2
- '''
-# ---
# name: TestNotebooks.test_updates_notebook.5
'''
SELECT "ee_accesscontrol"."id",
diff --git a/posthog/api/test/notebooks/test_notebook.py b/posthog/api/test/notebooks/test_notebook.py
index 0d11796a17f2..f15e88b74ddc 100644
--- a/posthog/api/test/notebooks/test_notebook.py
+++ b/posthog/api/test/notebooks/test_notebook.py
@@ -329,28 +329,6 @@ class TestNotebooks(APIBaseTest, QueryMatchingTest):
"source": {"kind": "HogQLQuery", "query": "SELECT 1"},
}
- def test_python_node_static_analysis(self) -> None:
- content = {
- "type": "doc",
- "content": [
- {
- "type": "ph-python",
- "attrs": {"code": "value = count + 1\nresult = len(value)\n"},
- }
- ],
- }
- response = self.client.post(
- f"/api/projects/{self.team.id}/notebooks",
- data={"content": content},
- )
- assert response.status_code == status.HTTP_201_CREATED
- attrs = response.json()["content"]["content"][0]["attrs"]
- assert attrs["globalsUsed"] == ["count"]
- assert attrs["globalsExportedWithTypes"] == [
- {"name": "result", "type": "unknown"},
- {"name": "value", "type": "unknown"},
- ]
-
def test_listing_does_not_leak_between_teams(self) -> None:
another_team = Team.objects.create(organization=self.organization)
another_user = User.objects.create_and_join(self.organization, "[email protected]", password="")
diff --git a/products/notebooks/backend/markdown_migration.py b/products/notebooks/backend/markdown_migration.py
index 31c95dca8ae7..7483f4e44802 100644
--- a/products/notebooks/backend/markdown_migration.py
+++ b/products/notebooks/backend/markdown_migration.py
@@ -25,7 +25,6 @@ from products.notebooks.backend.markdown_conversion import (
notebook_content_has_comment_marks,
)
from products.notebooks.backend.models import Notebook
-from products.notebooks.backend.python_analysis import annotate_python_nodes
MAX_NOTEBOOK_MIGRATION_BATCH_SIZE = 500
@@ -144,8 +143,7 @@ def _convert_notebook(notebook: Notebook, *, user: User, content: dict[str, Any]
return False
before_update = Notebook.objects.select_related("created_by", "last_modified_by").get(pk=locked_notebook.pk)
- annotated_content = annotate_python_nodes(content)
- locked_notebook.content = annotated_content
+ locked_notebook.content = content
locked_notebook.text_content = text_content
locked_notebook.version += 1
locked_notebook.save(
diff --git a/products/notebooks/backend/presentation/views/notebook.py b/products/notebooks/backend/presentation/views/notebook.py
index 846d440f717e..b9bc58779a7c 100644
--- a/products/notebooks/backend/presentation/views/notebook.py
+++ b/products/notebooks/backend/presentation/views/notebook.py
@@ -107,7 +107,7 @@ from products.notebooks.backend.presentation.widget_throttles import (
WidgetFrameBurstThrottle,
WidgetFrameSustainedThrottle,
)
-from products.notebooks.backend.python_analysis import analyze_python_globals, annotate_python_nodes
+from products.notebooks.backend.python_analysis import analyze_python_globals
from products.notebooks.backend.query_validation import InvalidNotebookQueryError, normalize_notebook_query_nodes
from products.notebooks.backend.sql_v2 import (
PAGE_LOCK_TTL_SECONDS,
@@ -346,9 +346,6 @@ class NotebookSerializer(NotebookMinimalSerializer):
validated_data["short_id"] = short_id
created_by = validated_data.pop("created_by", request.user)
- content = validated_data.get("content")
- if isinstance(content, dict):
- validated_data["content"] = annotate_python_nodes(content)
notebook = Notebook.objects.create(
team=team,
created_by=created_by,
@@ -422,9 +419,6 @@ class NotebookSerializer(NotebookMinimalSerializer):
"and this notebook is publicly shared."
)
- content = validated_data.get("content")
- if isinstance(content, dict):
- validated_data["content"] = annotate_python_nodes(content)
update_diff = markdown_collab.build_markdown_update_diff(
locked_instance.content, validated_data.get("content")
)
@@ -2217,7 +2211,7 @@ class NotebookViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, ForbidD
if result.status == "accepted":
notebook_before = Notebook.objects.get(pk=notebook.pk)
Notebook.objects.filter(pk=notebook.pk).update(
- content=annotate_python_nodes(content) if isinstance(content, dict) else content,
+ content=content,
text_content=data.get("text_content", ""),
title=data.get("title", notebook.title),
version=result.version,
@@ -2324,8 +2318,7 @@ class NotebookViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, ForbidD
validate_cell_count(locked_notebook.content, submitted_content)
except NotebookCellLimitExceeded as err:
raise serializers.ValidationError(str(err))
- annotated_content = annotate_python_nodes(submitted_content)
- diff = markdown_collab.build_markdown_update_diff(locked_notebook.content, annotated_content)
+ diff = markdown_collab.build_markdown_update_diff(locked_notebook.content, submitted_content)
result = markdown_collab.submit_markdown_update(
locked_notebook.team_id,
str(locked_notebook.short_id),
@@ -2339,7 +2332,7 @@ class NotebookViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, ForbidD
)
if result.status == "accepted":
notebook_before = Notebook.objects.get(pk=notebook.pk)
- locked_notebook.content = annotated_content
+ locked_notebook.content = submitted_content
locked_notebook.text_content = data.get("text_content", "")
if "title" in data:
locked_notebook.title = data["title"]
diff --git a/products/notebooks/backend/python_analysis.py b/products/notebooks/backend/python_analysis.py
index 6f2105c5748e..79b510665386 100644
--- a/products/notebooks/backend/python_analysis.py
+++ b/products/notebooks/backend/python_analysis.py
@@ -1,8 +1,7 @@
import ast
-import hashlib
import builtins
from dataclasses import dataclass
-from typing import Any, Literal
+from typing import Literal
from posthog.dataclasses import frozen
@@ -419,48 +418,3 @@ def analyze_python_globals(code: str) -> PythonGlobalsAnalysis:
used=sorted(analyzer.used),
exported_with_types=exported_with_types,
)
-
-
-def compute_globals_analysis_hash(code: str) -> str:
- return hashlib.sha256(code.encode("utf-8")).hexdigest()
-
-
-def annotate_python_nodes(content: Any) -> Any:
- if not isinstance(content, dict):
- return content
-
- def walk(node: Any) -> Any:
- if not isinstance(node, dict):
- return node
-
- node_type = node.get("type")
- if node_type == "ph-python":
- attrs = node.get("attrs")
- if isinstance(attrs, dict):
- code = attrs.get("code", "")
- if isinstance(code, str):
- code_hash = compute_globals_analysis_hash(code)
- existing_hash = attrs.get("globalsAnalysisHash")
- has_cached_analysis = (
- isinstance(existing_hash, str)
- and existing_hash == code_hash
- and "globalsUsed" in attrs
- and "globalsExportedWithTypes" in attrs
- )
- if not has_cached_analysis:
- analysis = analyze_python_globals(code)
- attrs = {
- **attrs,
- "globalsUsed": analysis.used,
- "globalsExportedWithTypes": analysis.exported_with_types,
- "globalsAnalysisHash": code_hash,
- }
- node = {**node, "attrs": attrs}
-
- content_nodes = node.get("content")
- if isinstance(content_nodes, list):
- node = {**node, "content": [walk(child) for child in content_nodes]}
-
- return node
-
- return walk(content)