Fix AttributeError on Python 3.9 when generating invalid attribute error message #4890
+23
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4821
On Python 3.9, when an invalid attribute value is passed that cannot be converted to a string, the code raises
AttributeErrorinstead of the intendedTypeError. This occurs because the error message generation attempts to access__name__on all types in_VALID_ANY_VALUE_TYPES, buttyping.Mapping(and other generic types) don't have a__name__attribute in Python 3.9.Changes
_clean_extended_attribute_value()in opentelemetry-api/src/opentelemetry/attributes/init.py:193 to usegetattr(valid_type, '__name__', getattr(valid_type, '_name', None))for safe attribute accesstest_invalid_type_error_message()to verify that invalid types that cannot be stringified returnNonewithout raisingAttributeErrorImplementation
The fix follows the suggestion from @xrmx in the issue discussion, using nested
getattr()calls to:__name__attribute_nameattribute if__name__doesn't existNoneif neither attribute existsThis ensures that the error message generation works correctly across all Python versions, including Python 3.9 where generic types from
typingmodule don't have__name__.Test plan
The CI tests will run this on Python 3.9-3.14 to verify the fix works across all supported versions.