newrelic-python-agent icon indicating copy to clipboard operation
newrelic-python-agent copied to clipboard

Support for type hints on decorators

Open whwright opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe.

Usage of decorators such as @newrelic.agent.function_trace prevent mypy from checking types on those functions. Here is mypy documentation describing this issue: https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators

For example

import newrelic.agent

@newrelic.agent.function_trace()
def func_i_want_to_trace(value: str) -> bool:
    return value == "yes"

func_i_want_to_trace(1)

mypy will not catch this type mismatch

Feature Description

Type all newrelic.agent decorators as described in the mypy doc

Describe Alternatives

Here is a workaround

F = TypeVar("F", bound=Callable[..., Any])

def newrelic_agent_function_trace(func: F) -> F:
    return cast(F, newrelic.agent.function_trace()(func))

@newrelic_agent_function_trace
def func_i_want_to_trace(value: str) -> bool:
    return value == "yes"

Additional context

I am not sure if this is possible if you would like to keep supporting python 2.7

Priority

Nice to Have

whwright avatar Feb 10 '23 21:02 whwright

Hello,

The agent uses the wrapt library internally to implement all wrappers, if the signature of functions is being obstructed that would need to be directed to the wrapt library as an issue.

It sounds like this issue has already been raised unless I'm mistaken and there's not been much progress as of yet.

TimPansino avatar Feb 10 '23 22:02 TimPansino

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 22 '23 04:05 stale[bot]

https://new-relic.atlassian.net/browse/NR-451226

Newrelic-Boomi avatar Aug 13 '25 21:08 Newrelic-Boomi

Updating to say this is still an issue, and wrapt still has not made any headway on actually fixing the type hinting.

Latest attempt was here and was unsuccessful.

As an addendum, this is only really relevant to the decorator APIs. It's possible to work around this by using the context manager APIs for the same functionality. It may be less convenient but it will likely fix static type checking.

TimPansino avatar Aug 13 '25 21:08 TimPansino