Skip to content

Commit

Permalink
mypy garbage
Browse files Browse the repository at this point in the history
  • Loading branch information
aspicer committed Dec 19, 2024
1 parent 7ba2f4c commit 244f40c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
7 changes: 4 additions & 3 deletions posthog/hogql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ class HogQLQueryExecutor:
limit_context: Optional[LimitContext] = LimitContext.QUERY
timings: HogQLTimings = dataclasses.field(default_factory=HogQLTimings)
pretty: Optional[bool] = True
__uninitialized_context: ClassVar[HogQLContext] = HogQLContext()
context: HogQLContext = dataclasses.field(default_factory=lambda: HogQLQueryExecutor.__uninitialized_context)

__uninitialized_context: ClassVar[HogQLContext] = HogQLContext()

def __post_init__(self):
if self.context is self.__uninitialized_context:
self.context = HogQLContext(team_id=self.team.pk)
Expand Down Expand Up @@ -184,7 +185,7 @@ def _generate_clickhouse_sql(self):
)
except Exception as e:
if self.debug:
self.clickhouse_sql = None
self.clickhouse_sql = ""
if isinstance(e, ExposedCHQueryError | ExposedHogQLError):
self.error = str(e)
else:
Expand Down Expand Up @@ -243,7 +244,7 @@ def _execute_clickhouse_query(self):
HogQLMetadata(language=HogLanguage.HOG_QL, query=self.hogql, debug=True), self.team
)

def generate_clickhouse_sql(self) -> (str, HogQLContext):
def generate_clickhouse_sql(self) -> tuple[str, HogQLContext]:
self._parse_query()
self._process_variables()
self._process_placeholders()
Expand Down
22 changes: 13 additions & 9 deletions posthog/hogql_queries/hogql_cohort_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ def convert(prop: PropertyGroup) -> PropertyGroupFilterValue:


class HogQLCohortQuery:
def __init__(self, cohort_query: CohortQuery = None, cohort: Cohort = None):
team_id = (cohort and cohort.team.pk) or cohort_query._team_id
self.hogql_context = HogQLContext(team_id=team_id, enable_select_queries=True)

def __init__(self, cohort_query: Optional[CohortQuery] = None, cohort: Optional[Cohort] = None):
if cohort is not None:
self.hogql_context = HogQLContext(team_id=cohort.team.pk, enable_select_queries=True)
self.cohort_query = CohortQuery(
Filter(
data={"properties": cohort.properties},
Expand All @@ -78,21 +76,26 @@ def __init__(self, cohort_query: CohortQuery = None, cohort: Cohort = None):
cohort.team,
cohort_pk=cohort.pk,
)
else:
elif cohort_query is not None:
self.hogql_context = HogQLContext(team_id=cohort_query._team_id, enable_select_queries=True)
self.cohort_query = cohort_query
else:
raise

# self.properties = clean_global_properties(self.cohort_query._filter._data["properties"])
self._inner_property_groups = self.cohort_query._inner_property_groups
self._outer_property_groups = self.cohort_query._outer_property_groups
self.team = self.cohort_query._team

def _actors_query(self):
pgfv = convert(self._inner_property_groups)
pgfv = None
if self._inner_property_groups:
pgfv = convert(self._inner_property_groups)
actors_query = ActorsQuery(properties=pgfv, select=["id"])
query_runner = ActorsQueryRunner(team=self.team, query=actors_query)
return query_runner.to_query()

def get_query(self) -> SelectQuery:
def get_query(self) -> SelectQuery | SelectSetQuery:
if not self.cohort_query._outer_property_groups:
# everything is pushed down, no behavioral stuff to do
# thus, use personQuery directly
Expand Down Expand Up @@ -194,16 +197,17 @@ def get_performed_event_condition(self, prop: Property, first_time: bool = False
def get_performed_event_multiple(self, prop: Property) -> ast.SelectQuery:
count = parse_and_validate_positive_integer(prop.operator_value, "operator_value")
# either an action or an event
series: list[Union[EventsNode, ActionsNode]]
if prop.event_type == "events":
series = [EventsNode(event=prop.key)] * (count + 1)
elif prop.event_type == "actions":
series = [ActionsNode(id=int(prop.key))] * (count + 1)
else:
raise ValueError(f"Event type must be 'events' or 'actions'")

funnelStep: int = None
funnelStep: Optional[int] = None

funnelCustomSteps: list[int] = None
funnelCustomSteps: Optional[list[int]] = None

if prop.operator == "gte":
funnelStep = count
Expand Down
6 changes: 3 additions & 3 deletions posthog/hogql_queries/insights/insight_actors_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ def to_query(self) -> ast.SelectQuery | ast.SelectSetQuery:
elif isinstance(self.source_runner, StickinessQueryRunner):
stickiness_runner = cast(StickinessQueryRunner, self.source_runner)
# might have to isinstance this
query = cast(StickinessActorsQuery, self.query)
stickiness_actors_query = cast(StickinessActorsQuery, self.query)
return stickiness_runner.to_actors_query(
interval_num=int(query.day) if query.day is not None else None,
operator=getattr(query, "operator", None),
interval_num=int(stickiness_actors_query.day) if stickiness_actors_query.day is not None else None,
operator=getattr(stickiness_actors_query, "operator", None),
)
elif isinstance(self.source_runner, LifecycleQueryRunner):
lifecycle_runner = cast(LifecycleQueryRunner, self.source_runner)
Expand Down

0 comments on commit 244f40c

Please sign in to comment.