38
38
from typing import (
39
39
Any ,
40
40
Callable ,
41
+ cast ,
41
42
ContextManager ,
42
43
Dict ,
43
44
Iterator ,
44
45
List ,
45
46
NoReturn ,
46
47
Optional ,
48
+ overload ,
47
49
ParamSpec ,
48
50
Set ,
49
51
Tuple ,
@@ -1631,9 +1633,39 @@ def reraise(tp, value, tb=None):
1631
1633
raise value
1632
1634
1633
1635
1636
+ def _no_op (* _a , ** _k ):
1637
+ # type: (*Any, **Any) -> None
1638
+ """No-op function for ensure_integration_enabled."""
1639
+ pass
1640
+
1641
+
1642
+ async def _no_op_async (* _a , ** _k ):
1643
+ # type: (*Any, **Any) -> None
1644
+ """No-op function for ensure_integration_enabled_async."""
1645
+ pass
1646
+
1647
+
1648
+ if TYPE_CHECKING :
1649
+
1650
+ @overload
1651
+ def ensure_integration_enabled (
1652
+ integration , # type: type[sentry_sdk.integrations.Integration]
1653
+ original_function , # type: Callable[P, R]
1654
+ ):
1655
+ # type: (...) -> Callable[[Callable[P, R]], Callable[P, R]]
1656
+ ...
1657
+
1658
+ @overload
1659
+ def ensure_integration_enabled (
1660
+ integration , # type: type[sentry_sdk.integrations.Integration]
1661
+ ):
1662
+ # type: (...) -> Callable[[Callable[P, None]], Callable[P, None]]
1663
+ ...
1664
+
1665
+
1634
1666
def ensure_integration_enabled (
1635
1667
integration , # type: type[sentry_sdk.integrations.Integration]
1636
- original_function , # type: Callable[P, R]
1668
+ original_function = _no_op , # type: Union[ Callable[P, R], Callable[P, None] ]
1637
1669
):
1638
1670
# type: (...) -> Callable[[Callable[P, R]], Callable[P, R]]
1639
1671
"""
@@ -1657,25 +1689,51 @@ def patch_my_function():
1657
1689
return my_function()
1658
1690
```
1659
1691
"""
1692
+ if TYPE_CHECKING :
1693
+ # Type hint to ensure the default function has the right typing. The overloads
1694
+ # ensure the default _no_op function is only used when R is None.
1695
+ original_function = cast (Callable [P , R ], original_function )
1660
1696
1661
1697
def patcher (sentry_patched_function ):
1662
1698
# type: (Callable[P, R]) -> Callable[P, R]
1663
- @wraps (original_function )
1664
1699
def runner (* args : "P.args" , ** kwargs : "P.kwargs" ):
1665
1700
# type: (...) -> R
1666
1701
if sentry_sdk .get_client ().get_integration (integration ) is None :
1667
1702
return original_function (* args , ** kwargs )
1668
1703
1669
1704
return sentry_patched_function (* args , ** kwargs )
1670
1705
1671
- return runner
1706
+ if original_function is _no_op :
1707
+ return wraps (sentry_patched_function )(runner )
1708
+
1709
+ return wraps (original_function )(runner )
1672
1710
1673
1711
return patcher
1674
1712
1675
1713
1676
- def ensure_integration_enabled_async (
1714
+ if TYPE_CHECKING :
1715
+
1716
+ # mypy has some trouble with the overloads, hence the ignore[no-overload-impl]
1717
+ @overload # type: ignore[no-overload-impl]
1718
+ def ensure_integration_enabled_async (
1719
+ integration , # type: type[sentry_sdk.integrations.Integration]
1720
+ original_function , # type: Callable[P, Awaitable[R]]
1721
+ ):
1722
+ # type: (...) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]
1723
+ ...
1724
+
1725
+ @overload
1726
+ def ensure_integration_enabled_async (
1727
+ integration , # type: type[sentry_sdk.integrations.Integration]
1728
+ ):
1729
+ # type: (...) -> Callable[[Callable[P, Awaitable[None]]], Callable[P, Awaitable[None]]]
1730
+ ...
1731
+
1732
+
1733
+ # The ignore[no-redef] also needed because mypy is struggling with these overloads.
1734
+ def ensure_integration_enabled_async ( # type: ignore[no-redef]
1677
1735
integration , # type: type[sentry_sdk.integrations.Integration]
1678
- original_function , # type: Callable[P, Awaitable[R]]
1736
+ original_function = _no_op_async , # type: Union[ Callable[P, Awaitable[R]], Callable[P, Awaitable[None] ]]
1679
1737
):
1680
1738
# type: (...) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]
1681
1739
"""
@@ -1684,17 +1742,24 @@ def ensure_integration_enabled_async(
1684
1742
Please refer to the `ensure_integration_enabled` documentation for more information.
1685
1743
"""
1686
1744
1745
+ if TYPE_CHECKING :
1746
+ # Type hint to ensure the default function has the right typing. The overloads
1747
+ # ensure the default _no_op function is only used when R is None.
1748
+ original_function = cast (Callable [P , Awaitable [R ]], original_function )
1749
+
1687
1750
def patcher (sentry_patched_function ):
1688
1751
# type: (Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]
1689
- @wraps (original_function )
1690
1752
async def runner (* args : "P.args" , ** kwargs : "P.kwargs" ):
1691
1753
# type: (...) -> R
1692
1754
if sentry_sdk .get_client ().get_integration (integration ) is None :
1693
1755
return await original_function (* args , ** kwargs )
1694
1756
1695
1757
return await sentry_patched_function (* args , ** kwargs )
1696
1758
1697
- return runner
1759
+ if original_function is _no_op_async :
1760
+ return wraps (sentry_patched_function )(runner )
1761
+
1762
+ return wraps (original_function )(runner )
1698
1763
1699
1764
return patcher
1700
1765
0 commit comments