Fix AttributeError on Python 3.9 when generating invalid attribute error message#4890
Closed
Jgprog117 wants to merge 1 commit intoopen-telemetry:mainfrom
Closed
Fix AttributeError on Python 3.9 when generating invalid attribute error message#4890Jgprog117 wants to merge 1 commit intoopen-telemetry:mainfrom
Jgprog117 wants to merge 1 commit intoopen-telemetry:mainfrom
Conversation
On Python 3.9, when an invalid attribute value is passed that cannot be converted to a string, the code raises AttributeError instead of the intended TypeError. This is because the error message generation tries to access __name__ on all types in _VALID_ANY_VALUE_TYPES, but typing.Mapping (and other generic types) don't have a __name__ attribute in Python 3.9. This fix uses getattr with fallback to _name attribute to safely handle types that don't have __name__. Fixes open-telemetry#4821
|
|
Contributor
|
Thanks for the PR but there is already a PR for that issue in #4876 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.