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): correct find_enum_collisions hint for inline ChoiceField (#59414)

Julian Bezd7660815519a
diff --git a/posthog/management/commands/find_enum_collisions.py b/posthog/management/commands/find_enum_collisions.py
index 346e266561eb..b78a86b8eeb2 100644
--- a/posthog/management/commands/find_enum_collisions.py
+++ b/posthog/management/commands/find_enum_collisions.py
@@ -5,9 +5,11 @@ but prints actionable output instead of opaque warnings.
Run this when `hogli build:openapi-schema` fails with messages like
"enum naming encountered a non-optimally resolvable collision" / "Format5eaEnum" — it
-prints a suggested ENUM_NAME_OVERRIDES entry for posthog/settings/web.py
-(pastable as-is for type-hint enum collisions; for ChoiceField collisions you fill in
-the Choices/Enum class path).
+prints a suggested ENUM_NAME_OVERRIDES entry for posthog/settings/web.py. The suggestion
+is pastable as-is for type-hint enum collisions and for ChoiceField collisions whose
+choices are plain inline lists (labels == values); only ChoiceField collisions with
+custom labels (e.g. a `TextChoices` class where labels differ from values) need the
+model class path filled in.
See also:
posthog/settings/web.py — ENUM_NAME_OVERRIDES (where the fix goes)
@@ -116,13 +118,22 @@ class Command(BaseCommand):
if len(hash_name_mapping[prop_hash]) == 1:
continue
auto_name = f"{_camelize(prop_name)}{prop_hash[:3].capitalize()}{enum_suffix}"
+ values = hash_values[prop_hash]
+ # Even ChoiceField gets x-spec-enum-id, but if the field was built from a
+ # plain list (`choices=["A", "B"]`) DRF expands it to (value, value) pairs
+ # — same hash as the inline-list override path. The model-class-path
+ # override is only required when labels differ from values (typical
+ # `TextChoices` with explicit labels).
+ inline_override_hash = list_hash([(v, v) for v in values if v not in ("", None)])
+ inline_override_matches = inline_override_hash == prop_hash
collisions.append(
{
"field": prop_name,
"auto_name": auto_name,
"hash": prop_hash,
- "values": hash_values[prop_hash],
+ "values": values,
"has_spec_id": hash_has_spec_id[prop_hash],
+ "inline_override_matches": inline_override_matches,
"components": sorted(hash_name_mapping[prop_hash]),
}
)
@@ -147,9 +158,11 @@ class Command(BaseCommand):
self.stdout.write("")
self.stdout.write(" Override entry to add to ENUM_NAME_OVERRIDES in web.py")
self.stdout.write(" (key defaults to the current auto-resolved name; rename for a nicer schema type):")
- if c["has_spec_id"]:
+ if c["has_spec_id"] and not c["inline_override_matches"]:
self.stdout.write(f' "{c["auto_name"]}": "your.models.module.Model.ChoicesClass",')
- self.stdout.write(" # ChoiceField path — fill in the actual Choices/Enum class path")
+ self.stdout.write(
+ " # ChoiceField with custom labels (labels != values) — override must be a model class path"
+ )
else:
vals = c["values"]
if all(isinstance(v, int) for v in vals):
@@ -157,7 +170,12 @@ class Command(BaseCommand):
self.stdout.write(f' "{c["auto_name"]}": {formatted},')
else:
self.stdout.write(f' "{c["auto_name"]}": {json.dumps(vals)},')
- self.stdout.write(" # Type-hint path — paste as-is to silence the warning")
+ if c["has_spec_id"]:
+ self.stdout.write(
+ " # ChoiceField with inline choices (labels == values) — paste as-is to silence the warning"
+ )
+ else:
+ self.stdout.write(" # Type-hint path — paste as-is to silence the warning")
self.stdout.write("\n ---\n")
all_hashes = set()
diff --git a/posthog/settings/web.py b/posthog/settings/web.py
index 1418b295ee59..dfdecb3cbec9 100644
--- a/posthog/settings/web.py
+++ b/posthog/settings/web.py
@@ -409,9 +409,11 @@ SPECTACULAR_SETTINGS = {
# Workflow to resolve a collision:
# 1. Run `python manage.py find_enum_collisions` — it prints the field name,
# the auto-generated name (e.g. `Format5eaEnum`), the enum values, which
- # components share the hash, and a suggested override entry. For type-hint
- # enum collisions the suggested line is pastable as-is; for ChoiceField
- # collisions you supply the Choices/Enum class path.
+ # components share the hash, and a suggested override entry. The suggestion
+ # is pastable as-is for inline-list collisions (type-hint enums and
+ # ChoiceFields with plain `choices=["A", "B"]`); only ChoiceFields with
+ # custom labels (TextChoices where labels differ from values) need the
+ # Choices/Enum class path filled in.
# 2. Add the suggested entry below (pick the right category — see "hash trap"
# note below). Optionally rename the key from the auto-generated name to a
# more semantic one to improve the generated schema type's name.
@@ -424,14 +426,17 @@ SPECTACULAR_SETTINGS = {
# hashes them, and using the wrong format silently fails (the override is ignored
# and the warning persists):
#
- # 1. Model class paths — used for ChoiceField-backed enums where drf-spectacular
- # injects x-spec-enum-id from (value, label) tuples. The override must point
- # to the same Choices/Enum class so _load_enum_name_overrides hashes identically.
+ # 1. Model class paths — used for ChoiceField-backed enums whose labels differ
+ # from their values (typical `TextChoices` with explicit labels). The override
+ # must point to the same Choices/Enum class so _load_enum_name_overrides hashes
+ # identically to the x-spec-enum-id.
#
# 2. Inline value lists — used for Enum type-hint enums (SerializerMethodField
- # return types) where there is NO x-spec-enum-id. Postprocessing hashes these
- # as (value, value) tuples, so the override must also be a plain value list
- # (which _load_enum_name_overrides normalizes to (value, value)).
+ # return types) AND ChoiceFields whose choices are plain lists (labels equal
+ # values). The override must be a plain value list, which
+ # _load_enum_name_overrides normalizes to (value, value) tuples — matching
+ # both the no-x-spec-enum-id type-hint path and the inline-choices ChoiceField
+ # path (drf-spectacular generates the x-spec-enum-id from the same tuples).
# --- Model class paths (ChoiceField x-spec-enum-id hashes) ---
"RestrictionLevelEnum": "products.dashboards.backend.models.dashboard.Dashboard.RestrictionLevel",
"OrganizationMembershipLevelEnum": "posthog.models.organization.OrganizationMembership.Level",