Skip to content

Commit c6779cc

Browse files
committed
Tests
1 parent aa50804 commit c6779cc

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@
2020

2121
from opentelemetry.util import types
2222

23-
WSGIRequest = None # type: ignore
24-
_HAS_DJANGO = False
25-
if TYPE_CHECKING:
23+
24+
def _get_wsgi_request_type():
25+
"""Get WSGIRequest type if Django is available, otherwise return None."""
2626
try:
27-
from django.core.handlers.wsgi import WSGIRequest
28-
_HAS_DJANGO = True
27+
from django.core.handlers.wsgi import WSGIRequest # type: ignore
28+
return WSGIRequest
2929
except ImportError:
30-
pass
30+
return None
3131

3232
# bytes are accepted as a user supplied value for attributes but
3333
# decoded to strings internally.
3434
_VALID_ATTR_VALUE_TYPES = (bool, str, bytes, int, float)
3535
# AnyValue possible values
36+
_WSGIRequest = _get_wsgi_request_type()
3637
_VALID_ANY_VALUE_TYPES = (
3738
type(None),
3839
bool,
@@ -42,7 +43,7 @@
4243
str,
4344
Sequence,
4445
Mapping,
45-
) + ((WSGIRequest,) if _HAS_DJANGO else ())
46+
) + ((_WSGIRequest,) if _WSGIRequest is not None else ())
4647

4748

4849
_logger = logging.getLogger(__name__)
@@ -137,7 +138,7 @@ def _clean_extended_attribute_value(
137138
value = value[:max_len]
138139
return value
139140

140-
if _HAS_DJANGO and WSGIRequest is not None and (isinstance(value, WSGIRequest)):
141+
if _WSGIRequest is not None and isinstance(value, _WSGIRequest):
141142
wsgi_data = {
142143
"method": getattr(value, "method", None),
143144
"path": getattr(value, "path", None),

opentelemetry-api/tests/attributes/test_attributes.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@
1717
import io
1818
import unittest
1919
from typing import TYPE_CHECKING, MutableSequence
20-
21-
_HAS_DJANGO = False
22-
WSGIRequest = None
23-
settings = None
24-
if TYPE_CHECKING:
25-
try:
26-
from django.conf import settings
27-
from django.core.handlers.wsgi import WSGIRequest
28-
_HAS_DJANGO = True
29-
except ImportError:
30-
pass
31-
3220
from opentelemetry.attributes import (
3321
BoundedAttributes,
3422
_clean_attribute,
3523
_clean_extended_attribute,
24+
_get_wsgi_request_type,
3625
)
3726

3827

28+
def _get_django_settings():
29+
"""Get Django settings if available, otherwise return None."""
30+
try:
31+
from django.conf import settings # pyright: ignore[reportMissingImports]
32+
return settings
33+
except ImportError:
34+
return None
35+
36+
37+
_WSGIRequest = _get_wsgi_request_type()
38+
_settings = _get_django_settings()
39+
3940
class TestAttributes(unittest.TestCase):
4041
# pylint: disable=invalid-name
4142
def assertValid(self, value, key="k"):
@@ -195,9 +196,11 @@ def test_mapping(self):
195196
)
196197

197198
def test_wsgi_request_attribute(self):
198-
if _HAS_DJANGO and WSGIRequest is not None:
199-
if settings and not settings.configured:
200-
settings.configure(
199+
if _WSGIRequest is None or _settings is None:
200+
self.skipTest("Django not available")
201+
202+
if not _settings.configured:
203+
_settings.configure(
201204
DEBUG=True,
202205
SECRET_KEY="test-secret-key",
203206
USE_TZ=True,
@@ -225,9 +228,7 @@ def test_wsgi_request_attribute(self):
225228
}
226229

227230
# Create a WSGIRequest object
228-
wsgi_request = WSGIRequest(environ)
229-
230-
# Test that WSGIRequest gets cleaned to a dictionary format
231+
wsgi_request = _WSGIRequest(environ)
231232
expected_cleaned = {
232233
"method": "GET",
233234
"path": "/test",

0 commit comments

Comments
 (0)