Skip to content

Commit bad01e3

Browse files
committed
Fixed pyright error
1 parent cf16d60 commit bad01e3

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616
import threading
1717
from collections import OrderedDict
1818
from collections.abc import MutableMapping
19-
from typing import Mapping, Optional, Sequence, Tuple, Union
19+
from typing import TYPE_CHECKING, Mapping, Optional, Sequence, Tuple, Union
2020

2121
from opentelemetry.util import types
2222

23-
# Optional Django imports - only available if Django is installed
24-
try:
25-
from django.core.handlers.wsgi import WSGIRequest
26-
27-
_HAS_DJANGO = True
28-
except ImportError:
29-
WSGIRequest = None # type: ignore
23+
if TYPE_CHECKING:
24+
try:
25+
from django.core.handlers.wsgi import WSGIRequest
26+
_HAS_DJANGO = True
27+
except ImportError:
28+
WSGIRequest = None # type: ignore
29+
_HAS_DJANGO = False
30+
else:
3031
_HAS_DJANGO = False
3132

3233
# bytes are accepted as a user supplied value for attributes but
@@ -137,11 +138,7 @@ def _clean_extended_attribute_value(
137138
value = value[:max_len]
138139
return value
139140

140-
if (
141-
_HAS_DJANGO
142-
and WSGIRequest is not None
143-
and isinstance(value, WSGIRequest)
144-
):
141+
if _HAS_DJANGO and WSGIRequest is not None and (isinstance(value, WSGIRequest)):
145142
wsgi_data = {
146143
"method": getattr(value, "method", None),
147144
"path": getattr(value, "path", None),

opentelemetry-api/tests/attributes/test_attributes.py

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

1515
# type: ignore
16+
1617
import io
1718
import unittest
18-
from typing import MutableSequence
19-
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
19+
from typing import TYPE_CHECKING, MutableSequence
20+
21+
if TYPE_CHECKING:
22+
try:
23+
from django.conf import settings
24+
from django.core.handlers.wsgi import WSGIRequest
25+
_HAS_DJANGO = True
26+
except ImportError:
27+
WSGIRequest = None # type: ignore
28+
settings = None # type: ignore
29+
_HAS_DJANGO = False
30+
else:
2931
_HAS_DJANGO = False
3032

3133
from opentelemetry.attributes import (
@@ -194,7 +196,7 @@ def test_mapping(self):
194196
)
195197

196198
def test_wsgi_request_attribute(self):
197-
if _HAS_DJANGO:
199+
if _HAS_DJANGO and WSGIRequest is not None:
198200
if not settings.configured:
199201
settings.configure(
200202
DEBUG=True,

0 commit comments

Comments
 (0)