Skip to content

ref(grouping): Small refactor of the GroupingComponent class #80723

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 4 commits into from
Nov 14, 2024
Merged
Changes from all commits
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
28 changes: 14 additions & 14 deletions src/sentry/grouping/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}


def _calculate_contributes(values: Sequence[str | GroupingComponent]) -> bool:
def _calculate_contributes(values: Sequence[str | int | GroupingComponent]) -> bool:
for value in values or ():
if not isinstance(value, GroupingComponent) or value.contributes:
return True
Expand All @@ -35,26 +35,26 @@ class GroupingComponent:
into components to make a hash for grouping purposes.
"""

id: str = "default"
hint: str | None = None
contributes: bool = False
values: Sequence[str | int | GroupingComponent]

def __init__(
self,
id: str,
hint: str | None = None,
contributes: bool | None = None,
values: Sequence[str | GroupingComponent] | None = None,
values: Sequence[str | int | GroupingComponent] | None = None,
variant_provider: bool = False,
):
self.id = id

# Default values
self.hint = DEFAULT_HINTS.get(id)
self.contributes = contributes
self.variant_provider = variant_provider
self.values: Sequence[str | GroupingComponent] = []

self.update(
hint=hint,
hint=hint or DEFAULT_HINTS.get(self.id),
contributes=contributes,
values=values,
values=values or [],
)

@property
Expand Down Expand Up @@ -99,13 +99,13 @@ def _walk_components(component: GroupingComponent, current_path: list[str | None

def get_subcomponent(
self, id: str, only_contributing: bool = False
) -> str | GroupingComponent | None:
) -> str | int | GroupingComponent | None:
"""Looks up a subcomponent by the id and returns the first or `None`."""
return next(self.iter_subcomponents(id=id, only_contributing=only_contributing), None)

def iter_subcomponents(
self, id: str, recursive: bool = False, only_contributing: bool = False
) -> Iterator[str | GroupingComponent | None]:
) -> Iterator[str | int | GroupingComponent | None]:
"""Finds all subcomponents matching an id, optionally recursively."""
for value in self.values:
if isinstance(value, GroupingComponent):
Expand All @@ -123,7 +123,7 @@ def update(
self,
hint: str | None = None,
contributes: bool | None = None,
values: Sequence[str | GroupingComponent] | None = None,
values: Sequence[str | int | GroupingComponent] | None = None,
) -> None:
"""Updates an already existing component with new values."""
if hint is not None:
Expand All @@ -142,7 +142,7 @@ def shallow_copy(self) -> GroupingComponent:
rv.values = list(self.values)
return rv

def iter_values(self) -> Generator[str | GroupingComponent]:
def iter_values(self) -> Generator[str | int | GroupingComponent]:
"""Recursively walks the component and flattens it into a list of
values.
"""
Expand Down Expand Up @@ -180,4 +180,4 @@ def as_dict(self) -> dict[str, Any]:
return rv

def __repr__(self) -> str:
return f"GroupingComponent({self.id!r}, hint={self.hint!r}, contributes={self.contributes!r}, values={self.values!r})"
return f"{self.__class__.__name__}({self.id!r}, hint={self.hint!r}, contributes={self.contributes!r}, values={self.values!r})"
Loading