Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type statsd.timed #797

Open
Meemaw opened this issue Sep 14, 2023 · 4 comments · May be fixed by #866
Open

Type statsd.timed #797

Meemaw opened this issue Sep 14, 2023 · 4 comments · May be fixed by #866
Labels
kind/feature-request Feature request related issue stale Stale - Bot reminder

Comments

@Meemaw
Copy link

Meemaw commented Sep 14, 2023

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

@Meemaw Meemaw added the kind/feature-request Feature request related issue label Sep 14, 2023
@drichardson
Copy link

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 (python/typeshed#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
Copy link

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

@drichardson
Copy link

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
        )

Copy link

github-actions bot commented Nov 8, 2023

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.

@github-actions github-actions bot added the stale Stale - Bot reminder label Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature-request Feature request related issue stale Stale - Bot reminder
Projects
None yet
2 participants