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(client): Improve get_integration typing #3550

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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 51.28205% with 38 lines in your changes missing coverage. Please review.

Project coverage is 84.30%. Comparing base (ed614c0) to head (d0fa5d5).

✅ 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/bottle.py 50.00% 1 Missing and 1 partial ⚠️
sentry_sdk/integrations/celery/__init__.py 33.33% 1 Missing and 1 partial ⚠️
sentry_sdk/integrations/huggingface_hub.py 33.33% 1 Missing and 1 partial ⚠️
... and 5 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3550      +/-   ##
==========================================
- Coverage   84.49%   84.30%   -0.19%     
==========================================
  Files         133      133              
  Lines       13837    13868      +31     
  Branches     2927     2928       +1     
==========================================
+ Hits        11691    11692       +1     
- Misses       1420     1436      +16     
- Partials      726      740      +14     
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 98.21% <100.00%> (-1.79%) ⬇️
sentry_sdk/integrations/anthropic.py 82.85% <50.00%> (-0.17%) ⬇️
sentry_sdk/integrations/atexit.py 75.75% <50.00%> (-2.37%) ⬇️
sentry_sdk/integrations/aiohttp.py 84.27% <50.00%> (-0.89%) ⬇️
sentry_sdk/integrations/bottle.py 86.31% <50.00%> (-1.73%) ⬇️
... and 11 more

... and 2 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 3 times, most recently from 6844797 to b3678cb Compare September 20, 2024 12:17
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 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

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