Skip to content

Async monitor decorator makes type checking complex #2939

Closed
@kojiromike

Description

@kojiromike

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.44.1

Steps to Reproduce

  1. Create two simple synchronous functions that returns a type, where one is decorated and the second calls the first:
from sentry_sdk.crons.decorator import monitor

@monitor(monitor_slug="test")
def foo() -> int:
    return 1

def bar() -> int:
    return foo()
  1. Run mypy on both and see if the behavior is the same in v1.44.0 and v1.44.1

Expected Result

The functions are synchronous and there is no type failure in v1.44.1.

Actual Result

When testing upgrading to 1.44.1, we got a type failure from mypy that was not there using the 1.44.0 sdk version. Our function returns a dictionary, not an int, but the essentials are the same as above.

We see

Incompatible return value type (got "dict[str, list[dict[str, Any]]] | Awaitable[dict[str, list[dict[str, Any]]]]", expected "dict[str, list[dict[str, Any]]]") [return-value]

But since our function is synchronous, mypy should not be confused by the Awaitable. I think this is ultimately due to inspect.iscoroutinefunction being a runtime check that mypy does not use for type elimination.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions