Skip to content

Commit

Permalink
fix: property labels need to be known about in the backend but are de…
Browse files Browse the repository at this point in the history
…fined in the front end (#27328)

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
pauldambra and github-actions[bot] authored Jan 9, 2025
1 parent e80bde2 commit 022d623
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 76 deletions.
2 changes: 1 addition & 1 deletion ee/hogai/taxonomy_agent/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from langgraph.errors import NodeInterrupt
from pydantic import ValidationError

from ee.hogai.taxonomy import CORE_FILTER_DEFINITIONS_BY_GROUP
from posthog.taxonomy.taxonomy import CORE_FILTER_DEFINITIONS_BY_GROUP
from ee.hogai.taxonomy_agent.parsers import (
ReActParserException,
ReActParserMissingActionException,
Expand Down
2 changes: 1 addition & 1 deletion ee/hogai/taxonomy_agent/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pydantic import BaseModel, Field, RootModel

from ee.hogai.taxonomy import CORE_FILTER_DEFINITIONS_BY_GROUP
from posthog.taxonomy.taxonomy import CORE_FILTER_DEFINITIONS_BY_GROUP
from posthog.hogql.database.schema.channel_type import DEFAULT_CHANNEL_TYPES
from posthog.hogql_queries.ai.actors_property_taxonomy_query_runner import ActorsPropertyTaxonomyQueryRunner
from posthog.hogql_queries.ai.event_taxonomy_query_runner import EventTaxonomyQueryRunner
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/lib/taxonomy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ export const SESSION_INITIAL_PROPERTIES_ADAPTED_FROM_EVENTS = new Set([
'rdt_cid',
])

// If adding event properties with labels, check whether they should be added to
// PROPERTY_NAME_ALIASES in posthog/api/property_definition.py
// see code to output JSON below this
// changing values in here you need to sync to python posthog/posthog/taxonomy/taxonomy.py
export const CORE_FILTER_DEFINITIONS_BY_GROUP = {
events: {
'': {
Expand Down
32 changes: 16 additions & 16 deletions mypy-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,22 @@ posthog/temporal/data_imports/workflow_activities/sync_new_schemas.py:0: note: d
posthog/temporal/data_imports/workflow_activities/sync_new_schemas.py:0: note: def get(self, Type, Sequence[str], /) -> Sequence[str]
posthog/temporal/data_imports/workflow_activities/sync_new_schemas.py:0: note: def [_T] get(self, Type, _T, /) -> Sequence[str] | _T
posthog/temporal/data_imports/workflow_activities/sync_new_schemas.py:0: error: Argument "source_id" to "sync_old_schemas_with_new_schemas" has incompatible type "str"; expected "UUID" [arg-type]
posthog/taxonomy/property_definition_api.py:0: error: Item "AnonymousUser" of "User | AnonymousUser" has no attribute "organization" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "None" of "Organization | Any | None" has no attribute "is_feature_available" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "ForeignObjectRel" of "Field[Any, Any] | ForeignObjectRel | GenericForeignKey" has no attribute "cached_col" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "GenericForeignKey" of "Field[Any, Any] | ForeignObjectRel | GenericForeignKey" has no attribute "cached_col" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Incompatible types in assignment (expression has type "Manager[EnterprisePropertyDefinition]", variable has type "QuerySet[PropertyDefinition, PropertyDefinition]") [assignment]
posthog/taxonomy/property_definition_api.py:0: error: Item "BasePagination" of "BasePagination | None" has no attribute "get_limit" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "None" of "BasePagination | None" has no attribute "get_limit" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "BasePagination" of "BasePagination | None" has no attribute "get_offset" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "None" of "BasePagination | None" has no attribute "get_offset" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "BasePagination" of "BasePagination | None" has no attribute "set_count" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "None" of "BasePagination | None" has no attribute "set_count" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "AnonymousUser" of "User | AnonymousUser" has no attribute "organization" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "None" of "Organization | Any | None" has no attribute "is_feature_available" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Incompatible types in assignment (expression has type "type[EnterprisePropertyDefinitionSerializer]", variable has type "type[PropertyDefinitionSerializer]") [assignment]
posthog/taxonomy/property_definition_api.py:0: error: Item "AnonymousUser" of "User | AnonymousUser" has no attribute "organization" [union-attr]
posthog/taxonomy/property_definition_api.py:0: error: Item "None" of "Organization | Any | None" has no attribute "is_feature_available" [union-attr]
posthog/tasks/exports/test/test_csv_exporter.py:0: error: Function is missing a return type annotation [no-untyped-def]
posthog/tasks/exports/test/test_csv_exporter.py:0: error: Function is missing a type annotation [no-untyped-def]
posthog/tasks/exports/test/test_csv_exporter.py:0: error: Function is missing a type annotation for one or more arguments [no-untyped-def]
Expand Down Expand Up @@ -717,22 +733,6 @@ posthog/api/test/dashboards/test_dashboard.py:0: error: Module "django.utils.tim
posthog/api/test/dashboards/test_dashboard.py:0: error: Module "django.utils.timezone" does not explicitly export attribute "timedelta" [attr-defined]
posthog/api/test/dashboards/test_dashboard.py:0: error: Module "django.utils.timezone" does not explicitly export attribute "timedelta" [attr-defined]
posthog/api/query.py:0: error: Statement is unreachable [unreachable]
posthog/api/property_definition.py:0: error: Item "AnonymousUser" of "User | AnonymousUser" has no attribute "organization" [union-attr]
posthog/api/property_definition.py:0: error: Item "None" of "Organization | Any | None" has no attribute "is_feature_available" [union-attr]
posthog/api/property_definition.py:0: error: Item "ForeignObjectRel" of "Field[Any, Any] | ForeignObjectRel | GenericForeignKey" has no attribute "cached_col" [union-attr]
posthog/api/property_definition.py:0: error: Item "GenericForeignKey" of "Field[Any, Any] | ForeignObjectRel | GenericForeignKey" has no attribute "cached_col" [union-attr]
posthog/api/property_definition.py:0: error: Incompatible types in assignment (expression has type "Manager[EnterprisePropertyDefinition]", variable has type "QuerySet[PropertyDefinition, PropertyDefinition]") [assignment]
posthog/api/property_definition.py:0: error: Item "BasePagination" of "BasePagination | None" has no attribute "get_limit" [union-attr]
posthog/api/property_definition.py:0: error: Item "None" of "BasePagination | None" has no attribute "get_limit" [union-attr]
posthog/api/property_definition.py:0: error: Item "BasePagination" of "BasePagination | None" has no attribute "get_offset" [union-attr]
posthog/api/property_definition.py:0: error: Item "None" of "BasePagination | None" has no attribute "get_offset" [union-attr]
posthog/api/property_definition.py:0: error: Item "BasePagination" of "BasePagination | None" has no attribute "set_count" [union-attr]
posthog/api/property_definition.py:0: error: Item "None" of "BasePagination | None" has no attribute "set_count" [union-attr]
posthog/api/property_definition.py:0: error: Item "AnonymousUser" of "User | AnonymousUser" has no attribute "organization" [union-attr]
posthog/api/property_definition.py:0: error: Item "None" of "Organization | Any | None" has no attribute "is_feature_available" [union-attr]
posthog/api/property_definition.py:0: error: Incompatible types in assignment (expression has type "type[EnterprisePropertyDefinitionSerializer]", variable has type "type[PropertyDefinitionSerializer]") [assignment]
posthog/api/property_definition.py:0: error: Item "AnonymousUser" of "User | AnonymousUser" has no attribute "organization" [union-attr]
posthog/api/property_definition.py:0: error: Item "None" of "Organization | Any | None" has no attribute "is_feature_available" [union-attr]
posthog/api/event.py:0: error: Argument 1 to <tuple> has incompatible type "*tuple[str, ...]"; expected "type[BaseRenderer]" [arg-type]
posthog/admin/inlines/plugin_attachment_inline.py:0: error: Signature of "has_add_permission" incompatible with supertype "BaseModelAdmin" [override]
posthog/admin/inlines/plugin_attachment_inline.py:0: note: Superclass:
Expand Down
4 changes: 2 additions & 2 deletions posthog/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
personal_api_key,
plugin,
plugin_log_entry,
property_definition,
proxy_record,
query,
scheduled_change,
Expand All @@ -61,6 +60,7 @@
uploaded_media,
user,
)
from ..taxonomy import property_definition_api
from .dashboards import dashboard, dashboard_templates
from .data_management import DataManagementViewSet
from .session import SessionViewSet
Expand Down Expand Up @@ -264,7 +264,7 @@ def register_grandfathered_environment_nested_viewset(
)
projects_router.register(
r"property_definitions",
property_definition.PropertyDefinitionViewSet,
property_definition_api.PropertyDefinitionViewSet,
"project_property_definitions",
["project_id"],
)
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/test/__snapshots__/test_api_docs.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
'/home/runner/work/posthog/posthog/posthog/api/project.py: Warning [ProjectViewSet > ProjectBackwardCompatSerializer]: could not resolve field on model <class \'posthog.models.project.Project\'> with path "default_modifiers". This is likely a custom field that does some unknown magic. Maybe consider annotating the field/property? Defaulting to "string". (Exception: Project has no field named \'default_modifiers\')',
'/home/runner/work/posthog/posthog/posthog/api/project.py: Warning [ProjectViewSet > ProjectBackwardCompatSerializer]: could not resolve field on model <class \'posthog.models.project.Project\'> with path "person_on_events_querying_enabled". This is likely a custom field that does some unknown magic. Maybe consider annotating the field/property? Defaulting to "string". (Exception: Project has no field named \'person_on_events_querying_enabled\')',
'/home/runner/work/posthog/posthog/posthog/api/project.py: Warning [ProjectViewSet > ProjectBackwardCompatSerializer]: unable to resolve type hint for function "get_product_intents". Consider using a type hint or @extend_schema_field. Defaulting to string.',
"/home/runner/work/posthog/posthog/posthog/api/property_definition.py: Error [PropertyDefinitionViewSet]: exception raised while getting serializer. Hint: Is get_serializer_class() returning None or is get_queryset() not working without a request? Ignoring the view for now. (Exception: 'AnonymousUser' object has no attribute 'organization')",
'/home/runner/work/posthog/posthog/posthog/api/proxy_record.py: Warning [ProxyRecordViewset]: could not derive type of path parameter "id" because it is untyped and obtaining queryset from the viewset failed. Consider adding a type to the path (e.g. <int:id>) or annotating the parameter type with @extend_schema. Defaulting to "string".',
'/home/runner/work/posthog/posthog/posthog/api/proxy_record.py: Warning [ProxyRecordViewset]: could not derive type of path parameter "organization_id" because it is untyped and obtaining queryset from the viewset failed. Consider adding a type to the path (e.g. <int:organization_id>) or annotating the parameter type with @extend_schema. Defaulting to "string".',
'/home/runner/work/posthog/posthog/posthog/api/query.py: Warning [QueryViewSet]: could not derive type of path parameter "id" because it is untyped and obtaining queryset from the viewset failed. Consider adding a type to the path (e.g. <int:id>) or annotating the parameter type with @extend_schema. Defaulting to "string".',
Expand All @@ -83,6 +82,7 @@
'/home/runner/work/posthog/posthog/posthog/models/event/util.py: Warning [EventViewSet > ClickhouseEventSerializer]: unable to resolve type hint for function "get_timestamp". Consider using a type hint or @extend_schema_field. Defaulting to string.',
'/home/runner/work/posthog/posthog/posthog/session_recordings/session_recording_api.py: Warning [SessionRecordingViewSet > SessionRecordingSerializer]: unable to resolve type hint for function "storage". Consider using a type hint or @extend_schema_field. Defaulting to string.',
'/home/runner/work/posthog/posthog/posthog/session_recordings/session_recording_api.py: Warning [SessionRecordingViewSet]: could not derive type of path parameter "project_id" because model "posthog.session_recordings.models.session_recording.SessionRecording" contained no such field. Consider annotating parameter with @extend_schema. Defaulting to "string".',
"/home/runner/work/posthog/posthog/posthog/taxonomy/property_definition_api.py: Error [PropertyDefinitionViewSet]: exception raised while getting serializer. Hint: Is get_serializer_class() returning None or is get_queryset() not working without a request? Ignoring the view for now. (Exception: 'AnonymousUser' object has no attribute 'organization')",
'/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py: Warning [QueryViewSet > ModelMetaclass]: Encountered 2 components with identical names "Person" and different classes <class \'str\'> and <class \'posthog.api.person.PersonSerializer\'>. This will very likely result in an incorrect schema. Try renaming one.',
'Warning: encountered multiple names for the same choice set (EffectivePrivilegeLevelEnum). This may be unwanted even though the generated schema is technically correct. Add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
'Warning: encountered multiple names for the same choice set (HrefMatchingEnum). This may be unwanted even though the generated schema is technically correct. Add an entry to ENUM_NAME_OVERRIDES to fix the naming.',
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/test/test_property_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from rest_framework import status

from posthog.api.property_definition import PropertyDefinitionQuerySerializer
from posthog.models import (
EventDefinition,
EventProperty,
Expand All @@ -13,6 +12,7 @@
Team,
ActivityLog,
)
from posthog.taxonomy.property_definition_api import PropertyDefinitionQuerySerializer
from posthog.test.base import APIBaseTest, BaseTest


Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql_queries/ai/team_taxonomy_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)

try:
from ee.hogai.taxonomy import CORE_FILTER_DEFINITIONS_BY_GROUP
from posthog.taxonomy.taxonomy import CORE_FILTER_DEFINITIONS_BY_GROUP
except ImportError:
CORE_FILTER_DEFINITIONS_BY_GROUP = {}

Expand Down
Empty file added posthog/taxonomy/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dataclasses
import json
from typing import Any, Optional, cast
from typing import Any, Optional, cast, Self

from django.db import connection
from loginas.utils import is_impersonated_session
Expand All @@ -19,6 +19,7 @@
from posthog.models import EventProperty, PropertyDefinition, User
from posthog.models.activity_logging.activity_log import Detail, log_activity
from posthog.models.utils import UUIDT
from posthog.taxonomy.taxonomy import PROPERTY_NAME_ALIASES


class SeenTogetherQuerySerializer(serializers.Serializer):
Expand Down Expand Up @@ -126,7 +127,7 @@ class QueryContext:

params: dict = dataclasses.field(default_factory=dict)

def with_properties_to_filter(self, properties_to_filter: Optional[str]) -> "QueryContext":
def with_properties_to_filter(self, properties_to_filter: Optional[str]) -> Self:
if properties_to_filter:
return dataclasses.replace(
self,
Expand All @@ -136,7 +137,7 @@ def with_properties_to_filter(self, properties_to_filter: Optional[str]) -> "Que
else:
return self

def with_is_numerical_flag(self, is_numerical: Optional[str]) -> "QueryContext":
def with_is_numerical_flag(self, is_numerical: Optional[str]) -> Self:
if is_numerical:
return dataclasses.replace(
self,
Expand All @@ -145,7 +146,7 @@ def with_is_numerical_flag(self, is_numerical: Optional[str]) -> "QueryContext":
else:
return self

def with_feature_flags(self, is_feature_flag: Optional[bool]) -> "QueryContext":
def with_feature_flags(self, is_feature_flag: Optional[bool]) -> Self:
if is_feature_flag is None:
return self
elif is_feature_flag:
Expand Down Expand Up @@ -202,9 +203,7 @@ def with_type_filter(self, type: str, group_type_index: Optional[int]):
},
)

def with_event_property_filter(
self, event_names: Optional[str], filter_by_event_names: Optional[bool]
) -> "QueryContext":
def with_event_property_filter(self, event_names: Optional[str], filter_by_event_names: Optional[bool]) -> Self:
event_property_filter = ""
event_name_filter = ""
event_property_field = "NULL"
Expand All @@ -228,14 +227,14 @@ def with_event_property_filter(
params={**self.params, "event_names": list(map(str, event_names or []))},
)

def with_search(self, search_query: str, search_kwargs: dict) -> "QueryContext":
def with_search(self, search_query: str, search_kwargs: dict) -> Self:
return dataclasses.replace(
self,
search_query=search_query,
params={**self.params, "team_id": self.team_id, **search_kwargs},
)

def with_excluded_properties(self, excluded_properties: Optional[str], type: str) -> "QueryContext":
def with_excluded_properties(self, excluded_properties: Optional[str], type: str) -> Self:
if excluded_properties:
excluded_properties = json.loads(excluded_properties)

Expand Down Expand Up @@ -305,45 +304,6 @@ def _join_on_event_property(self):
)


# See frontend/src/lib/taxonomy.tsx for where this came from and see
# frontend/scripts/print_property_name_aliases.ts for how to regenerate
PROPERTY_NAME_ALIASES = {
"$autocapture_disabled_server_side": "Autocapture Disabled Server-Side",
"$client_session_initial_referring_host": "Referrer Host",
"$client_session_initial_utm_content": "Initial UTM Source",
"$client_session_initial_utm_term": "Initial UTM Source",
"$console_log_recording_enabled_server_side": "Console Log Recording Enabled Server-Side",
"$el_text": "Element Text",
"$exception_colno": "Exception source column number",
"$exception_handled": "Exception was handled",
"$exception_lineno": "Exception source line number",
"$geoip_disable": "GeoIP Disabled",
"$geoip_time_zone": "Timezone",
"$group_0": "Group 1",
"$group_1": "Group 2",
"$group_2": "Group 3",
"$group_3": "Group 4",
"$group_4": "Group 5",
"$ip": "IP Address",
"$lib": "Library",
"$lib_custom_api_host": "Library Custom API Host",
"$lib_version": "Library Version",
"$lib_version__major": "Library Version (Major)",
"$lib_version__minor": "Library Version (Minor)",
"$lib_version__patch": "Library Version (Patch)",
"$performance_raw": "Browser Performance",
"$referrer": "Referrer URL",
"$selected_content": "Copied content",
"$session_recording_recorder_version_server_side": "Session Recording Recorder Version Server-Side",
"$user_agent": "Raw User Agent",
"build": "App Build",
"previous_build": "App Previous Build",
"previous_version": "App Previous Version",
"referring_application": "Referrer Application",
"version": "App Version",
}


def add_name_alias_to_search_query(search_term: str):
if not search_term:
return ""
Expand Down Expand Up @@ -504,6 +464,7 @@ def dangerously_get_queryset(self):
order_by_verified = False
if use_enterprise_taxonomy:
try:
# noinspection PyUnresolvedReferences
from ee.models.property_definition import EnterprisePropertyDefinition

# Prevent fetching deprecated `tags` field. Tags are separately fetched in TaggedItemSerializerMixin
Expand Down Expand Up @@ -591,6 +552,7 @@ def safely_get_object(self, queryset):
id = self.kwargs["id"]
if self.request.user.organization.is_feature_available(AvailableFeature.INGESTION_TAXONOMY):
try:
# noinspection PyUnresolvedReferences
from ee.models.property_definition import EnterprisePropertyDefinition
except ImportError:
pass
Expand Down
Loading

0 comments on commit 022d623

Please sign in to comment.