Skip to content

Commit d913572

Browse files
authored
Migrate more type comments to annotations (#4651)
Unsure if the types in `tests` are any good, but might as well make them into annotations
1 parent 93f3f25 commit d913572

File tree

17 files changed

+57
-94
lines changed

17 files changed

+57
-94
lines changed

scripts/build_aws_lambda_layer.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import subprocess
44
import sys
55
import tempfile
6-
from typing import TYPE_CHECKING
6+
from typing import Optional
77

88
from sentry_sdk.consts import VERSION as SDK_VERSION
99

10-
if TYPE_CHECKING:
11-
from typing import Optional
1210

1311
DIST_PATH = "dist" # created by "make dist" that is called by "make aws-lambda-layer"
1412
PYTHON_SITE_PACKAGES = "python" # see https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
@@ -17,10 +15,9 @@
1715
class LayerBuilder:
1816
def __init__(
1917
self,
20-
base_dir, # type: str
21-
out_zip_filename=None, # type: Optional[str]
22-
):
23-
# type: (...) -> None
18+
base_dir: str,
19+
out_zip_filename: Optional[str] = None,
20+
) -> None:
2421
self.base_dir = base_dir
2522
self.python_site_packages = os.path.join(self.base_dir, PYTHON_SITE_PACKAGES)
2623
self.out_zip_filename = (
@@ -29,12 +26,10 @@ def __init__(
2926
else out_zip_filename
3027
)
3128

32-
def make_directories(self):
33-
# type: (...) -> None
29+
def make_directories(self) -> None:
3430
os.makedirs(self.python_site_packages)
3531

36-
def install_python_packages(self):
37-
# type: (...) -> None
32+
def install_python_packages(self) -> None:
3833
# Install requirements for Lambda Layer (these are more limited than the SDK requirements,
3934
# because Lambda does not support the newest versions of some packages)
4035
subprocess.check_call(
@@ -68,8 +63,7 @@ def install_python_packages(self):
6863
check=True,
6964
)
7065

71-
def create_init_serverless_sdk_package(self):
72-
# type: (...) -> None
66+
def create_init_serverless_sdk_package(self) -> None:
7367
"""
7468
Method that creates the init_serverless_sdk pkg in the
7569
sentry-python-serverless zip
@@ -84,8 +78,7 @@ def create_init_serverless_sdk_package(self):
8478
"scripts/init_serverless_sdk.py", f"{serverless_sdk_path}/__init__.py"
8579
)
8680

87-
def zip(self):
88-
# type: (...) -> None
81+
def zip(self) -> None:
8982
subprocess.run(
9083
[
9184
"zip",

scripts/init_serverless_sdk.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99
import os
1010
import sys
1111
import re
12+
from typing import Any
1213

1314
import sentry_sdk
1415
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
1516

16-
from typing import TYPE_CHECKING
17-
18-
if TYPE_CHECKING:
19-
from typing import Any
20-
2117

2218
# Configure Sentry SDK
2319
sentry_sdk.init(
@@ -70,8 +66,7 @@ def get_lambda_handler(self):
7066
return getattr(self.lambda_function_module, self.handler_name)
7167

7268

73-
def sentry_lambda_handler(event, context):
74-
# type: (Any, Any) -> None
69+
def sentry_lambda_handler(event: Any, context: Any) -> None:
7570
"""
7671
Handler function that invokes a lambda handler which path is defined in
7772
environment variables as "SENTRY_INITIAL_HANDLER"

sentry_sdk/api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,5 @@ def end_session() -> None:
339339

340340

341341
@scopemethod
342-
def set_transaction_name(name, source=None):
343-
# type: (str, Optional[str]) -> None
342+
def set_transaction_name(name: str, source: Optional[str] = None) -> None:
344343
return get_current_scope().set_transaction_name(name, source)

sentry_sdk/integrations/django/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,7 @@ def _set_db_data(span: Span, cursor_or_db: Any) -> None:
712712
span.set_attribute(SPANDATA.SERVER_SOCKET_ADDRESS, server_socket_address)
713713

714714

715-
def add_template_context_repr_sequence():
716-
# type: () -> None
715+
def add_template_context_repr_sequence() -> None:
717716
try:
718717
from django.template.context import BaseContext
719718

sentry_sdk/serializer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ def add_global_repr_processor(processor: ReprProcessor) -> None:
5353
global_repr_processors.append(processor)
5454

5555

56-
sequence_types = [Sequence, Set] # type: List[type]
56+
sequence_types: list[type] = [Sequence, Set]
5757

5858

59-
def add_repr_sequence_type(ty):
60-
# type: (type) -> None
59+
def add_repr_sequence_type(ty: type) -> None:
6160
sequence_types.append(ty)
6261

6362

sentry_sdk/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,12 +1875,12 @@ def set_thread_info_from_span(
18751875
data[SPANDATA.THREAD_NAME] = span.get_attribute(SPANDATA.THREAD_NAME)
18761876

18771877

1878-
def safe_serialize(data):
1879-
# type: (Any) -> str
1878+
def safe_serialize(data: Any) -> str:
18801879
"""Safely serialize to a readable string."""
18811880

1882-
def serialize_item(item):
1883-
# type: (Any) -> Union[str, dict[Any, Any], list[Any], tuple[Any, ...]]
1881+
def serialize_item(
1882+
item: Any,
1883+
) -> Union[str, dict[Any, Any], list[Any], tuple[Any, ...]]:
18841884
if callable(item):
18851885
try:
18861886
module = getattr(item, "__module__", None)

tests/integrations/chalice/test_chalice.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
from pytest_chalice.handlers import RequestHandler
1111

1212

13-
def _generate_lambda_context(self):
13+
def _generate_lambda_context(self) -> LambdaContext:
1414
# Monkeypatch of the function _generate_lambda_context
1515
# from the class LocalGateway
1616
# for mock the timeout
17-
# type: () -> LambdaContext
1817
if self._config.lambda_timeout is None:
1918
timeout = 10 * 1000
2019
else:

tests/integrations/django/test_middleware.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from sentry_sdk.integrations.django.middleware import _wrap_middleware
66

77

8-
def _sync_capable_middleware_factory(sync_capable):
9-
# type: (Optional[bool]) -> type
8+
def _sync_capable_middleware_factory(sync_capable: Optional[bool]) -> type:
109
"""Create a middleware class with a sync_capable attribute set to the value passed to the factory.
1110
If the factory is called with None, the middleware class will not have a sync_capable attribute.
1211
"""

tests/integrations/langchain/test_langchain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def get_word_length(word: str) -> int:
3434
return len(word)
3535

3636

37-
global stream_result_mock # type: Mock
38-
global llm_type # type: str
37+
stream_result_mock: Mock
38+
llm_type: str
3939

4040

4141
class MockOpenAI(ChatOpenAI):

tests/integrations/sanic/test_sanic.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import random
55
import sys
6+
from typing import Any, Iterable, Optional, Container
67
from unittest.mock import Mock
78

89
import pytest
@@ -26,11 +27,6 @@
2627
except ImportError:
2728
ReusableClient = None
2829

29-
from typing import TYPE_CHECKING
30-
31-
if TYPE_CHECKING:
32-
from collections.abc import Iterable, Container
33-
from typing import Any, Optional
3430

3531
SANIC_VERSION = tuple(map(int, SANIC_VERSION_RAW.split(".")))
3632
PERFORMANCE_SUPPORTED = SANIC_VERSION >= (21, 9)
@@ -341,14 +337,13 @@ class TransactionTestConfig:
341337

342338
def __init__(
343339
self,
344-
integration_args,
345-
url,
346-
expected_status,
347-
expected_transaction_name,
348-
expected_source=None,
349-
has_transaction_event=True,
350-
):
351-
# type: (Iterable[Optional[Container[int]]], str, int, Optional[str], Optional[str], bool) -> None
340+
integration_args: Iterable[Optional[Container[int]]],
341+
url: str,
342+
expected_status: int,
343+
expected_transaction_name: Optional[str],
344+
expected_source: Optional[str] = None,
345+
has_transaction_event: bool = True,
346+
) -> None:
352347
"""
353348
expected_transaction_name of None indicates we expect to not receive a transaction
354349
"""
@@ -408,8 +403,9 @@ def __init__(
408403
),
409404
],
410405
)
411-
def test_transactions(test_config, sentry_init, app, capture_events):
412-
# type: (TransactionTestConfig, Any, Any, Any) -> None
406+
def test_transactions(
407+
test_config: TransactionTestConfig, sentry_init: Any, app: Any, capture_events: Any
408+
) -> None:
413409

414410
# Init the SanicIntegration with the desired arguments
415411
sentry_init(

0 commit comments

Comments
 (0)