refactor: update metadata handling to use MetadataValue type alias #912
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.
What was wrong?
Issue #911 Some methods used generic [Any] type annotations where more specific types could be used, leading to reduced type safety and potential runtime errors. This affected metadata handling in [abc.py] (lines 391, 870, 2135) and [peerstore.py] (line 293), where [Any] was overly permissive for JSON-serializable metadata values.
How was it fixed?
Introduced [MetadataValue] Type Alias:
Added [MetadataValue = Union[str, int, float, bool, list, dict, None]] in [abc.py] to represent JSON-serializable types commonly used for peer metadata.
Updated Method Signatures:
Replaced [Any] with [MetadataValue] in key interfaces and implementations:
Fixed Incorrect Return Type:
Corrected IPeerData.get_metadata() to return [MetadataValue] instead of the erroneous IPeerMetadata.
Ensured Consistency:
Updated all related implementations in [peerstore.py] and [peerdata.py] to use the new type, maintaining backward compatibility while improving type safety.
To-Do
Cute Animal Picture