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(workflows): return frozen dataclasses instead of tuples (#76126)

Arno Hillenc6ff2e4a92e5
diff --git a/posthog/migrations/0243_unpack_plugin_source_files.py b/posthog/migrations/0243_unpack_plugin_source_files.py
index c15b641759c5..d39c8a394735 100644
--- a/posthog/migrations/0243_unpack_plugin_source_files.py
+++ b/posthog/migrations/0243_unpack_plugin_source_files.py
@@ -27,22 +27,22 @@ def forwards_func(apps, schema_editor):
def sync_from_plugin_archive(plugin):
"""Create PluginSourceFile objects from a plugin that has an archive."""
try:
- plugin_json, index_ts, frontend_tsx, site_ts = extract_plugin_code(plugin.archive)
+ code = extract_plugin_code(plugin.archive)
except ValueError as e:
raise exceptions.ValidationError(f"{e} in plugin {plugin}")
# Save plugin.json
- PluginSourceFile.objects.create(plugin=plugin, filename="plugin.json", source=plugin_json)
+ PluginSourceFile.objects.create(plugin=plugin, filename="plugin.json", source=code.plugin_json)
# Save frontend.tsx
- if frontend_tsx is not None:
- PluginSourceFile.objects.create(plugin=plugin, filename="frontend.tsx", source=frontend_tsx)
+ if code.frontend_tsx is not None:
+ PluginSourceFile.objects.create(plugin=plugin, filename="frontend.tsx", source=code.frontend_tsx)
# Save site.ts
- if site_ts is not None:
- PluginSourceFile.objects.create(plugin=plugin, filename="site.ts", source=site_ts)
+ if code.site_ts is not None:
+ PluginSourceFile.objects.create(plugin=plugin, filename="site.ts", source=code.site_ts)
# Save index.ts
- if index_ts is not None:
+ if code.index_ts is not None:
# The original name of the file is not preserved, but this greatly simplifies the rest of the code,
# and we don't need to model the whole filesystem (at this point)
- PluginSourceFile.objects.create(plugin=plugin, filename="index.ts", source=index_ts)
+ PluginSourceFile.objects.create(plugin=plugin, filename="index.ts", source=code.index_ts)
# Source plugins have already been migrated in 0233_plugin_source_file, while local ones don't store code in the DB
for plugin in Plugin.objects.exclude(plugin_type__in=("source", "local")):
diff --git a/posthog/plugins/utils.py b/posthog/plugins/utils.py
index 5324a93a5b77..f980e3797d24 100644
--- a/posthog/plugins/utils.py
+++ b/posthog/plugins/utils.py
@@ -3,6 +3,7 @@ import os
import re
import json
import tarfile
+from dataclasses import dataclass
from tarfile import ReadError
from typing import Any, Optional
from urllib.parse import parse_qs, quote
@@ -311,9 +312,15 @@ def find_index_ts_in_archive(archive: bytes, main_filename: Optional[str] = None
raise ValueError(f"Could not find main file {' or '.join(main_filenames_to_try)}")
-def extract_plugin_code(
- archive: bytes | None, plugin_json_parsed: Optional[dict[str, Any]] = None
-) -> tuple[str, Optional[str], Optional[str], Optional[str]]:
+@dataclass(frozen=True, kw_only=True, slots=True)
+class PluginCode:
+ plugin_json: str
+ index_ts: Optional[str]
+ frontend_tsx: Optional[str]
+ site_ts: Optional[str]
+
+
+def extract_plugin_code(archive: bytes | None, plugin_json_parsed: Optional[dict[str, Any]] = None) -> PluginCode:
"""Extract plugin.json, index.ts (which can be aliased) and frontend.tsx out of an archive.
If plugin.json has already been parsed before this is called, its value can be passed in as an optimization."""
@@ -342,7 +349,7 @@ def extract_plugin_code(
except ValueError:
if frontend_tsx is None and site_ts is None:
raise
- return plugin_json, index_ts, frontend_tsx, site_ts
+ return PluginCode(plugin_json=plugin_json, index_ts=index_ts, frontend_tsx=frontend_tsx, site_ts=site_ts)
def put_json_into_zip_archive(archive: bytes, json_data: dict, filename: str):
diff --git a/products/cdp/backend/models/plugin.py b/products/cdp/backend/models/plugin.py
index 205028d7d859..8cc4aa200a9f 100644
--- a/products/cdp/backend/models/plugin.py
+++ b/products/cdp/backend/models/plugin.py
@@ -358,7 +358,7 @@ class PluginSourceFileManager(models.Manager):
If plugin.json has already been parsed before this is called, its value can be passed in as an optimization."""
try:
archive = bytes(plugin.archive) if plugin.archive else None
- plugin_json, index_ts, frontend_tsx, site_ts = extract_plugin_code(archive, plugin_json_parsed)
+ code = extract_plugin_code(archive, plugin_json_parsed)
except ValueError as e:
raise exceptions.ValidationError(f"{e} in plugin {plugin}")
@@ -370,7 +370,7 @@ class PluginSourceFileManager(models.Manager):
plugin=plugin,
filename="plugin.json",
defaults={
- "source": plugin_json,
+ "source": code.plugin_json,
"transpiled": None,
"status": None,
"error": None,
@@ -379,12 +379,12 @@ class PluginSourceFileManager(models.Manager):
# Save frontend.tsx
frontend_tsx_instance: Optional[PluginSourceFile] = None
- if frontend_tsx is not None:
+ if code.frontend_tsx is not None:
transpiled = None
status = None
error = None
try:
- transpiled = transpile(frontend_tsx, type="site")
+ transpiled = transpile(code.frontend_tsx, type="site")
status = PluginSourceFile.Status.TRANSPILED
except Exception as e:
error = str(e)
@@ -393,7 +393,7 @@ class PluginSourceFileManager(models.Manager):
plugin=plugin,
filename="frontend.tsx",
defaults={
- "source": frontend_tsx,
+ "source": code.frontend_tsx,
"transpiled": transpiled,
"status": status,
"error": error,
@@ -404,12 +404,12 @@ class PluginSourceFileManager(models.Manager):
# Save site.ts
site_ts_instance: Optional[PluginSourceFile] = None
- if site_ts is not None:
+ if code.site_ts is not None:
transpiled = None
status = None
error = None
try:
- transpiled = transpile(site_ts, type="site")
+ transpiled = transpile(code.site_ts, type="site")
status = PluginSourceFile.Status.TRANSPILED
except Exception as e:
error = str(e)
@@ -419,7 +419,7 @@ class PluginSourceFileManager(models.Manager):
plugin=plugin,
filename="site.ts",
defaults={
- "source": site_ts,
+ "source": code.site_ts,
"transpiled": transpiled,
"status": status,
"error": error,
@@ -430,14 +430,14 @@ class PluginSourceFileManager(models.Manager):
# Save index.ts
index_ts_instance: Optional[PluginSourceFile] = None
- if index_ts is not None:
+ if code.index_ts is not None:
# The original name of the file is not preserved, but this greatly simplifies the rest of the code,
# and we don't need to model the whole filesystem (at this point)
index_ts_instance, _ = PluginSourceFile.objects.update_or_create(
plugin=plugin,
filename="index.ts",
defaults={
- "source": index_ts,
+ "source": code.index_ts,
"transpiled": None,
"status": None,
"error": None,