Skip to content
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

ref(grouping): Add types to enhancement methods #87370

Merged
merged 7 commits into from
Mar 20, 2025

Conversation

lobsterkatie
Copy link
Member

This adds type hints for all of the enhancements methods, to increase the safety of upcoming refactors.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Mar 18, 2025
Copy link

codecov bot commented Mar 19, 2025

Codecov Report

Attention: Patch coverage is 98.61111% with 1 line in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/sentry/grouping/enhancer/__init__.py 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #87370      +/-   ##
==========================================
- Coverage   87.74%   87.74%   -0.01%     
==========================================
  Files        9896     9896              
  Lines      561312   561334      +22     
  Branches    22146    22146              
==========================================
+ Hits       492510   492527      +17     
- Misses      68398    68403       +5     
  Partials      404      404              

class Enhancements:
# NOTE: You must add a version to ``VERSIONS`` any time attributes are added
# to this class, s.t. no enhancements lacking these attributes are loaded
# from cache.
# See ``GroupingConfigLoader._get_enhancements`` in src/sentry/grouping/api.py.

def __init__(
self, rules, rust_enhancements: RustEnhancements, version=None, bases=None, id=None
self,
rules: list[EnhancementRule],
Copy link
Member

Choose a reason for hiding this comment

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

Please use abstract types when within the signature (e.g. Sequence) and more specific ones when returning (e.g. list). Unless it gets on the way for whatever reason.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is definitely not the hill I want to die on, and I can switch it if you feel strongly. But before I go through and make a bunch of changes:

In a purely theoretical sense I guess I understand the principle, but in practice, for methods used purely internally, it always feels like overkill to me. Is there a strong argument in favor that I'm missing?

Copy link
Member

Choose a reason for hiding this comment

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

I won't push hard on this. It is mostly as documented in the best practice document. I can understand the argument of internal usage.

@@ -287,7 +299,8 @@ def as_dict(self, with_rules=False):
rv["rules"] = [x.as_dict() for x in self.rules]
return rv

def _to_config_structure(self):
def _to_config_structure(self) -> list[Any]:
# TODO: Can we switch this to a tuple so we can type it more exactly?
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a named tuple?

        from collections import namedtuple
        
        ConfigStructure = namedtuple('ConfigStructure', ['version', 'bases', 'rules'])

        return ConfigStructure(
            version=self.version,
            bases=self.bases,
            rules=[x._to_config_structure(self.version) for x in self.rules],
        )

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, that'd work. I'd rather leave it for a future PR, though, as my goal with this one is purely to add types, not change behavior.

Copy link
Member

Choose a reason for hiding this comment

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

Works for me.

@armenzg armenzg self-requested a review March 19, 2025 18:53
@lobsterkatie lobsterkatie force-pushed the kmclb-enhancement-types-prework branch from 2760276 to 4eb7eb8 Compare March 19, 2025 20:30
@lobsterkatie lobsterkatie force-pushed the kmclb-add-types-to-enhancements-code branch from f742d58 to 570fff0 Compare March 19, 2025 20:32
@lobsterkatie lobsterkatie force-pushed the kmclb-enhancement-types-prework branch from 4eb7eb8 to 0c66767 Compare March 19, 2025 21:23
@lobsterkatie lobsterkatie force-pushed the kmclb-add-types-to-enhancements-code branch from 570fff0 to bebfc24 Compare March 19, 2025 21:23
@lobsterkatie lobsterkatie force-pushed the kmclb-enhancement-types-prework branch from 0c66767 to 94395bd Compare March 20, 2025 18:33
@lobsterkatie lobsterkatie force-pushed the kmclb-add-types-to-enhancements-code branch from bebfc24 to 0478892 Compare March 20, 2025 18:33
lobsterkatie added a commit that referenced this pull request Mar 20, 2025
As a first step towards adding types to the enhancements code, this makes a handful of code changes which will be necessary once types are added to all of the enhancements methods, which will happen in #87370.
Base automatically changed from kmclb-enhancement-types-prework to master March 20, 2025 19:29
@lobsterkatie lobsterkatie force-pushed the kmclb-add-types-to-enhancements-code branch from 0478892 to a637739 Compare March 20, 2025 19:34
@lobsterkatie lobsterkatie marked this pull request as ready for review March 20, 2025 20:05
@lobsterkatie lobsterkatie requested a review from a team as a code owner March 20, 2025 20:05
@lobsterkatie lobsterkatie merged commit 51d4ccf into master Mar 20, 2025
47 checks passed
@lobsterkatie lobsterkatie deleted the kmclb-add-types-to-enhancements-code branch March 20, 2025 20:06
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.

2 participants