Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit d07dcfb

Browse files
chore: fix (some) type errors in BA notify (#777)
1 parent 1799be9 commit d07dcfb

12 files changed

Lines changed: 127 additions & 92 deletions

File tree

services/activation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def activate_user(db_session, org_ownerid: int, user_ownerid: int) -> bool:
104104
return activation_success
105105

106106

107-
def schedule_new_user_activated_task(self, org_ownerid, user_ownerid):
107+
def schedule_new_user_activated_task(org_ownerid, user_ownerid):
108108
celery_app.send_task(
109109
new_user_activated_task_name,
110110
args=None,

services/bundle_analysis/notify/__init__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import NamedTuple
2+
from typing import NamedTuple, Never
33

44
import sentry_sdk
55
from shared.yaml import UserYaml
@@ -35,9 +35,9 @@ class NotificationFullContext(NamedTuple):
3535

3636

3737
class BundleAnalysisNotifyReturn(NamedTuple):
38-
notifications_configured: tuple[NotificationType]
39-
notifications_attempted: tuple[NotificationType]
40-
notifications_successful: tuple[NotificationType]
38+
notifications_configured: tuple[NotificationType, ...]
39+
notifications_attempted: tuple[NotificationType, ...] | tuple[Never]
40+
notifications_successful: tuple[NotificationType, ...] | tuple[Never]
4141

4242
def to_NotificationSuccess(self) -> NotificationSuccess:
4343
notification_configured_count = len(self.notifications_configured)
@@ -102,33 +102,33 @@ def create_context_for_notification(
102102
tuple[NotificationContextBuilder, MessageStrategyInterface],
103103
] = {
104104
NotificationType.PR_COMMENT: (
105-
BundleAnalysisPRCommentContextBuilder,
106-
BundleAnalysisCommentMarkdownStrategy,
105+
BundleAnalysisPRCommentContextBuilder(),
106+
BundleAnalysisCommentMarkdownStrategy(),
107107
),
108108
NotificationType.COMMIT_STATUS: (
109-
CommitStatusNotificationContextBuilder,
110-
CommitStatusMessageStrategy,
109+
CommitStatusNotificationContextBuilder(),
110+
CommitStatusMessageStrategy(),
111111
),
112112
# The commit-check API is more powerful than COMMIT_STATUS
113113
# but we currently don't differentiate between them
114114
NotificationType.GITHUB_COMMIT_CHECK: (
115-
CommitStatusNotificationContextBuilder,
116-
CommitStatusMessageStrategy,
115+
CommitStatusNotificationContextBuilder(),
116+
CommitStatusMessageStrategy(),
117117
),
118118
}
119119
notifier_strategy = notifier_lookup.get(notification_type)
120120
if notifier_strategy is None:
121121
msg = f"No context builder for {notification_type}. Skipping"
122122
log.error(msg)
123123
return None
124-
builder_class, message_strategy_class = notifier_strategy
124+
builder_instance, message_strategy_instance = notifier_strategy
125125
try:
126-
builder = builder_class().initialize_from_context(
126+
builder = builder_instance.initialize_from_context(
127127
self.current_yaml, base_context
128128
)
129129
return NotificationFullContext(
130130
builder.build_context().get_result(),
131-
message_strategy_class(),
131+
message_strategy_instance,
132132
)
133133
except NotificationContextBuildError as exp:
134134
log.warning(
@@ -142,8 +142,8 @@ def create_context_for_notification(
142142
def get_notification_contexts(
143143
self,
144144
base_context: BaseBundleAnalysisNotificationContext,
145-
notification_types: list[NotificationType],
146-
) -> list[BaseBundleAnalysisNotificationContext]:
145+
notification_types: tuple[NotificationType, ...],
146+
) -> list[NotificationFullContext]:
147147
previous_context = base_context
148148
specific_contexts = []
149149
for notification_type in notification_types:

services/bundle_analysis/notify/contexts/__init__.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ class NotificationContextField(Generic[T]):
4747
def __set_name__(self, owner, name) -> None:
4848
self._name = name
4949

50-
def __get__(self, instance: "NotificationContextField", owner) -> T:
50+
def __get__(self, instance, owner) -> T:
5151
if self._name not in instance.__dict__:
5252
msg = f"Property {self._name} is not loaded. Make sure to build the context before using it."
5353
raise ContextNotLoadedError(msg)
5454
return instance.__dict__[self._name]
5555

56-
def __set__(self, instance: "NotificationContextField", value: T) -> None:
56+
def __set__(self, instance, value: T) -> None:
5757
instance.__dict__[self._name] = value
5858

5959

@@ -84,13 +84,9 @@ def repository_service(self) -> TorngitBaseAdapter:
8484
installation_name_to_use=self.gh_app_installation_name,
8585
)
8686

87-
commit_report: CommitReport = NotificationContextField[CommitReport]()
88-
bundle_analysis_report: BundleAnalysisReport = NotificationContextField[
89-
BundleAnalysisReport
90-
]()
91-
user_config: NotificationUserConfig = NotificationContextField[
92-
NotificationUserConfig
93-
]()
87+
commit_report = NotificationContextField[CommitReport]()
88+
bundle_analysis_report = NotificationContextField[BundleAnalysisReport]()
89+
user_config = NotificationContextField[NotificationUserConfig]()
9490

9591

9692
class NotificationContextBuildError(Exception):
@@ -109,7 +105,7 @@ class NotificationContextBuilder:
109105

110106
current_yaml: UserYaml
111107
""" Used with `initialize_from_context` method. Declare the fields the class wants to copy over when initializing from another context."""
112-
fields_of_interest: tuple[str] = (
108+
fields_of_interest: tuple[str, ...] = (
113109
"commit_report",
114110
"bundle_analysis_report",
115111
"user_config",
@@ -188,14 +184,13 @@ def load_user_config(self) -> "NotificationContextBuilder":
188184
This allows all notifiers to access configuration for any notifier and already have the defaults
189185
"""
190186
comment_config: bool | dict = self.current_yaml.read_yaml_field("comment")
191-
if not comment_config:
192-
required_changes = False
187+
required_changes: bool | Literal["bundle_increase"]
188+
if isinstance(comment_config, bool):
189+
required_changes = comment_config
193190
required_changes_threshold = BundleThreshold("absolute", 0)
194191
else:
195-
required_changes: bool | Literal["bundle_increase"] = comment_config.get(
196-
"require_bundle_changes", False
197-
)
198-
required_changes_threshold: int | float = comment_config.get(
192+
required_changes = comment_config.get("require_bundle_changes", False)
193+
required_changes_threshold = comment_config.get(
199194
"bundle_change_threshold",
200195
BundleThreshold("absolute", 0),
201196
)

services/bundle_analysis/notify/contexts/comment.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,14 @@ class BundleAnalysisPRCommentNotificationContext(BaseBundleAnalysisNotificationC
4242

4343
notification_type = NotificationType.PR_COMMENT
4444

45-
pull: EnrichedPull = NotificationContextField[EnrichedPull]()
46-
bundle_analysis_comparison: BundleAnalysisComparison = NotificationContextField[
47-
BundleAnalysisComparison
48-
]()
49-
commit_status_level: CommitStatusLevel = NotificationContextField[
50-
CommitStatusLevel
51-
]()
52-
should_use_upgrade_comment: bool = NotificationContextField[bool]()
45+
pull = NotificationContextField[EnrichedPull]()
46+
bundle_analysis_comparison = NotificationContextField[BundleAnalysisComparison]()
47+
commit_status_level = NotificationContextField[CommitStatusLevel]()
48+
should_use_upgrade_comment = NotificationContextField[bool]()
5349

5450

5551
class BundleAnalysisPRCommentContextBuilder(NotificationContextBuilder):
56-
fields_of_interest: tuple[str] = (
52+
fields_of_interest: tuple[str, ...] = (
5753
"commit_report",
5854
"bundle_analysis_report",
5955
"user_config",
@@ -176,8 +172,7 @@ def evaluate_should_use_upgrade_message(self) -> Self:
176172
)
177173
if successful_activation:
178174
schedule_new_user_activated_task(
179-
activate_seat_info.owner_id,
180-
activate_seat_info.author_id,
175+
activate_seat_info.owner_id, activate_seat_info.author_id
181176
)
182177
self._notification_context.should_use_upgrade_comment = False
183178
else:
@@ -215,4 +210,4 @@ def build_context(self) -> Self:
215210
)
216211

217212
def get_result(self) -> BundleAnalysisPRCommentNotificationContext:
218-
return self._notification_context
213+
return self._notification_context # type: ignore

services/bundle_analysis/notify/contexts/commit_status.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,12 @@
3939
class CommitStatusNotificationContext(BaseBundleAnalysisNotificationContext):
4040
notification_type = NotificationType.COMMIT_STATUS
4141

42-
pull: EnrichedPull | None = NotificationContextField[EnrichedPull | None]()
43-
bundle_analysis_comparison: BundleAnalysisComparison = NotificationContextField[
44-
BundleAnalysisComparison
45-
]()
46-
commit_status_level: CommitStatusLevel = NotificationContextField[
47-
CommitStatusLevel
48-
]()
49-
commit_status_url: str = NotificationContextField[str]()
50-
cache_ttl: int = NotificationContextField[int]()
51-
should_use_upgrade_comment: bool = NotificationContextField[bool]()
42+
pull = NotificationContextField[EnrichedPull | None]()
43+
bundle_analysis_comparison = NotificationContextField[BundleAnalysisComparison]()
44+
commit_status_level = NotificationContextField[CommitStatusLevel]()
45+
commit_status_url = NotificationContextField[str]()
46+
cache_ttl = NotificationContextField[int]()
47+
should_use_upgrade_comment = NotificationContextField[bool]()
5248

5349
@property
5450
def base_commit(self) -> Commit:
@@ -58,7 +54,7 @@ def base_commit(self) -> Commit:
5854

5955

6056
class CommitStatusNotificationContextBuilder(NotificationContextBuilder):
61-
fields_of_interest: tuple[str] = (
57+
fields_of_interest: tuple[str, ...] = (
6258
"commit_report",
6359
"bundle_analysis_report",
6460
"user_config",

services/bundle_analysis/notify/contexts/tests/test_comment_context.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import MagicMock
22

33
import pytest
4+
from shared.config import PATCH_CENTRIC_DEFAULT_CONFIG
45
from shared.yaml import UserYaml
56

67
from database.models.core import GITHUB_APP_INSTALLATION_DEFAULT_NAME
@@ -145,7 +146,11 @@ def test_evaluate_changes_fail(self, config, total_size_delta, dbsession, mocker
145146
@pytest.mark.parametrize(
146147
"config, total_size_delta",
147148
[
148-
pytest.param({}, 100, id="default_config"),
149+
pytest.param(
150+
PATCH_CENTRIC_DEFAULT_CONFIG,
151+
100,
152+
id="default_config",
153+
),
149154
pytest.param(
150155
{"comment": {"require_bundle_changes": False}},
151156
100,
@@ -281,7 +286,7 @@ def test_build_context(self, dbsession, mocker, mock_storage):
281286
enriched_pull = get_enriched_pull_setting_up_mocks(
282287
dbsession, mocker, (head_commit, base_commit)
283288
)
284-
user_yaml = UserYaml.from_dict({})
289+
user_yaml = UserYaml.from_dict(PATCH_CENTRIC_DEFAULT_CONFIG)
285290
builder = BundleAnalysisPRCommentContextBuilder().initialize(
286291
head_commit, user_yaml, GITHUB_APP_INSTALLATION_DEFAULT_NAME
287292
)
@@ -304,7 +309,7 @@ def test_build_context(self, dbsession, mocker, mock_storage):
304309

305310
def test_initialize_from_context(self, dbsession, mocker):
306311
head_commit, _ = get_commit_pair(dbsession)
307-
user_yaml = UserYaml.from_dict({})
312+
user_yaml = UserYaml.from_dict(PATCH_CENTRIC_DEFAULT_CONFIG)
308313
builder = BundleAnalysisPRCommentContextBuilder().initialize(
309314
head_commit, user_yaml, GITHUB_APP_INSTALLATION_DEFAULT_NAME
310315
)

services/bundle_analysis/notify/contexts/tests/test_commit_status_context.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import MagicMock
22

33
import pytest
4+
from shared.config import PATCH_CENTRIC_DEFAULT_CONFIG
45
from shared.validation.types import BundleThreshold
56
from shared.yaml import UserYaml
67

@@ -143,42 +144,51 @@ async def test_load_bundle_comparison_when_pull_is_none(
143144
"yaml_dict, percent_change, absolute_change, expected",
144145
[
145146
pytest.param(
146-
{},
147+
PATCH_CENTRIC_DEFAULT_CONFIG,
147148
1.0,
148149
10000,
149150
CommitStatusLevel.INFO,
150151
id="default_config_within_5%_change",
151152
),
152153
pytest.param(
153-
{},
154+
PATCH_CENTRIC_DEFAULT_CONFIG,
154155
5.5,
155156
10000,
156157
CommitStatusLevel.WARNING,
157158
id="default_config_outside_5%_change",
158159
),
159160
pytest.param(
160-
{"bundle_analysis": {"warning_threshold": 10000}},
161+
{
162+
**PATCH_CENTRIC_DEFAULT_CONFIG,
163+
"bundle_analysis": {"warning_threshold": 10000},
164+
},
161165
1.0,
162166
10001,
163167
CommitStatusLevel.WARNING,
164168
id="informational_outside_range",
165169
),
166170
pytest.param(
167-
{"bundle_analysis": {"warning_threshold": 10000}},
171+
{
172+
**PATCH_CENTRIC_DEFAULT_CONFIG,
173+
"bundle_analysis": {"warning_threshold": 10000},
174+
},
168175
1.0,
169176
10000,
170177
CommitStatusLevel.INFO,
171178
id="informational_within_range",
172179
),
173180
pytest.param(
174-
{"bundle_analysis": {"warning_threshold": 10000, "status": True}},
181+
{
182+
**PATCH_CENTRIC_DEFAULT_CONFIG,
183+
"bundle_analysis": {"warning_threshold": 10000, "status": True},
184+
},
175185
1.0,
176186
10001,
177187
CommitStatusLevel.ERROR,
178188
id="fail_outside_range_absolute",
179189
),
180190
pytest.param(
181-
{"bundle_analysis": {"status": True}},
191+
{**PATCH_CENTRIC_DEFAULT_CONFIG, "bundle_analysis": {"status": True}},
182192
5.1,
183193
10000,
184194
CommitStatusLevel.ERROR,
@@ -191,6 +201,7 @@ def test_load_commit_status_level(
191201
):
192202
head_commit = CommitFactory()
193203
dbsession.add(head_commit)
204+
yaml_dict.update(PATCH_CENTRIC_DEFAULT_CONFIG)
194205
user_yaml = UserYaml.from_dict(yaml_dict)
195206
builder = CommitStatusNotificationContextBuilder().initialize(
196207
head_commit, user_yaml, GITHUB_APP_INSTALLATION_DEFAULT_NAME
@@ -220,7 +231,7 @@ def test_build_context(self, dbsession, mocker, mock_storage):
220231
enriched_pull = get_enriched_pull_setting_up_mocks(
221232
dbsession, mocker, (head_commit, base_commit)
222233
)
223-
user_yaml = UserYaml.from_dict({})
234+
user_yaml = UserYaml.from_dict(PATCH_CENTRIC_DEFAULT_CONFIG)
224235
builder = CommitStatusNotificationContextBuilder().initialize(
225236
head_commit, user_yaml, GITHUB_APP_INSTALLATION_DEFAULT_NAME
226237
)
@@ -252,7 +263,7 @@ def test_build_context(self, dbsession, mocker, mock_storage):
252263

253264
def test_initialize_from_context(self, dbsession, mocker):
254265
head_commit, _ = get_commit_pair(dbsession)
255-
user_yaml = UserYaml.from_dict({})
266+
user_yaml = UserYaml.from_dict(PATCH_CENTRIC_DEFAULT_CONFIG)
256267
builder = CommitStatusNotificationContextBuilder().initialize(
257268
head_commit, user_yaml, GITHUB_APP_INSTALLATION_DEFAULT_NAME
258269
)
@@ -330,7 +341,15 @@ def test_evaluate_should_use_upgrade_message(
330341
return_value=auto_activate_succeeds,
331342
)
332343
head_commit, _ = get_commit_pair(dbsession)
333-
user_yaml = UserYaml.from_dict({})
344+
user_yaml = UserYaml.from_dict(
345+
{
346+
"comment": {
347+
"layout": "reach,diff,flags,tree,reach",
348+
"behavior": "default",
349+
"show_carryforward_flags": False,
350+
}
351+
}
352+
)
334353
builder = CommitStatusNotificationContextBuilder().initialize(
335354
head_commit, user_yaml, GITHUB_APP_INSTALLATION_DEFAULT_NAME
336355
)

0 commit comments

Comments
 (0)