Skip to content

Conversation

@RyanSkonnord
Copy link
Contributor

@RyanSkonnord RyanSkonnord commented Aug 2, 2024

Add type hints to the sentry.incidents.logic module and some related functions.

Remove unused function parameters. Change default collection args from None to () 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.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Aug 2, 2024
@codecov
Copy link

codecov bot commented Aug 2, 2024

Codecov Report

Attention: Patch coverage is 93.37748% with 10 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/sentry/incidents/logic.py 95.31% 3 Missing and 3 partials ⚠️
src/sentry/incidents/models/alert_rule.py 66.66% 1 Missing and 1 partial ⚠️
src/sentry/integrations/pagerduty/utils.py 33.33% 1 Missing and 1 partial ⚠️
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     

@RyanSkonnord RyanSkonnord marked this pull request as ready for review August 6, 2024 03:37
@RyanSkonnord RyanSkonnord requested a review from a team as a code owner August 6, 2024 03:37
@RyanSkonnord RyanSkonnord requested a review from a team August 6, 2024 03:37
Copy link
Member

@cathteng cathteng left a 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 :/



def schedule_update_project_config(alert_rule: AlertRule, projects: Sequence[Project]):
def schedule_update_project_config(alert_rule: AlertRule, projects: Collection[Project]) -> None:
Copy link
Member

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?

Copy link
Contributor Author

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.

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`.)
TODO: Refactor to not violate the type system
"""
model.id = None # type: ignore[assignment]
Copy link
Contributor Author

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:

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.)

Copy link
Contributor

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)

Copy link
Contributor

@asottile-sentry asottile-sentry left a 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?

@RyanSkonnord
Copy link
Contributor Author

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 "sentry.incidents.logic" from pyproject.toml. You want me to put it back in?

@asottile-sentry
Copy link
Contributor

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 "sentry.incidents.logic" from pyproject.toml. You want me to put it back in?

no. there are logical parts of this patch that can be landed separately which will reduce risk and improve reviewability

@RyanSkonnord
Copy link
Contributor Author

@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.

Copy link
Contributor

@asottile-sentry asottile-sentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much easier to review now -- thanks!

@RyanSkonnord RyanSkonnord merged commit ed8e530 into master Sep 11, 2024
@RyanSkonnord RyanSkonnord deleted the ryanskonnord/incident-type-hints branch September 11, 2024 17:23
@github-actions github-actions bot locked and limited conversation to collaborators Sep 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants