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(devex): semgrep rule for swappable or big tuple returns (#75956)

Arno Hillen667bd6dedf5a
diff --git a/.github/workflows/ci-security.yaml b/.github/workflows/ci-security.yaml
index 12ee4ebca832..b880453cca9b 100644
--- a/.github/workflows/ci-security.yaml
+++ b/.github/workflows/ci-security.yaml
@@ -117,6 +117,8 @@ jobs:
- 'packages/**'
- 'posthog/**'
- 'products/**'
+ - 'services/**'
+ - 'tools/**'
- '.semgrep/rules/devex/**'
- '.github/workflows/ci-security.yaml'
# Rule tests only read fixtures under .semgrep/.
@@ -395,6 +397,9 @@ jobs:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
+ # PR merge commit plus both parents, so the new-warnings
+ # pass can baseline against the PR base (HEAD^1)
+ fetch-depth: 2
persist-credentials: false
# Pass 1: print WARNING-severity findings. No --error — this step
@@ -411,7 +416,7 @@ jobs:
--metrics=off \
--verbose \
--timeout 300 \
- bin/ common/ ee/ frontend/ packages/ posthog/ products/
+ bin/ common/ ee/ frontend/ packages/ posthog/ products/ services/ tools/
# Pass 2: enforce ERROR-severity rules. Any finding fails the
# job — regression guards live here.
@@ -428,7 +433,27 @@ jobs:
--metrics=off \
--verbose \
--timeout 300 \
- bin/ common/ ee/ frontend/ packages/ posthog/ products/
+ bin/ common/ ee/ frontend/ packages/ posthog/ products/ services/ tools/
+
+ # Pass 3 (PRs only): fail on newly-introduced WARNING findings.
+ # --baseline-commit diffs against the PR base (first parent of the
+ # merge commit), so the pass-1 backlog is grandfathered while new
+ # violations block. See .semgrep/rules/devex/README.md.
+ - name: New warnings (blocking)
+ if: github.event_name == 'pull_request'
+ run: |
+ docker run --rm -v "${{ github.workspace }}:/src" -w /src \
+ -e SEMGREP_ENABLE_VERSION_CHECK=false \
+ ${{ env.SEMGREP_IMAGE }} \
+ semgrep \
+ --config ".semgrep/rules/devex/" \
+ --severity=WARNING \
+ --baseline-commit "$(git rev-parse HEAD^1)" \
+ --error \
+ --metrics=off \
+ --verbose \
+ --timeout 300 \
+ bin/ common/ ee/ frontend/ packages/ posthog/ products/ services/ tools/
semgrep-test-rules:
needs: changes
diff --git a/.semgrep/rules/devex/README.md b/.semgrep/rules/devex/README.md
index eaaea89704da..ea5e1b97a48e 100644
--- a/.semgrep/rules/devex/README.md
+++ b/.semgrep/rules/devex/README.md
@@ -1,9 +1,10 @@
# Devex semgrep rules
-Devex / hygiene rules for the PostHog codebase. Run by the `semgrep-devex` job in `ci-security.yaml` in two passes:
+Devex / hygiene rules for the PostHog codebase. Run by the `semgrep-devex` job in `ci-security.yaml` in three passes:
1. **Warnings (informational)** — `semgrep --severity=WARNING` runs all `severity: WARNING` rules. Findings appear in the CI run output but the step does not fail. Use this mode when a rule has a non-zero backlog of existing violations — the list shows up in CI without blocking master while the codebase is cleaned up.
2. **Errors (blocking)** — `semgrep --severity=ERROR --error` runs all `severity: ERROR` rules and fails CI on any finding. Use this mode for hard regression guards: patterns that should never appear, or migration-style rules whose backlog has been cleaned up.
+3. **New warnings (blocking, PRs only)** — the WARNING rules run again with `--baseline-commit` set to the PR base, plus `--error`. Findings that already exist on the base are grandfathered; findings the PR introduces fail the job. This stops the bleeding on backlogged rules while the backlog is worked down. Caveat: moving or renaming a file makes its grandfathered findings look new to the baseline diff — either fix them as part of the move or exempt them with `# nosemgrep: <rule-id>`.
## Picking a severity
@@ -14,7 +15,7 @@ Pick the severity that matches the rule's intent:
A WARNING rule typically gets flipped to ERROR once its backlog hits zero. The rule stays in `.semgrep/rules/devex/`; only the severity changes. To flip a rule:
-1. Verify locally that `semgrep --config .semgrep/rules/devex/<rule>.yaml common/ ee/ frontend/ posthog/ products/` returns zero findings.
+1. Verify locally that `semgrep --config .semgrep/rules/devex/<rule>.yaml bin/ common/ ee/ frontend/ packages/ posthog/ products/ services/ tools/` returns zero findings.
2. Edit the rule file: change `severity: WARNING` to `severity: ERROR`.
3. Update the rule's `message:` to drop any "informational, not blocking" phrasing — it's now blocking.
diff --git a/.semgrep/rules/devex/no-init-reexports.yaml b/.semgrep/rules/devex/no-init-reexports.yaml
index 4cdbceb75ce7..0b4b82c3402f 100644
--- a/.semgrep/rules/devex/no-init-reexports.yaml
+++ b/.semgrep/rules/devex/no-init-reexports.yaml
@@ -99,6 +99,11 @@ rules:
- 'products/web_analytics/backend/api/__init__.py'
- 'products/wizard/backend/logic/__init__.py'
- 'products/workflows/backend/providers/__init__.py'
+ - 'tools/hogbox-preview/hogbox_preview/__init__.py'
+ - 'tools/hogli-commands/hogli_commands/devenv/__init__.py'
+ - 'tools/hogli-commands/hogli_commands/product/__init__.py'
+ - 'tools/hogli-commands/hogli_commands/workflow_lint/checks/__init__.py'
+ - 'tools/owners/posthog_owners/__init__.py'
# Column-0 anchor: only flag eager, top-level re-exports. Indented ones (TYPE_CHECKING,
# try/except ImportError, function-local) are already lazy and intentionally skipped.
pattern-regex: '(?m)^from\s+\.'
diff --git a/.semgrep/rules/devex/tuple-return-prefer-dataclass.py b/.semgrep/rules/devex/tuple-return-prefer-dataclass.py
new file mode 100644
index 000000000000..62a522b4c2f0
--- /dev/null
+++ b/.semgrep/rules/devex/tuple-return-prefer-dataclass.py
@@ -0,0 +1,83 @@
+# Test cases for tuple-return-prefer-dataclass.
+# ruff: noqa
+from typing import Optional, Tuple
+
+
+# Two elements sharing a type are swappable at the call site
+# ruleid: tuple-return-prefer-dataclass
+def get_range() -> tuple[int, int]:
+ return 0, 1
+
+
+# Legacy typing.Tuple spelling counts too
+# ruleid: tuple-return-prefer-dataclass
+def get_bounds() -> Tuple[float, float]:
+ return 0.0, 1.0
+
+
+# A same-type pair wrapped in Optional is still swappable
+# ruleid: tuple-return-prefer-dataclass
+def maybe_range() -> Optional[tuple[str, str]]:
+ return None
+
+
+# ruleid: tuple-return-prefer-dataclass
+def union_range() -> tuple[int, int] | None:
+ return None
+
+
+# Nested generics still count as the same type
+# ruleid: tuple-return-prefer-dataclass
+def get_lists() -> tuple[list[int], list[int]]:
+ return [], []
+
+
+# 3+ elements force positional access even when all types differ
+# ruleid: tuple-return-prefer-dataclass
+def get_stats() -> tuple[int, str, float]:
+ return 0, "", 0.0
+
+
+# ruleid: tuple-return-prefer-dataclass
+def get_more_stats() -> tuple[int, str, float, bool]:
+ return 0, "", 0.0, True
+
+
+# ruleid: tuple-return-prefer-dataclass
+async def get_async_range() -> tuple[int, int]:
+ return 0, 1
+
+
+class Widget:
+ # ruleid: tuple-return-prefer-dataclass
+ def size(self) -> tuple[int, int]:
+ return 0, 0
+
+
+# Small tuples with unambiguous, differently typed elements are fine
+# ok: tuple-return-prefer-dataclass
+def get_user() -> tuple[str, int]:
+ return "", 0
+
+
+# Homogeneous variable-length tuples have no fixed fields to name
+# ok: tuple-return-prefer-dataclass
+def get_ids() -> tuple[int, ...]:
+ return (1, 2)
+
+
+# Tuple parameters and locals are out of scope; only return annotations
+# ok: tuple-return-prefer-dataclass
+def takes_pair(pair: tuple[int, int]) -> None:
+ coords: tuple[int, int] = pair
+
+
+# Unannotated returns are mypy's job, not this rule's
+# ok: tuple-return-prefer-dataclass
+def unannotated():
+ return 0, 1
+
+
+# ok: tuple-return-prefer-dataclass
+def get_name() -> str:
+ return ""
diff --git a/.semgrep/rules/devex/tuple-return-prefer-dataclass.yaml b/.semgrep/rules/devex/tuple-return-prefer-dataclass.yaml
new file mode 100644
index 000000000000..6f098be8e363
--- /dev/null
+++ b/.semgrep/rules/devex/tuple-return-prefer-dataclass.yaml
@@ -0,0 +1,39 @@
+# Flags function return annotations that are tuples where callers can silently
+# swap same-typed elements (e.g. `tuple[int, int]` for `(start, end)`) or must
+# read 3+ elements positionally. Partial enforcement of the frozen-dataclass
+# convention in the Code Style section of AGENTS.md.
+#
+# See: https://github.com/PostHog/posthog/pull/75818 for context
+rules:
+ - id: tuple-return-prefer-dataclass
+ patterns:
+ - pattern-either:
+ - pattern: |
+ def $F(...) -> $RET:
+ ...
+ - pattern: |
+ async def $F(...) -> $RET:
+ ...
+ - metavariable-pattern:
+ metavariable: $RET
+ patterns:
+ - pattern-either:
+ - pattern: tuple[$T, $T]
+ - pattern: Tuple[$T, $T]
+ - pattern: tuple[$A, $B, $C, ...]
+ - pattern: Tuple[$A, $B, $C, ...]
+ - focus-metavariable: $RET
+ message: |
+ This returns a tuple whose elements can be silently swapped at the
+ call site (two share a type) or must be read positionally (3+
+ elements). Prefer a frozen dataclass with named fields: `@frozen`
+ from `posthog.dataclasses` (frozen, kw_only, slots by default). See
+ the Python bullets in the Code Style section of AGENTS.md.
+ Pre-existing findings are grandfathered; only newly-introduced ones
+ block CI.
+ languages: [python]
+ severity: WARNING
+ metadata:
+ category: maintainability
+ subcategory: audit
+ confidence: HIGH