Skip to content

Commit 973dda7

Browse files
authored
Convert type annotations to PEP-526 format (#5206)
1 parent 1dcf70b commit 973dda7

File tree

164 files changed

+4159
-4883
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+4159
-4883
lines changed

sentry_sdk/_compat.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515
PY311 = sys.version_info[0] == 3 and sys.version_info[1] >= 11
1616

1717

18-
def with_metaclass(meta, *bases):
19-
# type: (Any, *Any) -> Any
18+
def with_metaclass(meta: "Any", *bases: "Any") -> "Any":
2019
class MetaClass(type):
21-
def __new__(metacls, name, this_bases, d):
22-
# type: (Any, Any, Any, Any) -> Any
20+
def __new__(metacls: "Any", name: "Any", this_bases: "Any", d: "Any") -> "Any":
2321
return meta(name, bases, d)
2422

2523
return type.__new__(MetaClass, "temporary_class", (), {})
2624

2725

28-
def check_uwsgi_thread_support():
29-
# type: () -> bool
26+
def check_uwsgi_thread_support() -> bool:
3027
# We check two things here:
3128
#
3229
# 1. uWSGI doesn't run in threaded mode by default -- issue a warning if
@@ -46,8 +43,7 @@ def check_uwsgi_thread_support():
4643

4744
from sentry_sdk.consts import FALSE_VALUES
4845

49-
def enabled(option):
50-
# type: (str) -> bool
46+
def enabled(option: str) -> bool:
5147
value = opt.get(option, False)
5248
if isinstance(value, bool):
5349
return value

sentry_sdk/_init_implementation.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ class _InitGuard:
1818
"functionality, and we will remove it in the next major release."
1919
)
2020

21-
def __init__(self, client):
22-
# type: (sentry_sdk.Client) -> None
21+
def __init__(self, client: "sentry_sdk.Client") -> None:
2322
self._client = client
2423

25-
def __enter__(self):
26-
# type: () -> _InitGuard
24+
def __enter__(self) -> "_InitGuard":
2725
warnings.warn(
2826
self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE,
2927
stacklevel=2,
@@ -32,8 +30,7 @@ def __enter__(self):
3230

3331
return self
3432

35-
def __exit__(self, exc_type, exc_value, tb):
36-
# type: (Any, Any, Any) -> None
33+
def __exit__(self, exc_type: "Any", exc_value: "Any", tb: "Any") -> None:
3734
warnings.warn(
3835
self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE,
3936
stacklevel=2,
@@ -45,16 +42,14 @@ def __exit__(self, exc_type, exc_value, tb):
4542
c.close()
4643

4744

48-
def _check_python_deprecations():
49-
# type: () -> None
45+
def _check_python_deprecations() -> None:
5046
# Since we're likely to deprecate Python versions in the future, I'm keeping
5147
# this handy function around. Use this to detect the Python version used and
5248
# to output logger.warning()s if it's deprecated.
5349
pass
5450

5551

56-
def _init(*args, **kwargs):
57-
# type: (*Optional[str], **Any) -> ContextManager[Any]
52+
def _init(*args: "Optional[str]", **kwargs: "Any") -> "ContextManager[Any]":
5853
"""Initializes the SDK and optionally integrations.
5954
6055
This takes the same arguments as the client constructor.

sentry_sdk/_log_batcher.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,21 @@ class LogBatcher:
1818

1919
def __init__(
2020
self,
21-
capture_func, # type: Callable[[Envelope], None]
22-
record_lost_func, # type: Callable[..., None]
23-
):
24-
# type: (...) -> None
25-
self._log_buffer = [] # type: List[Log]
21+
capture_func: "Callable[[Envelope], None]",
22+
record_lost_func: "Callable[..., None]",
23+
) -> None:
24+
self._log_buffer: "List[Log]" = []
2625
self._capture_func = capture_func
2726
self._record_lost_func = record_lost_func
2827
self._running = True
2928
self._lock = threading.Lock()
3029

31-
self._flush_event = threading.Event() # type: threading.Event
30+
self._flush_event: "threading.Event" = threading.Event()
3231

33-
self._flusher = None # type: Optional[threading.Thread]
34-
self._flusher_pid = None # type: Optional[int]
32+
self._flusher: "Optional[threading.Thread]" = None
33+
self._flusher_pid: "Optional[int]" = None
3534

36-
def _ensure_thread(self):
37-
# type: (...) -> bool
35+
def _ensure_thread(self) -> bool:
3836
"""For forking processes we might need to restart this thread.
3937
This ensures that our process actually has that thread running.
4038
"""
@@ -66,18 +64,16 @@ def _ensure_thread(self):
6664

6765
return True
6866

69-
def _flush_loop(self):
70-
# type: (...) -> None
67+
def _flush_loop(self) -> None:
7168
while self._running:
7269
self._flush_event.wait(self.FLUSH_WAIT_TIME + random.random())
7370
self._flush_event.clear()
7471
self._flush()
7572

7673
def add(
7774
self,
78-
log, # type: Log
79-
):
80-
# type: (...) -> None
75+
log: "Log",
76+
) -> None:
8177
if not self._ensure_thread() or self._flusher is None:
8278
return None
8379

@@ -106,24 +102,20 @@ def add(
106102
if len(self._log_buffer) >= self.MAX_LOGS_BEFORE_FLUSH:
107103
self._flush_event.set()
108104

109-
def kill(self):
110-
# type: (...) -> None
105+
def kill(self) -> None:
111106
if self._flusher is None:
112107
return
113108

114109
self._running = False
115110
self._flush_event.set()
116111
self._flusher = None
117112

118-
def flush(self):
119-
# type: (...) -> None
113+
def flush(self) -> None:
120114
self._flush()
121115

122116
@staticmethod
123-
def _log_to_transport_format(log):
124-
# type: (Log) -> Any
125-
def format_attribute(val):
126-
# type: (int | float | str | bool) -> Any
117+
def _log_to_transport_format(log: "Log") -> "Any":
118+
def format_attribute(val: "int | float | str | bool") -> "Any":
127119
if isinstance(val, bool):
128120
return {"value": val, "type": "boolean"}
129121
if isinstance(val, int):
@@ -151,9 +143,7 @@ def format_attribute(val):
151143

152144
return res
153145

154-
def _flush(self):
155-
# type: (...) -> Optional[Envelope]
156-
146+
def _flush(self) -> "Optional[Envelope]":
157147
envelope = Envelope(
158148
headers={"sent_at": format_timestamp(datetime.now(timezone.utc))}
159149
)

sentry_sdk/_lru_cache.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88

99

1010
class LRUCache:
11-
def __init__(self, max_size):
12-
# type: (int) -> None
11+
def __init__(self, max_size: int) -> None:
1312
if max_size <= 0:
1413
raise AssertionError(f"invalid max_size: {max_size}")
1514
self.max_size = max_size
16-
self._data = {} # type: dict[Any, Any]
15+
self._data: "dict[Any, Any]" = {}
1716
self.hits = self.misses = 0
1817
self.full = False
1918

20-
def set(self, key, value):
21-
# type: (Any, Any) -> None
19+
def set(self, key: "Any", value: "Any") -> None:
2220
current = self._data.pop(key, _SENTINEL)
2321
if current is not _SENTINEL:
2422
self._data[key] = value
@@ -29,8 +27,7 @@ def set(self, key, value):
2927
self._data[key] = value
3028
self.full = len(self._data) >= self.max_size
3129

32-
def get(self, key, default=None):
33-
# type: (Any, Any) -> Any
30+
def get(self, key: "Any", default: "Any" = None) -> "Any":
3431
try:
3532
ret = self._data.pop(key)
3633
except KeyError:
@@ -42,6 +39,5 @@ def get(self, key, default=None):
4239

4340
return ret
4441

45-
def get_all(self):
46-
# type: () -> list[tuple[Any, Any]]
42+
def get_all(self) -> "list[tuple[Any, Any]]":
4743
return list(self._data.items())

sentry_sdk/_metrics_batcher.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,21 @@ class MetricsBatcher:
1818

1919
def __init__(
2020
self,
21-
capture_func, # type: Callable[[Envelope], None]
22-
record_lost_func, # type: Callable[..., None]
23-
):
24-
# type: (...) -> None
25-
self._metric_buffer = [] # type: List[Metric]
21+
capture_func: "Callable[[Envelope], None]",
22+
record_lost_func: "Callable[..., None]",
23+
) -> None:
24+
self._metric_buffer: "List[Metric]" = []
2625
self._capture_func = capture_func
2726
self._record_lost_func = record_lost_func
2827
self._running = True
2928
self._lock = threading.Lock()
3029

31-
self._flush_event = threading.Event() # type: threading.Event
30+
self._flush_event: "threading.Event" = threading.Event()
3231

33-
self._flusher = None # type: Optional[threading.Thread]
34-
self._flusher_pid = None # type: Optional[int]
32+
self._flusher: "Optional[threading.Thread]" = None
33+
self._flusher_pid: "Optional[int]" = None
3534

36-
def _ensure_thread(self):
37-
# type: (...) -> bool
35+
def _ensure_thread(self) -> bool:
3836
if not self._running:
3937
return False
4038

@@ -59,18 +57,16 @@ def _ensure_thread(self):
5957

6058
return True
6159

62-
def _flush_loop(self):
63-
# type: (...) -> None
60+
def _flush_loop(self) -> None:
6461
while self._running:
6562
self._flush_event.wait(self.FLUSH_WAIT_TIME + random.random())
6663
self._flush_event.clear()
6764
self._flush()
6865

6966
def add(
7067
self,
71-
metric, # type: Metric
72-
):
73-
# type: (...) -> None
68+
metric: "Metric",
69+
) -> None:
7470
if not self._ensure_thread() or self._flusher is None:
7571
return None
7672

@@ -87,24 +83,20 @@ def add(
8783
if len(self._metric_buffer) >= self.MAX_METRICS_BEFORE_FLUSH:
8884
self._flush_event.set()
8985

90-
def kill(self):
91-
# type: (...) -> None
86+
def kill(self) -> None:
9287
if self._flusher is None:
9388
return
9489

9590
self._running = False
9691
self._flush_event.set()
9792
self._flusher = None
9893

99-
def flush(self):
100-
# type: (...) -> None
94+
def flush(self) -> None:
10195
self._flush()
10296

10397
@staticmethod
104-
def _metric_to_transport_format(metric):
105-
# type: (Metric) -> Any
106-
def format_attribute(val):
107-
# type: (Union[int, float, str, bool]) -> Any
98+
def _metric_to_transport_format(metric: "Metric") -> "Any":
99+
def format_attribute(val: "Union[int, float, str, bool]") -> "Any":
108100
if isinstance(val, bool):
109101
return {"value": val, "type": "boolean"}
110102
if isinstance(val, int):
@@ -134,9 +126,7 @@ def format_attribute(val):
134126

135127
return res
136128

137-
def _flush(self):
138-
# type: (...) -> Optional[Envelope]
139-
129+
def _flush(self) -> "Optional[Envelope]":
140130
envelope = Envelope(
141131
headers={"sent_at": format_timestamp(datetime.now(timezone.utc))}
142132
)

sentry_sdk/_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def get_nowait(self):
275275

276276
# Initialize the queue representation
277277
def _init(self, maxsize):
278-
self.queue = deque() # type: Any
278+
self.queue: "Any" = deque()
279279

280280
def _qsize(self):
281281
return len(self.queue)

sentry_sdk/_types.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,27 @@ class AnnotatedValue:
1818

1919
__slots__ = ("value", "metadata")
2020

21-
def __init__(self, value, metadata):
22-
# type: (Optional[Any], Dict[str, Any]) -> None
21+
def __init__(self, value: "Optional[Any]", metadata: "Dict[str, Any]") -> None:
2322
self.value = value
2423
self.metadata = metadata
2524

26-
def __eq__(self, other):
27-
# type: (Any) -> bool
25+
def __eq__(self, other: "Any") -> bool:
2826
if not isinstance(other, AnnotatedValue):
2927
return False
3028

3129
return self.value == other.value and self.metadata == other.metadata
3230

33-
def __str__(self):
34-
# type: (AnnotatedValue) -> str
31+
def __str__(self: "AnnotatedValue") -> str:
3532
return str({"value": str(self.value), "metadata": str(self.metadata)})
3633

37-
def __len__(self):
38-
# type: (AnnotatedValue) -> int
34+
def __len__(self: "AnnotatedValue") -> int:
3935
if self.value is not None:
4036
return len(self.value)
4137
else:
4238
return 0
4339

4440
@classmethod
45-
def removed_because_raw_data(cls):
46-
# type: () -> AnnotatedValue
41+
def removed_because_raw_data(cls) -> "AnnotatedValue":
4742
"""The value was removed because it could not be parsed. This is done for request body values that are not json nor a form."""
4843
return AnnotatedValue(
4944
value="",
@@ -58,8 +53,7 @@ def removed_because_raw_data(cls):
5853
)
5954

6055
@classmethod
61-
def removed_because_over_size_limit(cls, value=""):
62-
# type: (Any) -> AnnotatedValue
56+
def removed_because_over_size_limit(cls, value: "Any" = "") -> "AnnotatedValue":
6357
"""
6458
The actual value was removed because the size of the field exceeded the configured maximum size,
6559
for example specified with the max_request_body_size sdk option.
@@ -77,8 +71,7 @@ def removed_because_over_size_limit(cls, value=""):
7771
)
7872

7973
@classmethod
80-
def substituted_because_contains_sensitive_data(cls):
81-
# type: () -> AnnotatedValue
74+
def substituted_because_contains_sensitive_data(cls) -> "AnnotatedValue":
8275
"""The actual value was removed because it contained sensitive information."""
8376
return AnnotatedValue(
8477
value=SENSITIVE_DATA_SUBSTITUTE,
@@ -116,7 +109,7 @@ def substituted_because_contains_sensitive_data(cls):
116109
class SDKInfo(TypedDict):
117110
name: str
118111
version: str
119-
packages: Sequence[Mapping[str, str]]
112+
packages: "Sequence[Mapping[str, str]]"
120113

121114
# "critical" is an alias of "fatal" recognized by Relay
122115
LogLevelStr = Literal["fatal", "critical", "error", "warning", "info", "debug"]

sentry_sdk/_werkzeug.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
# We need this function because Django does not give us a "pure" http header
4848
# dict. So we might as well use it for all WSGI integrations.
4949
#
50-
def _get_headers(environ):
51-
# type: (Dict[str, str]) -> Iterator[Tuple[str, str]]
50+
def _get_headers(environ: "Dict[str, str]") -> "Iterator[Tuple[str, str]]":
5251
"""
5352
Returns only proper HTTP headers.
5453
"""
@@ -67,8 +66,7 @@ def _get_headers(environ):
6766
# `get_host` comes from `werkzeug.wsgi.get_host`
6867
# https://github.com/pallets/werkzeug/blob/1.0.1/src/werkzeug/wsgi.py#L145
6968
#
70-
def get_host(environ, use_x_forwarded_for=False):
71-
# type: (Dict[str, str], bool) -> str
69+
def get_host(environ: "Dict[str, str]", use_x_forwarded_for: bool = False) -> str:
7270
"""
7371
Return the host for the given WSGI environment.
7472
"""

0 commit comments

Comments
 (0)