Skip to content

Conversation

@Jgprog117
Copy link

Summary

Fixes #4821

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 occurs because the error message generation attempts 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.

Changes

  • Updated _clean_extended_attribute_value() in opentelemetry-api/src/opentelemetry/attributes/init.py:193 to use getattr(valid_type, '__name__', getattr(valid_type, '_name', None)) for safe attribute access
  • Added test case test_invalid_type_error_message() to verify that invalid types that cannot be stringified return None without raising AttributeError

Implementation

The fix follows the suggestion from @xrmx in the issue discussion, using nested getattr() calls to:

  1. First try to get __name__ attribute
  2. Fall back to _name attribute if __name__ doesn't exist
  3. Return None if neither attribute exists

This ensures that the error message generation works correctly across all Python versions, including Python 3.9 where generic types from typing module don't have __name__.

Test plan

  • Added test case that creates an object which cannot be converted to string
  • This triggers the TypeError path where the error message is generated
  • On Python 3.9 with the old code, this would fail with AttributeError
  • With the fix, it correctly returns None and logs a warning

The CI tests will run this on Python 3.9-3.14 to verify the fix works across all supported versions.

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
@Jgprog117 Jgprog117 requested a review from a team as a code owner January 31, 2026 11:55
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 31, 2026

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: Jgprog117 / name: Jgprog117 (5491829)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect error when passing wrong attribute type on Python 3.9

1 participant