-
Notifications
You must be signed in to change notification settings - Fork 483
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
Typing issue #2460
Comments
Hey @agusdmb, thanks for reporting this. I imagine the challenge here (and maybe the reason why we went with |
im not sure if this would work on python 2.7 (which is deprecated now, i couldn't find in the documentation about the TypeVar) however:
let me know if im missing something or if you need anything else from me. BTW, this is how i "fixed" it for my usage: from typing import Any, Callable, TypeVar
import sentry_sdk
F = TypeVar("F", bound=Callable[..., Any])
def trace(func: F) -> F:
return sentry_sdk.trace(func) # type: ignore[no-any-return] i am now using that trace instead of the sentry one |
this might not seem like a big issue, but here at Nebuly we have the whole code base type hinted, and we recently started using sentry to find get more insight from different issues, but in some cases loosing the type hint caused more issues than the ones it helped discover with the traces |
Thanks for investigating, you're right, one of the occurrences, for instance, is in the WSGI integration, which is used across all Python versions, so looks like we're not as strict about type hints as I thought. Which makes sense, since in 2.7 typing was a rather niche thing, unlike now, so it's mostly important it works in newer versions. In that case, your
All good! We probably won't get around to doing this soon, but PRs are very much appreciated -- I'll mark this as good first issue so that more folks see it and maybe someone has time to look into it earlier than we would. |
@sentrivana ok great to hear that i have considered opening a PR but i wanted to make sure the change was welcome before investing time on it... i can give it a try in the coming days thanks |
Type hints for sentry_sdk.trace decorator function now indicate that the decorator returns a function with the same signature as it was called with. Previously, the type hints indicated that the decorator could return Any, which caused users to lose type hints for decorated functions. * Improve `sentry_sdk.trace` type hints * Add overloads for None case * Fix typing when `trace` called with `None` Fixes GH-2460
How do you use Sentry?
Sentry Saas (sentry.io)
Version
1.32.0
Steps to Reproduce
In the following snippet i am changing a super signature with a subclass, using
mypy
or any other static type checker this is mark as an error, but when the sentry decorator is added (as in the example) the error is hidden.The issue is in the following lines:
sentry-python/sentry_sdk/tracing.py
Lines 958 to 959 in 085595b
the decorator is changing the signature of both input and output to
Any
. This is not the expected behavior since the function will not actually be changed. And because the original signature expects integers, but the overriden one expects strings (which are not compatible) but when type checking the checker all it sees is "any" then because int is part of any is not giving any error when it should.This can be easily fix by using a
TypeVar
in this case, like so:Expected Result
Type checker (like mypy) complains that the signature is not correct
Actual Result
Type checker (like mypy) doesn't complain at all, the bug is hidden
The text was updated successfully, but these errors were encountered: