Replies: 1 comment
-
fixed in #770 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
TODO Analysis: Multiselect Type Consistency Issue
Issue Location
File:
libp2p/protocol_muxer/multiselect.py:48
Method:
negotiate()
TODO Comment:
# FIXME: Make TProtocol Optional[TProtocol] to keep types consistent
Current Status Analysis
Current Implementation
The
negotiate()
method in theMultiselect
class currently has a type inconsistency:The Type Inconsistency Problem
Interface Definition vs Implementation
Interface (abc.py):
Implementation (multiselect.py):
Key Differences
TProtocol | None
(protocol can be None)TProtocol
(protocol is always non-None)Current Data Structure
The
handlers
dictionary allowsNone
as a key:This means:
None
protocol: Can be registered as a handlerNone
handler: Can be associated with any protocolNone
protocols inls
commandWhat the TODO Suggests
The TODO suggests making the return type
Optional[TProtocol]
(equivalent toTProtocol | None
) to maintain type consistency with:None
protocolNone
keysImpact Analysis
Current Behavior
The current implementation never returns
None
for the protocol because:protocol = TProtocol(command)
always creates a non-None protocolif protocol in self.handlers:
only matches non-None protocolsTProtocol
instancePotential Issues
Usage Patterns
Looking at the codebase usage:
SecurityMultistream:
MuxerMultistream:
BasicHost:
What Must Be Done to Fix This Issue
Option A: Make Return Type Optional (Recommended)
Change the implementation to match the interface:
Benefits:
Option B: Change Interface to Match Implementation
Make the interface more restrictive:
Drawbacks:
Option C: Hybrid Approach
Handle None protocol cases explicitly:
Required Changes
Core Implementation
libp2p/protocol_muxer/multiselect.py
- Update return type annotationInterface Updates
libp2p/abc.py
- Keep current interface (Option A) or update (Option B)Usage Updates
If implementing Option A, update all callers to handle potential None protocol:
SecurityMultistream:
MuxerMultistream:
BasicHost:
Tests
tests/core/protocol_muxer/test_*.py
- Update tests to handle None protocolTesting Strategy
Unit Tests
Integration Tests
Static Analysis
Files That Would Need Updates
Core Implementation
libp2p/protocol_muxer/multiselect.py
- Main implementationlibp2p/abc.py
- Interface definition (if Option B)Usage Sites
libp2p/security/security_multistream.py
- Security negotiationlibp2p/stream_muxer/muxer_multistream.py
- Muxer negotiationlibp2p/host/basic_host.py
- Host stream handlinglibp2p/transport/upgrader.py
- Transport upgrade logicTests
tests/core/protocol_muxer/test_multiselect.py
- Multiselect teststests/core/protocol_muxer/test_multiselect_client.py
- Client teststests/core/security/test_*.py
- Security teststests/core/stream_muxer/test_*.py
- Muxer testsRecommendation
Recommended Approach: Option A (Make Return Type Optional)
Rationale:
Implementation Plan
negotiate()
methodMigration Strategy
Conclusion
The TODO issue represents a type system inconsistency that should be addressed to maintain code quality and future flexibility. The recommended approach (Option A) provides the best balance of:
This fix will improve the overall type safety of the libp2p codebase and make it more maintainable for future development.
Beta Was this translation helpful? Give feedback.
All reactions