Skip to content

Commit c86c5e8

Browse files
marcoffeeColton Myers
andauthored
Use typing.TypeVar on decorators' type hints (#1655)
* Fixes issue #1654 Co-authored-by: Colton Myers <[email protected]>
1 parent 5b80432 commit c86c5e8

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

elasticapm/contrib/asyncio/traces.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@
3030

3131
import functools
3232
from types import TracebackType
33-
from typing import Optional, Type
33+
from typing import Optional, Type, TypeVar
3434

3535
from elasticapm.conf.constants import LABEL_RE
3636
from elasticapm.traces import SpanType, capture_span, execution_context
3737
from elasticapm.utils import get_name_from_func
3838

39+
_AnnotatedFunctionT = TypeVar("_AnnotatedFunctionT")
40+
3941

4042
class async_capture_span(capture_span):
41-
def __call__(self, func):
43+
def __call__(self, func: _AnnotatedFunctionT) -> _AnnotatedFunctionT:
4244
self.name = self.name or get_name_from_func(func)
4345

4446
@functools.wraps(func)

elasticapm/contrib/serverless/aws.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import os
3636
import platform
3737
import time
38-
from typing import Optional
38+
from typing import Optional, TypeVar
3939
from urllib.parse import urlencode
4040

4141
import elasticapm
@@ -51,6 +51,8 @@
5151

5252
COLD_START = True
5353

54+
_AnnotatedFunctionT = TypeVar("_AnnotatedFunctionT")
55+
5456

5557
class capture_serverless(object):
5658
"""
@@ -89,7 +91,7 @@ def __init__(self, name: Optional[str] = None, elasticapm_client: Optional[Clien
8991

9092
self.client_kwargs = kwargs
9193

92-
def __call__(self, func):
94+
def __call__(self, func: _AnnotatedFunctionT) -> _AnnotatedFunctionT:
9395
self.name = self.name or get_name_from_func(func)
9496

9597
@functools.wraps(func)

elasticapm/traces.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from collections import defaultdict
4040
from datetime import timedelta
4141
from types import TracebackType
42-
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union
42+
from typing import Any, Dict, List, Optional, Sequence, Tuple, Type, TypeVar, Union
4343

4444
import elasticapm
4545
from elasticapm.conf import constants
@@ -62,6 +62,7 @@
6262
execution_context = init_execution_context()
6363

6464
SpanType = Union["Span", "DroppedSpan"]
65+
_AnnotatedFunctionT = TypeVar("_AnnotatedFunctionT")
6566

6667

6768
class ChildDuration(object):
@@ -1056,7 +1057,7 @@ def __init__(
10561057
self.sync = sync
10571058
self.links = links
10581059

1059-
def __call__(self, func: Callable) -> Callable:
1060+
def __call__(self, func: _AnnotatedFunctionT) -> _AnnotatedFunctionT:
10601061
self.name = self.name or get_name_from_func(func)
10611062

10621063
@functools.wraps(func)

elasticapm/utils/compat.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
import atexit
3434
import functools
3535
import platform
36+
from typing import TypeVar
3637

38+
_AnnotatedFunctionT = TypeVar("_AnnotatedFunctionT")
3739

38-
def noop_decorator(func):
40+
41+
def noop_decorator(func: _AnnotatedFunctionT) -> _AnnotatedFunctionT:
3942
@functools.wraps(func)
4043
def wrapped(*args, **kwargs):
4144
return func(*args, **kwargs)

elasticapm/utils/deprecation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
import functools
3232
import warnings
33+
from typing import TypeVar
34+
35+
_AnnotatedFunctionT = TypeVar("_AnnotatedFunctionT")
3336

3437
# https://wiki.python.org/moin/PythonDecoratorLibrary#Smart_deprecation_warnings_.28with_valid_filenames.2C_line_numbers.2C_etc..29
3538

@@ -39,7 +42,7 @@ def deprecated(alternative=None):
3942
as deprecated. It will result in a warning being emitted
4043
when the function is used."""
4144

42-
def real_decorator(func):
45+
def real_decorator(func: _AnnotatedFunctionT) -> _AnnotatedFunctionT:
4346
@functools.wraps(func)
4447
def new_func(*args, **kwargs):
4548
msg = "Call to deprecated function {0}.".format(func.__name__)

0 commit comments

Comments
 (0)