DataDog / datadogpy

The Datadog Python library
https://datadoghq.com/
Other
605 stars 303 forks source link

Type statsd.timed #797

Open Meemaw opened 10 months ago

Meemaw commented 10 months ago

Note: If you have a feature request, you should contact support so the request can be properly tracked.

Is your feature request related to a problem? Please describe. @statsd.timed decorator is missing type hints, which results in a decorated function losing all of its type information.

https://github.com/DataDog/datadogpy/blob/master/datadog/dogstatsd/base.py#L801

Describe the solution you'd like @statsd.timed decorator provides proper type hints, which correctly preserver the decorated function argument types.

Describe alternatives you've considered N/A

Additional context N/A

drichardson commented 9 months ago

Yes please. Whatnot is running into this now as well and are resorting to silly things like Sub-classing DogStatsd to workaround it. I was about to open up an issue with typeshed to add types (https://github.com/python/typeshed/pull/10843) but it got closed because supposedly datadogpy already supports types. Sure enough, I saw that some very basic typing is supported which is nice, but the problem with statsd.timed is really bad because it erases types we have on our own functions. That is, it does not only lack type hints itself, but it essentially removes them from our own code as well.

drichardson commented 9 months ago

I've submitted a support ticket about this issue on behalf of Whatnot here https://help.datadoghq.com/hc/en-us/requests/1381023

drichardson commented 9 months ago

Here is our current workaround.

from collections.abc import Callable
from typing import ParamSpec
from typing import TypeVar

from datadog import DogStatsd as _DogStatsd

P = ParamSpec("P")
T = TypeVar("T")

# This is a subclass of DogStatsd to make sure that all related decorators
# preserve the function type. The built in decorators will remove the function type
# and any type error will not be caught by mypy.
class DogStatsd(_DogStatsd):
    def timed(
        self,
        metric: str | None = None,
        tags: list[str] | None = None,
        sample_rate: float | None = None,
        use_ms: bool | None = None,
    ) -> Callable[[Callable[P, T]], Callable[P, T]]:
        return super().timed(
            metric=metric, tags=tags, sample_rate=sample_rate, use_ms=use_ms
        )
github-actions[bot] commented 8 months ago

Thanks for your contribution!

This issue has been automatically marked as stale because it has not had activity in the last 30 days. Note that the issue will not be automatically closed, but this notification will remind us to investigate why there's been inactivity. Thank you for participating in the Datadog open source community.

If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of this project.

  2. Comment that the issue is still reproducible and include updated details requested in the issue template.