Skip to content

Conversation

Aldorax
Copy link

@Aldorax Aldorax commented Jul 2, 2025

Fix: Correct get_mux() return type and module ordering for #736

What was wrong?

The IHost.get_mux() abstract method in libp2p/abc.py incorrectly used Any as its return type. An attempt to specify the precise IMultiselectMuxer type led to an "Undefined name" error from static analysis tools. This was caused by IMultiselectMuxer being defined after IHost in the libp2p/abc.py file, creating a forward-reference issue during parsing.

Additionally, type annotations and imports in the BasicHost implementation (libp2p/host/basic_host.py) and in libp2p/protocol_muxer/multiselect.py were not fully aligned with the correct IMultiselectMuxer type.

How was it fixed?

The issue was resolved through the following steps:

  1. Module Reordering in libp2p/abc.py: The definitions of IMultiselectCommunicator, IMultiselectClient, and IMultiselectMuxer were strategically moved to appear before the IHost class definition within libp2p/abc.py. This ensures proper symbol resolution during parsing.
  2. Interface Type Annotation Update: The return type annotation for IHost.get_mux() in libp2p/abc.py was accurately updated from Any to IMultiselectMuxer, bringing the interface definition into alignment.
  3. Implementation Type Annotation Update: The return type annotation for BasicHost.get_mux() in libp2p/host/basic_host.py was also updated to IMultiselectMuxer to match the corrected interface.
  4. Import Adjustments:
    • libp2p/host/basic_host.py was updated to correctly import IMultiselectMuxer from libp2p.abc.
    • libp2p/protocol_muxer/multiselect.py was updated to import IMultiselectCommunicator and IMultiselectMuxer directly from libp2p.abc to reflect the consolidated interface definitions.

These changes significantly enhance the type safety and clarity of the API for static analysis tools like Pyrefly, providing more robust code verification.

To-Do

  • Clean up commit history (e.g., squashing related commits)
  • Add or update documentation related to these changes (e.g., API reference for IHost.get_mux())
  • Add entry to the release notes (suggested category: bugfix or internal)

Cute Animal Picture

Sleeping Dog

@seetadev
Copy link
Contributor

seetadev commented Jul 2, 2025

@Aldorax and @manny-uncharted : Welcome to py-libp2p :) Thank you for submitting this PR. Appreciate your efforts and initiative.

CCing @acul71 , @sumanjeet0012 for their feedback and review on return type for mux.

Also, CCing @Khwahish29 and @mystical-prog , who are working together on abc.py interfaces and anyio upgrades too.

@Khwahish29
Copy link
Contributor

@Aldorax and @manny-uncharted : Welcome to py-libp2p :) Thank you for submitting this PR. Appreciate your efforts and initiative.

CCing @acul71 , @sumanjeet0012 for their feedback and review on return type for mux.

Also, CCing @Khwahish29 and @mystical-prog , who are working together on abc.py interfaces and anyio upgrades too.

I will surely look into this! Appreciate the heads up!

@pacrob pacrob mentioned this pull request Jul 11, 2025
3 tasks
@seetadev
Copy link
Contributor

Closing this PR as shared by the author at #742 (comment) .

Me and @pacrob are reviewing #742.

@seetadev seetadev closed this Jul 13, 2025
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.

4 participants