Skip to content

Commit b765831

Browse files
committed
Enforce from __future__ import annotations
1 parent 0f78816 commit b765831

File tree

60 files changed

+383
-325
lines changed

Some content is hidden

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

60 files changed

+383
-325
lines changed

exporter/opentelemetry-exporter-prometheus-remote-write/src/opentelemetry/exporter/prometheus_remote_write/__init__.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from __future__ import annotations
1415

1516
import logging
1617
import re
1718
from collections import defaultdict
1819
from itertools import chain
19-
from typing import Dict, Mapping, Sequence
20+
from typing import Mapping, Sequence
2021

2122
import requests
2223
import snappy
@@ -71,14 +72,14 @@ class PrometheusRemoteWriteMetricsExporter(MetricExporter):
7172
def __init__(
7273
self,
7374
endpoint: str,
74-
basic_auth: Dict = None,
75-
headers: Dict = None,
75+
basic_auth: dict = None,
76+
headers: dict = None,
7677
timeout: int = 30,
77-
tls_config: Dict = None,
78-
proxies: Dict = None,
78+
tls_config: dict = None,
79+
proxies: dict = None,
7980
resources_as_labels: bool = True,
80-
preferred_temporality: Dict[type, AggregationTemporality] = None,
81-
preferred_aggregation: Dict = None,
81+
preferred_temporality: dict[type, AggregationTemporality] = None,
82+
preferred_aggregation: dict = None,
8283
):
8384
self.endpoint = endpoint
8485
self.basic_auth = basic_auth
@@ -115,7 +116,7 @@ def basic_auth(self):
115116
return self._basic_auth
116117

117118
@basic_auth.setter
118-
def basic_auth(self, basic_auth: Dict):
119+
def basic_auth(self, basic_auth: dict):
119120
if basic_auth:
120121
if "username" not in basic_auth:
121122
raise ValueError("username required in basic_auth")
@@ -147,7 +148,7 @@ def tls_config(self):
147148
return self._tls_config
148149

149150
@tls_config.setter
150-
def tls_config(self, tls_config: Dict):
151+
def tls_config(self, tls_config: dict):
151152
if tls_config:
152153
new_config = {}
153154
if "ca_file" in tls_config:
@@ -170,15 +171,15 @@ def proxies(self):
170171
return self._proxies
171172

172173
@proxies.setter
173-
def proxies(self, proxies: Dict):
174+
def proxies(self, proxies: dict):
174175
self._proxies = proxies
175176

176177
@property
177178
def headers(self):
178179
return self._headers
179180

180181
@headers.setter
181-
def headers(self, headers: Dict):
182+
def headers(self, headers: dict):
182183
self._headers = headers
183184

