-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
ref(incidents): Add type hints to sentry/incidents/logic.py #75541
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
Conversation
Codecov ReportAttention: Patch coverage is ✅ All tests successful. No failed tests found. Additional details and impacted files@@ Coverage Diff @@
## master #75541 +/- ##
==========================================
- Coverage 78.13% 78.13% -0.01%
==========================================
Files 6931 6931
Lines 307646 307695 +49
Branches 50358 50364 +6
==========================================
+ Hits 240386 240424 +38
- Misses 60864 60870 +6
- Partials 6396 6401 +5 |
… a unit test can patch it
cathteng
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the src/sentry/incidents/logic.py file is huge :/
src/sentry/incidents/logic.py
Outdated
|
|
||
|
|
||
| def schedule_update_project_config(alert_rule: AlertRule, projects: Sequence[Project]): | ||
| def schedule_update_project_config(alert_rule: AlertRule, projects: Collection[Project]) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is Collection looser than Sequence?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Sequence implies that it has a consistent iteration order and can be indexed with brackets, usually meaning a list or tuple. Collection includes set and frozenset.
This reverts commit 88cd7e0.
Raise an InvalidTriggerActionError if the target_value is None. Modify test_discord_channel_id_none to assert for this error. (It appears that the test case's original intent was to test that the serializer doesn't crash, but the case where the target_value is None would inevitably cause an error anyway when we call `DiscordClient.get_channel`.)
…try_app_config Field annotation
…skonnord/incident-type-hints
| TODO: Refactor to not violate the type system | ||
| """ | ||
| model.id = None # type: ignore[assignment] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into it, and apparently nulling out the primary key is the normal and recommended way to copy a model in this way (official docs; Stack Overflow).
I think the reason Mypy doesn't like it is because of our Model class:
sentry/src/sentry/db/models/base.py
Lines 315 to 316 in 0a4fdfd
| class Model(BaseModel): | |
| id: models.Field[int, int] = BoundedBigAutoField(primary_key=True) |
Changing it to
id: models.Field[int | None, int] = BoundedBigAutoField(primary_key=True)seems to fix it. I couldn't find any docs that explain the difference between the two type params to Field; I found the above through trial and error. But I'm guessing/hoping it satisfies the meaning of "it can be nullable during a model's lifecycle as a Python object, but is always a non-null int when persisted". Should we go ahead with such a change to the Model class? (If so, I'd lean toward doing it as a follow-up PR and reverting this then.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's actually an intentional django-stubs thing -- you almost never want id = None (and making it nullable causes all sorts of other problems)
asottile-sentry
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this patch has gotten pretty large -- can we split out parts of those and land them separately to minimize risk and increase reviewability?
It got large because that's what was necessary to remove |
no. there are logical parts of this patch that can be landed separately which will reduce risk and improve reviewability |
|
@asottile-sentry Functional changes broken out into: |
thank you! those are so much easier to review |
|
@asottile-sentry Now the PR should consist only of type annotation fixes, with the only changes to flow being a few helper functions motivated by type-checking. |
asottile-sentry
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

Add type hints to the
sentry.incidents.logicmodule and some related functions.Remove unused function parameters. Change default collection args from
Noneto()where they are treated interchangeably, to minimize nullable arg types. Add explicit null checks where required by the type checker.Add underscore prefix to functions used privately. Move functions that were used only by a single module.