Skip to content

ref(client): Improve get_integration typing #3550

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

Merged
merged 4 commits into from
Sep 23, 2024

Conversation

szokeasaurusrex
Copy link
Member

Improve get_integration typing to make it clear that we return an Optional[Integration]. Further, add overloads to specify that when called with some integration type I (i.e. I is a subclass of Integration), then get_integration guarantees a return value of Optional[I].

These changes should enhance type safety by explicitly guaranteeing the existing behavior of get_integration.

Copy link

codecov bot commented Sep 20, 2024

Codecov Report

Attention: Patch coverage is 50.63291% with 39 lines in your changes missing coverage. Please review.

Project coverage is 84.29%. Comparing base (2a2fab1) to head (7f456f4).
Report is 2 commits behind head on master.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/integrations/aws_lambda.py 16.66% 5 Missing ⚠️
sentry_sdk/integrations/django/__init__.py 20.00% 2 Missing and 2 partials ⚠️
sentry_sdk/integrations/gcp.py 0.00% 4 Missing ⚠️
sentry_sdk/integrations/cohere.py 50.00% 1 Missing and 2 partials ⚠️
sentry_sdk/integrations/openai.py 50.00% 1 Missing and 2 partials ⚠️
sentry_sdk/integrations/starlette.py 57.14% 1 Missing and 2 partials ⚠️
sentry_sdk/integrations/aiohttp.py 50.00% 1 Missing and 1 partial ⚠️
sentry_sdk/integrations/atexit.py 33.33% 1 Missing and 1 partial ⚠️
sentry_sdk/integrations/bottle.py 50.00% 1 Missing and 1 partial ⚠️
sentry_sdk/integrations/celery/__init__.py 33.33% 1 Missing and 1 partial ⚠️
... and 5 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3550      +/-   ##
==========================================
- Coverage   84.51%   84.29%   -0.22%     
==========================================
  Files         133      133              
  Lines       13855    13885      +30     
  Branches     2930     2929       -1     
==========================================
- Hits        11709    11705       -4     
- Misses       1421     1440      +19     
- Partials      725      740      +15     
Files with missing lines Coverage Δ
sentry_sdk/client.py 83.49% <100.00%> (ø)
sentry_sdk/integrations/fastapi.py 90.00% <100.00%> (ø)
sentry_sdk/integrations/flask.py 86.99% <100.00%> (+0.10%) ⬆️
sentry_sdk/integrations/sanic.py 80.55% <100.00%> (ø)
sentry_sdk/integrations/sys_exit.py 100.00% <100.00%> (ø)
sentry_sdk/integrations/threading.py 100.00% <100.00%> (+1.81%) ⬆️
sentry_sdk/integrations/anthropic.py 82.85% <50.00%> (-0.17%) ⬇️
sentry_sdk/integrations/aiohttp.py 84.66% <50.00%> (-0.88%) ⬇️
sentry_sdk/integrations/atexit.py 71.87% <33.33%> (-6.25%) ⬇️
sentry_sdk/integrations/bottle.py 86.31% <50.00%> (-1.73%) ⬇️
... and 11 more

... and 3 files with indirect coverage changes

Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

Nice, LGTM as is, but mypy is complaining so this will probably need additional changes, would prefer to review once it's ready :)

@szokeasaurusrex szokeasaurusrex marked this pull request as draft September 20, 2024 11:47
@szokeasaurusrex
Copy link
Member Author

@sentrivana yep still working on fixing mypy, will mark as draft until this is ready to be reviewed. I think the cleanest solution will be to replace @ensure_integration_enabled with the old manual checking that the integration is not None whenever we need to access the Integration object, because this allows mypy to recognize that the integration object cannot be None.

We can still use @ensure_integration_enabled anywhere we don't need to access the Integration object

@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/get_integration-typing branch 4 times, most recently from b3678cb to d0fa5d5 Compare September 20, 2024 12:18
@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review September 20, 2024 12:35
@szokeasaurusrex
Copy link
Member Author

The mypy errors led me to find a couple places where we were not checking that the integration was still enabled, so I think this change will potentially help us avoid making such mistakes in the future

@szokeasaurusrex szokeasaurusrex marked this pull request as draft September 23, 2024 08:03
@szokeasaurusrex szokeasaurusrex self-assigned this Sep 23, 2024
Improve `get_integration` typing to make it clear that we return an `Optional[Integration]`. Further, add overloads to specify that when called with some integration type `I` (i.e. `I` is a subclass of `Integration`), then `get_integration` guarantees a return value of `Optional[I]`.

These changes should enhance type safety by explicitly guaranteeing the existing behavior of `get_integration`.
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/get_integration-typing branch from d0fa5d5 to bad136f Compare September 23, 2024 08:10
@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review September 23, 2024 08:11
Copy link
Contributor

@sentrivana sentrivana left a comment

Choose a reason for hiding this comment

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

Are we still using ensure_integration_enabled anywhere after this? If not, we can remove it.

@szokeasaurusrex
Copy link
Member Author

@sentrivana, yeah we are; I found ca. 100 usages after these changes. @ensure_integration_enabled is still helpful anytime we don't need access to the integration object within the patched function, which is quite often apparently. However, it is less useful when we need the integration object, since we anyways need to check whether the integration object is None in order to ensure type safety

@szokeasaurusrex szokeasaurusrex enabled auto-merge (squash) September 23, 2024 09:18
@szokeasaurusrex szokeasaurusrex mentioned this pull request Sep 23, 2024
@szokeasaurusrex szokeasaurusrex merged commit 8060a64 into master Sep 23, 2024
123 of 124 checks passed
@szokeasaurusrex szokeasaurusrex deleted the szokeasaurusrex/get_integration-typing branch September 23, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants