Skip to content

Commit 1912da9

Browse files
use ParamSpec to enable intellisense with distributed_trace decorators (Azure#22891)
* use ParamSpec in distributed_trace decorators to enable intellisense in py3.10+ * use backported ParamSpec from typings * add dependency on typing_extensions * fix name, remove from dev_reqs * fix typing-extensions version * update changelog
1 parent a10c604 commit 1912da9

File tree

6 files changed

+23
-31
lines changed

6 files changed

+23
-31
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.23.0 (Unreleased)
44

55
### Features Added
6+
- Improve intellisense type hinting for service client methods #22891
67

78
- Add a case insensitive dict `case_insensitive_dict` in `azure.core.utils`. #23206
89

sdk/core/azure-core/azure/core/tracing/decorator.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,30 @@
2727

2828
import functools
2929

30-
from typing import overload
31-
30+
from typing import Callable, Any, TypeVar, overload
31+
from typing_extensions import ParamSpec
3232
from .common import change_context, get_function_and_class_name
3333
from ..settings import settings
3434

35-
try:
36-
from typing import TYPE_CHECKING
37-
except ImportError:
38-
TYPE_CHECKING = False
39-
40-
if TYPE_CHECKING:
41-
from typing import Callable, Dict, Optional, Any, TypeVar
4235

43-
T = TypeVar("T")
36+
P = ParamSpec("P")
37+
T = TypeVar("T")
4438

4539

4640
@overload
47-
def distributed_trace(__func):
48-
# type: (Callable[..., T]) -> Callable[..., T]
41+
def distributed_trace(__func: Callable[P, T]) -> Callable[P, T]:
4942
pass
5043

5144

5245
@overload
53-
def distributed_trace(**kwargs): # pylint:disable=function-redefined,unused-argument
54-
# type: (**Any) -> Callable[[Callable[..., T]], Callable[..., T]]
46+
def distributed_trace( # pylint:disable=function-redefined
47+
**kwargs: Any, # pylint:disable=unused-argument
48+
) -> Callable[[Callable[P, T]], Callable[P, T]]:
5549
pass
5650

5751

5852
def distributed_trace( # pylint:disable=function-redefined
59-
__func=None, # type: Callable[..., T]
60-
**kwargs # type: Any
53+
__func: Callable[P, T] = None, **kwargs: Any
6154
):
6255
"""Decorator to apply to function to get traced automatically.
6356
@@ -69,12 +62,9 @@ def distributed_trace( # pylint:disable=function-redefined
6962
name_of_span = kwargs.pop("name_of_span", None)
7063
tracing_attributes = kwargs.pop("tracing_attributes", {})
7164

72-
def decorator(func):
73-
# type: (Callable[..., T]) -> Callable[..., T]
74-
65+
def decorator(func: Callable[P, T]) -> Callable[P, T]:
7566
@functools.wraps(func)
76-
def wrapper_use_tracer(*args, **kwargs):
77-
# type: (*Any, **Any) -> T
67+
def wrapper_use_tracer(*args: Any, **kwargs: Any) -> T:
7868
merge_span = kwargs.pop("merge_span", False)
7969
passed_in_parent = kwargs.pop("parent_span", None)
8070

sdk/core/azure-core/azure/core/tracing/decorator_async.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,31 @@
2727

2828
import functools
2929

30-
from typing import Awaitable, Callable, Dict, Optional, Any, TypeVar, overload
31-
30+
from typing import Awaitable, Callable, Any, TypeVar, overload
31+
from typing_extensions import ParamSpec
3232
from .common import change_context, get_function_and_class_name
3333
from ..settings import settings
3434

35-
35+
P = ParamSpec("P")
3636
T = TypeVar("T")
3737

3838

3939
@overload
4040
def distributed_trace_async(
41-
__func: Callable[..., Awaitable[T]]
42-
) -> Callable[..., Awaitable[T]]:
41+
__func: Callable[P, Awaitable[T]]
42+
) -> Callable[P, Awaitable[T]]:
4343
pass
4444

4545

4646
@overload
4747
def distributed_trace_async( # pylint:disable=function-redefined
48-
**kwargs: Any # pylint:disable=unused-argument
49-
) -> Callable[[Callable[..., Awaitable[T]]], Callable[..., Awaitable[T]]]:
48+
**kwargs: Any, # pylint:disable=unused-argument
49+
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:
5050
pass
5151

5252

5353
def distributed_trace_async( # pylint:disable=function-redefined
54-
__func: Callable[..., Awaitable[T]] = None, **kwargs: Any
54+
__func: Callable[P, Awaitable[T]] = None, **kwargs: Any
5555
):
5656
"""Decorator to apply to function to get traced automatically.
5757
@@ -63,7 +63,7 @@ def distributed_trace_async( # pylint:disable=function-redefined
6363
name_of_span = kwargs.pop("name_of_span", None)
6464
tracing_attributes = kwargs.pop("tracing_attributes", {})
6565

66-
def decorator(func: Callable[..., Awaitable[T]]) -> Callable[..., Awaitable[T]]:
66+
def decorator(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]:
6767
@functools.wraps(func)
6868
async def wrapper_use_tracer(*args: Any, **kwargs: Any) -> T:
6969
merge_span = kwargs.pop("merge_span", False)

sdk/core/azure-core/dev_requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
trio
22
aiohttp>=3.0
3-
typing_extensions>=3.7.2
43
opencensus>=0.6.0
54
opencensus-ext-azure
65
opencensus-ext-threading

sdk/core/azure-core/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@
6969
install_requires=[
7070
'requests>=2.18.4',
7171
'six>=1.11.0',
72+
"typing-extensions>=4.0.1"
7273
],
7374
)

shared_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ isodate>=0.6.0
129129
avro>=1.11.0
130130
pyjwt>=1.7.1
131131
chardet<5,>=3.0.2
132+
#override azure-core typing-extensions>=4.0.1
132133
#override azure-search-documents typing-extensions>=3.7.4.3
133134
#override azure azure-keyvault~=1.0
134135
#override azure-mgmt-core azure-core<2.0.0,>=1.15.0

0 commit comments

Comments
 (0)