Skip to content

ref(issues): Convert GroupType class config to ClassVar#113423

Draft
JoshFerge wants to merge 1 commit intomasterfrom
joshferge/ref/classvar-grouptype
Draft

ref(issues): Convert GroupType class config to ClassVar#113423
JoshFerge wants to merge 1 commit intomasterfrom
joshferge/ref/classvar-grouptype

Conversation

@JoshFerge
Copy link
Copy Markdown
Member

@JoshFerge JoshFerge commented Apr 20, 2026

`GroupType` is never instantiated in production — subclasses declare their configuration as class-level attributes (`type_id = 1001`, `slug = "foo"`, etc.) and the registry stores the classes themselves. Despite that, the base declared fields as `@dataclass(frozen=True)` instance attributes, so every access like `group_type.type_id` (where `group_type: type[GroupType]`) was really class-level access against an instance-level annotation.

Convert the fields to `ClassVar[T]`, preserving the same defaults. Also convert `ReplayGroupTypeDefaults.notification_config` for the same reason.

Small behavior change

`GroupType.post_init` was previously dead code — nothing instantiates `GroupType`, so the category-value validation never actually ran. Move the same validation to `init_subclass` so it fires at subclass-definition time (earlier, and actually reachable). Updates:

  • `test_grouptype.py::test_category_validation` — now expects the `ValueError` at class-definition time.
  • `test_validators.py` — two tests were instantiating `GroupType(...)` to build a mock; since the base no longer takes fields as constructor args, convert them to subclasses of `GroupType` instead (which matches real production usage).

Why this PR exists

Prep for the mypy upgrade to 1.20.1 in #113419. 1.20.1 adds a new `[misc]` check, "Cannot access instance-only attribute on class object," and `GroupType` is the single biggest cluster of such errors in the repo. Splitting it out so the targeted reviewers only have to look at their own areas.

Safe under the current mypy (1.19.1).

Agent transcript: https://claudescope.sentry.dev/share/JFAQdL9BKvw8zOc8RROKUSNl-Xpuu8y4p4URoIp7DDk

`GroupType` is never instantiated in production: subclasses declare their
configuration as class-level attributes (`type_id = 1001`, `slug = "foo"`,
etc.) and the registry stores the classes themselves. Despite that, the base
declared fields as `@dataclass(frozen=True)` instance attributes, so every
access like `group_type.type_id` (where `group_type: type[GroupType]`) is
really class-level access.

Convert the fields to `ClassVar[T]`, preserving the same defaults. Also
convert the `ReplayGroupTypeDefaults` mixin's `notification_config` for the
same reason.

Behavior change: `GroupType.__post_init__` was previously dead code (nothing
instantiates `GroupType`), so category validation never actually ran. Move
the same validation to `__init_subclass__` so it fires at subclass-definition
time. Update the two tests that were constructing `GroupType(...)` (not a real
production usage pattern) to define subclasses and the category-validation
test to expect the failure at class-definition time.

Prep work for the mypy 1.20 upgrade (#113419) — safe under 1.19.1.

Agent transcript: https://claudescope.sentry.dev/share/8kuOPo_F7g0JelnKiHguToNZr0_y4AKNCOGNvydSZac
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 20, 2026

Backend Test Failures

Failures on a91b976 in this run:

tests/sentry/workflow_engine/endpoints/test_validators.py::TestBaseGroupTypeDetectorValidator::test_validate_type_validlog
[gw1] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/workflow_engine/endpoints/test_validators.py:118: in test_validate_type_valid
    class TestGroupType(GroupType):
src/sentry/issues/grouptype.py:282: in __init_subclass__
    registry.add(cls)
src/sentry/issues/grouptype.py:117: in add
    raise ValueError(
E   ValueError: A group type with the type_id 1 has already been registered.
tests/sentry/workflow_engine/endpoints/test_validators.py::TestBaseGroupTypeDetectorValidator::test_validate_type_incompatiblelog
[gw1] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/workflow_engine/endpoints/test_validators.py:142: in test_validate_type_incompatible
    class TestGroupType(GroupType):
src/sentry/issues/grouptype.py:282: in __init_subclass__
    registry.add(cls)
src/sentry/issues/grouptype.py:117: in add
    raise ValueError(
E   ValueError: A group type with the type_id 1 has already been registered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant