Skip to content

ref(rules): Update typing in rule files #69068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/sentry/api/endpoints/project_rules_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get(self, request: Request, project) -> Response:

# TODO: conditions need to be based on actions
for rule_type, rule_cls in rules:
node = rule_cls(project)
node = rule_cls(project=project)
# skip over conditions if they are not in the migrated set for a project with alert-filters
if project_has_filters and node.id in MIGRATED_CONDITIONS:
continue
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/buffer/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def push_to_hash(

def get_hash(
self, model: type[models.Model], field: dict[str, models.Model | str | int]
) -> dict[str, str]:
) -> list[dict[str, str]]:
key = self._make_key(model, field)
return self._execute_redis_operation(key, RedisOperation.HASH_GET_ALL)

Expand Down
15 changes: 7 additions & 8 deletions src/sentry/rules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import abc
import logging
from collections import namedtuple
from collections.abc import Callable, Sequence
from typing import Any, ClassVar
from collections.abc import Callable, Mapping, Sequence
from typing import TYPE_CHECKING, Any, ClassVar

from django import forms

from sentry.eventstore.models import GroupEvent
from sentry.models.project import Project
from sentry.models.rule import Rule
from sentry.models.rulefirehistory import RuleFireHistory
from sentry.snuba.dataset import Dataset
from sentry.types.condition_activity import ConditionActivity
from sentry.types.rules import RuleFuture

if TYPE_CHECKING:
from sentry.models.rule import Rule

"""
Rules apply either before an event gets stored, or immediately after.

Expand Down Expand Up @@ -60,7 +62,7 @@ class RuleBase(abc.ABC):
def __init__(
self,
project: Project,
data: dict[str, Any] | None = None,
data: Mapping[str, Any] | None = None,
rule: Rule | None = None,
rule_fire_history: RuleFireHistory | None = None,
) -> None:
Expand All @@ -81,10 +83,7 @@ def get_option(self, key: str, default: str | None = None) -> str:
return self.data.get(key, default)

def get_form_instance(self) -> forms.Form:
data: dict[str, Any] | None = None
if self.had_data:
data = self.data
return self.form_cls(data)
return self.form_cls(self.data if self.had_data else None)

def render_label(self) -> str:
return self.label.format(**self.data)
Expand Down
7 changes: 7 additions & 0 deletions src/sentry/rules/conditions/base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import abc
from collections.abc import Sequence
from datetime import datetime
from typing import TypedDict

from sentry.eventstore.models import GroupEvent
from sentry.rules.base import EventState, RuleBase
from sentry.types.condition_activity import ConditionActivity


class GenericCondition(TypedDict):
# the ID in the rules registry that maps to a condition class
# e.g. "sentry.rules.conditions.every_event.EveryEventCondition"
id: str


class EventCondition(RuleBase, abc.ABC):
rule_type = "condition/event"

Expand Down
Loading
Loading