Skip to content
Closed
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 @@ -190,7 +190,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 {[getattr(valid_type, '__name__', getattr(valid_type, '_name', None)) for valid_type in _VALID_ANY_VALUE_TYPES]} or a "
"sequence of those types",
)

Expand Down
22 changes: 22 additions & 0 deletions opentelemetry-api/tests/attributes/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,25 @@ def __str__(self):
self.assertEqual(
"<DummyWSGIRequest method=GET path=/example/>", cleaned_value
)

def test_invalid_type_error_message(self):
"""Test that invalid types that cannot be converted to string raise TypeError with proper message.

This test specifically addresses issue #4821 where on Python 3.9, the error message
generation would fail with AttributeError when accessing __name__ on generic types like Mapping.
"""

class UnstringifiableObject:
"""An object that cannot be converted to string."""

def __str__(self):
raise ValueError("Cannot convert to string")

invalid_value = UnstringifiableObject()

# Should return None and log warning, not raise AttributeError
cleaned_value = _clean_extended_attribute(
"test_key", invalid_value, None
)

self.assertIsNone(cleaned_value)