Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
)


# TODO: Remove this workaround and revert to the simpler implementation
# once Python 3.9 support is dropped (planned around May 2026).
# This exists only to avoid issues caused by deprecated behavior in 3.9.
def _type_name(t):
return getattr(t, "__name__", getattr(t, "_name", repr(t)))


_logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -190,7 +197,7 @@ def _clean_extended_attribute_value( # pylint: disable=too-many-branches
except Exception:
raise TypeError(
f"Invalid type {type(value).__name__} for attribute value. "
f"Expected one of {[valid_type.__name__ for valid_type in _VALID_ANY_VALUE_TYPES]} or a "
f"Expected one of {[_type_name(valid_type) for valid_type in _VALID_ANY_VALUE_TYPES]} or a "
"sequence of those types",
)

Expand Down
10 changes: 10 additions & 0 deletions opentelemetry-api/tests/attributes/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
# type: ignore

import unittest
import unittest.mock
from typing import MutableSequence

from opentelemetry.attributes import (
BoundedAttributes,
_clean_attribute,
_clean_extended_attribute,
_clean_extended_attribute_value,
)


Expand Down Expand Up @@ -320,3 +322,11 @@ def __str__(self):
self.assertEqual(
"<DummyWSGIRequest method=GET path=/example/>", cleaned_value
)

def test_invalid_anyvalue_type_raises_typeerror(self):
class BadStr:
def __str__(self):
raise Exception("boom")

with self.assertRaises(TypeError):
_clean_extended_attribute_value(BadStr(), None)
Loading