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