newrelic / newrelic/newrelic-python-agent

Support for type hints on decorators

Open
#767 4 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request
Dominant language
Python
Stars
210
Forks
137
Avg merge
4d 19h
Merged PRs (30d)
12

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the newrelic.agent decorators and the linked mypy documentation on declaring decorators. Determine how type hints can cover all agent decorators while preserving the stated Python 2.7 support, then verify that the example mismatch is caught by mypy.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.