fix(core): handle non-iterable objects with __iter__ = None in metadata_to_dict - #493
Open
reginaldalfret wants to merge 1 commit into
Open
reginaldalfret wants to merge 1 commit into
reginaldalfret wants to merge 1 commit into
Conversation
…ta_to_dict Fixes aws#453 In Python, classes such as MySQLConnection (from mysql-connector-python) explicitly set __iter__ = None to mark instances as non-iterable. hasattr(obj, '__iter__') evaluates to True for such objects, but attempting iteration raises TypeError: '<Class>' object is not iterable. When metadata_to_dict encountered these objects, the TypeError caused it to log a warning and return an empty dictionary instead of serializing the object's attributes via __dict__. Fix this by: 1. Checking isinstance(obj, Iterable) rather than hasattr(obj, '__iter__'), which correctly identifies classes with __iter__ = None as non-iterable. 2. Catching TypeError during iteration as a fallback so that any non-iterable object safely proceeds to __dict__ serialization. 3. Adding regression tests for non-iterable and broken iterable objects.
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.
Fixes #453
Issue Description
In Python, classes such as
MySQLConnection(frommysql-connector-python) explicitly declare__iter__ = Noneto mark instances as non-iterable according to Python's data model. When such an object was passed toaws_xray_sdk.core.utils.conversion.metadata_to_dict, the checkhasattr(obj, "__iter__")evaluated toTrue. Attempting to iterate overobjthen raisedTypeError: 'MySQLConnection' object is not iterable.This caused
metadata_to_dictto fail with a warning log and return an empty dictionary{}instead of extracting and serializing the object's attributes viahasattr(obj, "__dict__").Solution
isinstance(obj, Iterable)fromcollections.abcinstead ofhasattr(obj, "__iter__"). In Python's ABC system,isinstance(obj, Iterable)returnsFalsewhen__iter__ = None.try...except TypeError:guard around iteration to gracefully fall through to__dict__serialization in the event of custom non-iterable or broken iterable types.__iter__ = None, broken iterators raisingTypeError, and primitive/collection types.Testing
pytest --ignore tests/ext): 152 passed.tests/test_utils.py:test_metadata_to_dict_non_iterable_object(passed)test_metadata_to_dict_broken_iterable(passed)test_metadata_to_dict_collection_types(passed)test_metadata_to_dict_self_referenceintests/util.py(passed).git diff --checkandpy_compileclean.