184185
def export(
@@ -355,7 +356,7 @@ def _build_message(timeseries: Sequence[TimeSeries]) -> bytes:
355356
serialized_message = write_request.SerializeToString()
356357
return snappy.compress(serialized_message)
357358

358-
def _build_headers(self) -> Dict:
359+
def _build_headers(self) -> dict:
359360
headers = {
360361
"Content-Encoding": "snappy",
361362
"Content-Type": "application/x-protobuf",
@@ -367,7 +368,7 @@ def _build_headers(self) -> Dict:
367368
return headers
368369

369370
def _send_message(
370-
self, message: bytes, headers: Dict
371+
self, message: bytes, headers: dict
371372
) -> MetricExportResult:
372373
auth = None
373374
if self.basic_auth:

exporter/opentelemetry-exporter-richconsole/src/opentelemetry/exporter/richconsole/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@
5050
.. _Rich: https://rich.readthedocs.io/
5151
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
5252
"""
53+
5354
# pylint: disable=import-error
55+
from __future__ import annotations
5456

5557
import datetime
5658
import typing
57-
from typing import Dict, Optional
5859

5960
from rich.console import Console
6061
from rich.syntax import Syntax
@@ -150,7 +151,7 @@ class RichConsoleSpanExporter(SpanExporter):
150151

151152
def __init__(
152153
self,
153-
service_name: Optional[str] = None,
154+
service_name: str | None = None,
154155
):
155156
self.service_name = service_name
156157
self.console = Console()
@@ -165,7 +166,7 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
165166
return SpanExportResult.SUCCESS
166167

167168
@staticmethod
168-
def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> Dict[str, Tree]:
169+
def spans_to_tree(spans: typing.Sequence[ReadableSpan]) -> dict[str, Tree]:
169170
trees = {}
170171
parents = {}
171172
spans = list(spans)

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/patch.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
16-
from typing import Optional
14+
from __future__ import annotations
1715

1816
from openai import Stream
1917

@@ -201,12 +199,12 @@ def append_tool_call(self, tool_call):
201199

202200
class StreamWrapper:
203201
span: Span
204-
response_id: Optional[str] = None
205-
response_model: Optional[str] = None
206-
service_tier: Optional[str] = None
202+
response_id: str | None = None
203+
response_model: str | None = None
204+
service_tier: str | None = None
207205
finish_reasons: list = []
208-
prompt_tokens: Optional[int] = 0
209-
completion_tokens: Optional[int] = 0
206+
prompt_tokens: int | None = 0
207+
completion_tokens: int | None = 0
210208

211209
def __init__(
212210
self,

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from __future__ import annotations
1415

1516
from os import environ
16-
from typing import Mapping, Optional, Union
17+
from typing import Mapping
1718
from urllib.parse import urlparse
1819

1920
from httpx import URL
@@ -179,7 +180,7 @@ def is_streaming(kwargs):
179180
return non_numerical_value_is_set(kwargs.get("stream"))
180181

181182

182-
def non_numerical_value_is_set(value: Optional[Union[bool, str]]):
183+
def non_numerical_value_is_set(value: bool | str | None):
183184
return bool(value) and value != NOT_GIVEN
184185

185186

instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_async_chat_completions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# pylint: disable=too-many-locals
15-
16-
from typing import Optional
15+
from __future__ import annotations
1716

1817
import pytest
1918
from openai import APIConnectionError, AsyncOpenAI, NotFoundError
@@ -802,8 +801,8 @@ def assert_all_attributes(
802801
request_model: str,
803802
response_id: str = None,
804803
response_model: str = None,
805-
input_tokens: Optional[int] = None,
806-
output_tokens: Optional[int] = None,
804+
input_tokens: int | None = None,
805+
output_tokens: int | None = None,
807806
operation_name: str = "chat",
808807
server_address: str = "api.openai.com",
809808
):

instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_chat_completions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# pylint: disable=too-many-locals
15-
16-
from typing import Optional
15+
from __future__ import annotations
1716

1817
import pytest
1918
from openai import APIConnectionError, NotFoundError, OpenAI
@@ -779,8 +778,8 @@ def assert_all_attributes(
779778
request_model: str,
780779
response_id: str = None,
781780
response_model: str = None,
782-
input_tokens: Optional[int] = None,
783-
output_tokens: Optional[int] = None,
781+
input_tokens: int | None = None,
782+
output_tokens: int | None = None,
784783
operation_name: str = "chat",
785784
server_address: str = "api.openai.com",
786785
):

instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/callback_decorator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Any, Callable, Optional
14+
from __future__ import annotations
15+
16+
from typing import Any, Callable
1517

1618
from aio_pika import Queue
1719
from aio_pika.abc import AbstractIncomingMessage
@@ -28,7 +30,7 @@ def __init__(self, tracer: Tracer, queue: Queue):
2830
self._tracer = tracer
2931
self._queue = queue
3032

31-
def _get_span(self, message: AbstractIncomingMessage) -> Optional[Span]:
33+
def _get_span(self, message: AbstractIncomingMessage) -> Span | None:
3234
builder = SpanBuilder(self._tracer)
3335
builder.set_as_consumer()
3436
builder.set_operation(MessagingOperationValues.RECEIVE)

instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/publish_decorator.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Callable, Optional
14+
from __future__ import annotations
15+
16+
from typing import Callable
1517

1618
import aiormq
1719
from aio_pika import Exchange
@@ -29,7 +31,7 @@ def __init__(self, tracer: Tracer, exchange: Exchange):
2931

3032
def _get_publish_span(
3133
self, message: AbstractMessage, routing_key: str
32-
) -> Optional[Span]:
34+
) -> Span | None:
3335
builder = SpanBuilder(self._tracer)
3436
builder.set_as_producer()
3537
builder.set_destination(f"{self._exchange.name},{routing_key}")
@@ -40,7 +42,7 @@ def _get_publish_span(
4042
def decorate(self, publish: Callable) -> Callable:
4143
async def decorated_publish(
4244
message: AbstractMessage, routing_key: str, **kwargs
43-
) -> Optional[aiormq.abc.ConfirmationFrameType]:
45+
) -> aiormq.abc.ConfirmationFrameType | None:
4446
span = self._get_publish_span(message, routing_key)
4547
if not span:
4648
return await publish(message, routing_key, **kwargs)

instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/span_builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Optional
14+
from __future__ import annotations
1515

1616
from aio_pika.abc import AbstractChannel, AbstractMessage
1717

@@ -77,7 +77,7 @@ def set_message(self, message: AbstractMessage):
7777
properties.correlation_id
7878
)
7979

80-
def build(self) -> Optional[Span]:
80+
def build(self) -> Span | None:
8181
if not is_instrumentation_enabled():
8282
return None
8383
if self._operation:

instrumentation/opentelemetry-instrumentation-aio-pika/tests/test_publish_decorator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from __future__ import annotations
15+
1416
import asyncio
15-
from typing import Type
1617
from unittest import TestCase, mock, skipIf
1718
from unittest.mock import MagicMock
1819

@@ -70,7 +71,7 @@ def test_get_publish_span(self):
7071
attributes=self.EXPECTED_ATTRIBUTES,
7172
)
7273

73-
def _test_publish(self, exchange_type: Type[Exchange]):
74+
def _test_publish(self, exchange_type: type[Exchange]):
7475
exchange = exchange_type(CONNECTION_7, CHANNEL_7, EXCHANGE_NAME)
7576
with mock.patch.object(
7677
PublishDecorator, "_get_publish_span"
@@ -149,7 +150,7 @@ def test_get_publish_span(self):
149150
attributes=self.EXPECTED_ATTRIBUTES,
150151
)
151152

152-
def _test_publish(self, exchange_type: Type[Exchange]):
153+
def _test_publish(self, exchange_type: type[Exchange]):
153154
exchange = exchange_type(CONNECTION_8, CHANNEL_8, EXCHANGE_NAME)
154155
with mock.patch.object(
155156
PublishDecorator, "_get_publish_span"

0 commit comments

Comments
 (0)