Skip to content

Typing issue #2460

Closed
Closed

Description

@agusdmb

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.

import sentry_sdk


class A:
    def foo(self, bar: int) -> int:
        return bar


class B(A):
    @sentry_sdk.trace
    def foo(self, bar: str) -> str:
        return bar

The issue is in the following lines:

def trace(func=None):
# type: (Any) -> Any

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:

from typing import TypeVar

T = TypeVar("T")

def trace(func=None):
    # type: (T) -> T

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

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Oct 20, 2023
  2. sentrivana commented on Oct 23, 2023

    @sentrivana
    Collaborator

    Hey @agusdmb, thanks for reporting this. I imagine the challenge here (and maybe the reason why we went with Any here) is to do this in a way that's compatible with all Python versions we currently support (2.7, 3.5-3.11).

  3. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀on Oct 23, 2023
  4. agusdmb commented on Oct 23, 2023

    @agusdmb
    Author

    Hey @agusdmb, thanks for reporting this. I imagine the challenge here (and maybe the reason why we went with Any here) is to do this in a way that's compatible with all Python versions we currently support (2.7, 3.5-3.11).

    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

  5. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Oct 23, 2023
  6. agusdmb commented on Oct 23, 2023

    @agusdmb
    Author

    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

  7. sentrivana commented on Oct 23, 2023

    @sentrivana
    Collaborator

    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:

    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 TypeVar suggestion sounds good!

    let me know if im missing something or if you need anything else from me.

    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.

  8. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀on Oct 23, 2023
  9. agusdmb commented on Oct 23, 2023

    @agusdmb
    Author

    @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

  10. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Oct 23, 2023
  11. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀on Oct 24, 2023
  12. added a commit that references this issue on Jan 16, 2024
    2f05ccb
  13. ghost added on Aug 13, 2025
  14. MerlinVeritas commented on Nov 28, 2025

    @MerlinVeritas

    I get that the project deliberately go with type comments instead of annotations for the support of old Python versions, but mypy (I'm trying the latest, 1.18.2) seems to be ignoring the type comments added by #2633 ? Any ideas?

    The risk can be very high, because adding @trace suddenly silences mypy on all code that directly or indirectly calls the function, without the developer's awareness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions