Skip to content

Commit e612547

Browse files
committed
Fixed ruff
1 parent 6b76a8e commit e612547

File tree

2 files changed

+61
-43
lines changed

2 files changed

+61
-43
lines changed

opentelemetry-api/src/opentelemetry/attributes/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
from collections import OrderedDict
1818
from collections.abc import MutableMapping
1919
from typing import Mapping, Optional, Sequence, Tuple, Union
20+
2021
from opentelemetry.util import types
2122

2223
# Optional Django imports - only available if Django is installed
2324
try:
2425
from django.core.handlers.wsgi import WSGIRequest
26+
2527
_HAS_DJANGO = True
2628
except ImportError:
2729
WSGIRequest = None # type: ignore
@@ -134,14 +136,20 @@ def _clean_extended_attribute_value(
134136
if max_len is not None and isinstance(value, str):
135137
value = value[:max_len]
136138
return value
137-
138-
if _HAS_DJANGO and WSGIRequest is not None and isinstance(value, WSGIRequest):
139+
140+
if (
141+
_HAS_DJANGO
142+
and WSGIRequest is not None
143+
and isinstance(value, WSGIRequest)
144+
):
139145
wsgi_data = {
140146
"method": getattr(value, "method", None),
141147
"path": getattr(value, "path", None),
142148
"path_info": getattr(value, "path_info", None),
143149
"content_type": getattr(value, "content_type", None),
144-
"user": str(getattr(value, "user", None)) if hasattr(value, "user") else None,
150+
"user": str(getattr(value, "user", None))
151+
if hasattr(value, "user")
152+
else None,
145153
}
146154
return {k: v for k, v in wsgi_data.items() if v is not None}
147155

opentelemetry-api/tests/attributes/test_attributes.py

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@
1313
# limitations under the License.
1414

1515
# type: ignore
16-
16+
import io
1717
import unittest
1818
from typing import MutableSequence
1919

20+
# Optional Django imports for testing
21+
try:
22+
from django.conf import settings
23+
from django.core.handlers.wsgi import WSGIRequest
24+
25+
_HAS_DJANGO = True
26+
except ImportError:
27+
WSGIRequest = None # type: ignore
28+
settings = None # type: ignore
29+
_HAS_DJANGO = False
30+
2031
from opentelemetry.attributes import (
2132
BoundedAttributes,
2233
_clean_attribute,
@@ -181,71 +192,70 @@ def test_mapping(self):
181192
self.assertEqual(
182193
_clean_extended_attribute("headers", mapping, None), expected
183194
)
184-
195+
185196
def test_wsgi_request_attribute(self):
186197
# Test WSGIRequest type validation
187198
try:
188-
from django.core.handlers.wsgi import WSGIRequest
189-
from django.conf import settings
190-
import io
191-
192199
# Configure Django settings if not already configured
193200
if not settings.configured:
194201
settings.configure(
195202
DEBUG=True,
196-
SECRET_KEY='test-secret-key',
203+
SECRET_KEY="test-secret-key",
197204
USE_TZ=True,
198205
ROOT_URLCONF=[],
199206
MIDDLEWARE=[],
200207
)
201-
208+
202209
# Create a minimal WSGI environ dict
203210
environ = {
204-
'REQUEST_METHOD': 'GET',
205-
'PATH_INFO': '/test',
206-
'QUERY_STRING': '',
207-
'CONTENT_TYPE': '',
208-
'CONTENT_LENGTH': '',
209-
'HTTP_HOST': 'testserver',
210-
'wsgi.version': (1, 0),
211-
'wsgi.url_scheme': 'http',
212-
'wsgi.input': io.StringIO(),
213-
'wsgi.errors': io.StringIO(),
214-
'wsgi.multithread': False,
215-
'wsgi.multiprocess': False,
216-
'wsgi.run_once': False,
217-
'SERVER_NAME': 'testserver',
218-
'SERVER_PORT': '80',
211+
"REQUEST_METHOD": "GET",
212+
"PATH_INFO": "/test",
213+
"QUERY_STRING": "",
214+
"CONTENT_TYPE": "",
215+
"CONTENT_LENGTH": "",
216+
"HTTP_HOST": "testserver",
217+
"wsgi.version": (1, 0),
218+
"wsgi.url_scheme": "http",
219+
"wsgi.input": io.StringIO(),
220+
"wsgi.errors": io.StringIO(),
221+
"wsgi.multithread": False,
222+
"wsgi.multiprocess": False,
223+
"wsgi.run_once": False,
224+
"SERVER_NAME": "testserver",
225+
"SERVER_PORT": "80",
219226
}
220-
227+
221228
# Create a WSGIRequest object
222229
wsgi_request = WSGIRequest(environ)
223-
230+
224231
# Test that WSGIRequest gets cleaned to a dictionary format
225232
expected_cleaned = {
226-
'method': 'GET',
227-
'path': '/test',
228-
'path_info': '/test',
229-
'content_type': ''
233+
"method": "GET",
234+
"path": "/test",
235+
"path_info": "/test",
236+
"content_type": "",
230237
}
231-
232-
# Test WSGIRequest cleaning directly
233-
from opentelemetry.attributes import _clean_extended_attribute
234-
cleaned_value = _clean_extended_attribute("request", wsgi_request, None)
238+
239+
cleaned_value = _clean_extended_attribute(
240+
"request", wsgi_request, None
241+
)
235242
self.assertEqual(cleaned_value, expected_cleaned)
236-
237-
# Test WSGIRequest in sequences - should be cleaned to dict
238-
cleaned_sequence = _clean_extended_attribute("requests", [wsgi_request], None)
243+
244+
cleaned_sequence = _clean_extended_attribute(
245+
"requests", [wsgi_request], None
246+
)
239247
self.assertEqual(cleaned_sequence, (expected_cleaned,))
240-
241-
# Test WSGIRequest in mappings - should be cleaned to dict
242-
cleaned_mapping = _clean_extended_attribute("data", {"request": wsgi_request}, None)
248+
249+
cleaned_mapping = _clean_extended_attribute(
250+
"data", {"request": wsgi_request}, None
251+
)
243252
self.assertEqual(cleaned_mapping, {"request": expected_cleaned})
244-
253+
245254
except ImportError:
246255
# Skip test if django is not available
247256
self.skipTest("Django not available")
248257

258+
249259
class TestBoundedAttributes(unittest.TestCase):
250260
# pylint: disable=consider-using-dict-items
251261
base = {

0 commit comments

Comments
 (0)