Skip to content

Commit 8c8b204

Browse files
committed
Reformat with ruff.
1 parent da9a98d commit 8c8b204

File tree

8 files changed

+42
-41
lines changed

8 files changed

+42
-41
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
BlockedReason,
2525
Candidate,
2626
Content,
27-
ContentUnion,
28-
ContentUnionDict,
2927
ContentListUnion,
3028
ContentListUnionDict,
29+
ContentUnion,
30+
ContentUnionDict,
3131
GenerateContentConfigOrDict,
3232
GenerateContentResponse,
3333
)
@@ -539,6 +539,7 @@ def _create_instrumented_generate_content_stream(
539539
snapshot: _MethodsSnapshot, otel_wrapper: OTelWrapper
540540
):
541541
wrapped_func = snapshot.generate_content_stream
542+
542543
@functools.wraps(wrapped_func)
543544
def instrumented_generate_content_stream(
544545
self: Models,
@@ -578,6 +579,7 @@ def _create_instrumented_async_generate_content(
578579
snapshot: _MethodsSnapshot, otel_wrapper: OTelWrapper
579580
):
580581
wrapped_func = snapshot.async_generate_content
582+
581583
@functools.wraps(wrapped_func)
582584
async def instrumented_generate_content(
583585
self: AsyncModels,
@@ -618,6 +620,7 @@ def _create_instrumented_async_generate_content_stream( # pyright: ignore
618620
snapshot: _MethodsSnapshot, otel_wrapper: OTelWrapper
619621
):
620622
wrapped_func = snapshot.async_generate_content_stream
623+
621624
@functools.wraps(wrapped_func)
622625
async def instrumented_generate_content_stream(
623626
self: AsyncModels,
@@ -630,6 +633,7 @@ async def instrumented_generate_content_stream(
630633
helper = _GenerateContentInstrumentationHelper(
631634
self, otel_wrapper, model
632635
)
636+
633637
async def _internal_generator():
634638
with helper.start_span_as_current_span(
635639
model, "google.genai.AsyncModels.generate_content_stream"
@@ -650,9 +654,11 @@ async def _internal_generator():
650654
raise
651655
finally:
652656
helper.finalize_processing()
657+
653658
class _GeneratorProvider:
654659
def __aiter__(self):
655660
return _internal_generator()
661+
656662
return _GeneratorProvider()
657663

658664
return instrumented_generate_content_stream

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/common/requests_mocker.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
# Because they APIs that need to be mocked are simple enough and well documented
3333
# enough, it seems approachable to mock the requests library, instead.
3434

35-
from typing import Optional
3635
import copy
3736
import functools
3837
import http.client
3938
import io
4039
import json
40+
from typing import Optional
4141

4242
import requests
4343
import requests.sessions
@@ -127,27 +127,29 @@ def generate_response_from_dict(args):
127127
def _to_stream_response_generator(response_generators):
128128
if len(response_generators) == 1:
129129
return response_generators[0]
130+
130131
def combined_generator(args):
131132
first_response = response_generators[0](args)
132133
if first_response.status_code != 200:
133134
return first_response
134135
result = requests.Response()
135136
result.status_code = 200
136-
result.headers['content-type'] = 'application/json'
137-
result.encoding = 'utf-8'
138-
result.headers['transfer-encoding'] = 'chunked'
137+
result.headers["content-type"] = "application/json"
138+
result.encoding = "utf-8"
139+
result.headers["transfer-encoding"] = "chunked"
139140
contents = []
140141
for generator in response_generators:
141142
response = generator(args)
142143
if response.status_code != 200:
143144
continue
144145
response_json = response.json()
145146
response_json_str = json.dumps(response_json)
146-
contents.append(f'data: {response_json_str}')
147-
contents_str = '\r\n'.join(contents)
148-
full_contents = f'{contents_str}\r\n\r\n'
147+
contents.append(f"data: {response_json_str}")
148+
contents_str = "\r\n".join(contents)
149+
full_contents = f"{contents_str}\r\n\r\n"
149150
result.raw = io.BytesIO(full_contents.encode())
150151
return result
152+
151153
return combined_generator
152154

153155

@@ -188,7 +190,7 @@ def _do_send(
188190
request: requests.PreparedRequest,
189191
**kwargs,
190192
):
191-
stream=kwargs.get('stream', False)
193+
stream = kwargs.get("stream", False)
192194
if not stream:
193195
return self._do_send_non_streaming(session, request, **kwargs)
194196
return self._do_send_streaming(session, request, **kwargs)

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/nonstreaming_base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@ def generate_content(self, *args, **kwargs):
3636
def expected_function_name(self):
3737
raise NotImplementedError("Must implement 'expected_function_name'.")
3838

39-
def configure_valid_response(
40-
self,
41-
*args,
42-
**kwargs
43-
):
44-
self.requests.add_response(
45-
create_valid_response(*args, **kwargs))
39+
def configure_valid_response(self, *args, **kwargs):
40+
self.requests.add_response(create_valid_response(*args, **kwargs))
4641

4742
def test_instrumentation_does_not_break_core_functionality(self):
4843
self.configure_valid_response(response_text="Yep, it works!")

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/streaming_base.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import unittest
1716

1817
from ..common.base import TestCase
@@ -35,13 +34,8 @@ def generate_content(self, *args, **kwargs):
3534
def expected_function_name(self):
3635
raise NotImplementedError("Must implement 'expected_function_name'.")
3736

38-
def configure_valid_response(
39-
self,
40-
*args,
41-
**kwargs
42-
):
43-
self.requests.add_response(
44-
create_valid_response(*args, **kwargs))
37+
def configure_valid_response(self, *args, **kwargs):
38+
self.requests.add_response(create_valid_response(*args, **kwargs))
4539

4640
def test_instrumentation_does_not_break_core_functionality(self):
4741
self.configure_valid_response(response_text="Yep, it works!")
@@ -79,4 +73,4 @@ def test_includes_token_counts_in_span_aggregated_from_responses(self):
7973
self.otel.assert_has_span_named("generate_content gemini-2.0-flash")
8074
span = self.otel.get_span_named("generate_content gemini-2.0-flash")
8175
self.assertEqual(span.attributes["gen_ai.usage.input_tokens"], 9)
82-
self.assertEqual(span.attributes["gen_ai.usage.output_tokens"], 12)
76+
self.assertEqual(span.attributes["gen_ai.usage.output_tokens"], 12)

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/test_async_nonstreaming.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818

1919

2020
class TestGenerateContentAsyncNonstreaming(NonStreamingTestCase):
21-
2221
def generate_content(self, *args, **kwargs):
2322
return asyncio.run(
2423
self.client.aio.models.generate_content(*args, **kwargs) # pylint: disable=missing-kwoa
2524
)
2625

2726
@property
2827
def expected_function_name(self):
29-
return 'google.genai.AsyncModels.generate_content'
28+
return "google.genai.AsyncModels.generate_content"

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/test_async_streaming.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020

2121
class AsyncStreamingMixin:
22-
2322
@property
2423
def expected_function_name(self):
2524
return "google.genai.AsyncModels.generate_content_stream"
@@ -35,18 +34,22 @@ async def _generate_content_stream_helper(self, *args, **kwargs):
3534
return result
3635

3736
def generate_content_stream(self, *args, **kwargs):
38-
return asyncio.run(self._generate_content_stream_helper(*args, **kwargs))
39-
37+
return asyncio.run(
38+
self._generate_content_stream_helper(*args, **kwargs)
39+
)
4040

41-
class TestGenerateContentAsyncStreamingWithSingleResult(AsyncStreamingMixin, NonStreamingTestCase):
4241

42+
class TestGenerateContentAsyncStreamingWithSingleResult(
43+
AsyncStreamingMixin, NonStreamingTestCase
44+
):
4345
def generate_content(self, *args, **kwargs):
4446
responses = self.generate_content_stream(*args, **kwargs)
4547
self.assertEqual(len(responses), 1)
4648
return responses[0]
4749

4850

49-
class TestGenerateContentAsyncStreamingWithStreamedResults(AsyncStreamingMixin, StreamingTestCase):
50-
51+
class TestGenerateContentAsyncStreamingWithStreamedResults(
52+
AsyncStreamingMixin, StreamingTestCase
53+
):
5154
def generate_content(self, *args, **kwargs):
5255
return self.generate_content_stream(*args, **kwargs)

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/test_sync_streaming.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,30 @@
1818

1919

2020
class StreamingMixin:
21-
2221
@property
2322
def expected_function_name(self):
2423
return "google.genai.Models.generate_content_stream"
2524

2625
def generate_content_stream(self, *args, **kwargs):
2726
result = []
2827
for response in self.client.models.generate_content_stream( # pylint: disable=missing-kwoa
29-
*args, **kwargs):
28+
*args, **kwargs
29+
):
3030
result.append(response)
3131
return result
3232

3333

34-
class TestGenerateContentStreamingWithSingleResult(StreamingMixin, NonStreamingTestCase):
35-
34+
class TestGenerateContentStreamingWithSingleResult(
35+
StreamingMixin, NonStreamingTestCase
36+
):
3637
def generate_content(self, *args, **kwargs):
3738
responses = self.generate_content_stream(*args, **kwargs)
3839
self.assertEqual(len(responses), 1)
3940
return responses[0]
4041

4142

42-
class TestGenerateContentStreamingWithStreamedResults(StreamingMixin, StreamingTestCase):
43-
43+
class TestGenerateContentStreamingWithStreamedResults(
44+
StreamingMixin, StreamingTestCase
45+
):
4446
def generate_content(self, *args, **kwargs):
4547
return self.generate_content_stream(*args, **kwargs)

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ def create_valid_response(
3535
}
3636
}
3737
],
38-
}
38+
}

0 commit comments

Comments
 (0)