Skip to content

feat: add type stubs for miniupnpc #1245

Open
GautamBytes wants to merge 2 commits intolibp2p:mainfrom
GautamBytes:fix/miniupnpc-type-stubs
Open

feat: add type stubs for miniupnpc #1245
GautamBytes wants to merge 2 commits intolibp2p:mainfrom
GautamBytes:fix/miniupnpc-type-stubs

Conversation

@GautamBytes
Copy link
Contributor

What was wrong?
The type checker (pyrefly) failed when importing miniupnpc because it is a C extension without native type stubs. A previous quick-fix (PR #1010) used a # type: ignore comment to bypass the error, but this did not provide proper type safety or IDE support for developers.

Issue #1009

How was it fixed?
Following the long-term solution suggested in the issue, I implemented the following changes:

  • Created Type Stubs: Added stubs/miniupnpc/init.pyi with type annotations for the specific methods used by libp2p (discover, selectigd, externalipaddress, addportmapping, and deleteportmapping).
  • Updated Tooling Configuration: Modified pyproject.toml to include the new stubs directory in the pyrefly search path.
  • Code Cleanup: Removed the now-unnecessary # type: ignore[import-untyped] comment from libp2p/discovery/upnp/upnp.py.
  • Verified Types and Logic: Confirmed that pyrefly now passes with 0 errors on the affected module and that all 10 UPnP-related unit tests pass successfully.
(venv) gautammanch@Gautams-MacBook-Air py-libp2p % pyrefly check libp2p/discovery/upnp/upnp.py
 INFO 0 errors shown, 0 errors ignored, 1 modules, 94 transitive dependencies, 25,956 lines, took 0.36s (checking 0.05s), peak memory physical 95.9 MiB
(venv) gautammanch@Gautams-MacBook-Air py-libp2p % pytest tests/core/discovery/upnp/test_upnp.py
================================================================================= test session starts =================================================================================
platform darwin -- Python 3.12.12, pytest-9.0.2, pluggy-1.6.0 -- /Users/gautammanch/py-libp2p/venv/bin/python3.12
cachedir: .pytest_cache
rootdir: /Users/gautammanch/py-libp2p
configfile: pyproject.toml
plugins: mock-3.15.1, anyio-1.4.0, xdist-3.8.0, timeout-2.4.0, trio-0.8.0, Faker-40.1.2, rerunfailures-16.1
collected 10 items                                                                                                                                                                    

tests/core/discovery/upnp/test_upnp.py::test_upnp_discover_success PASSED                                                                                                       [ 10%]
tests/core/discovery/upnp/test_upnp.py::test_upnp_discover_with_success_exception PASSED                                                                                        [ 20%]
tests/core/discovery/upnp/test_upnp.py::test_upnp_discover_no_devices_found PASSED                                                                                              [ 30%]
tests/core/discovery/upnp/test_upnp.py::test_upnp_discover_double_nat PASSED                                                                                                    [ 40%]
tests/core/discovery/upnp/test_upnp.py::test_add_port_mapping_success PASSED                                                                                                    [ 50%]
tests/core/discovery/upnp/test_upnp.py::test_add_port_mapping_failure_no_discover PASSED                                                                                        [ 60%]
tests/core/discovery/upnp/test_upnp.py::test_add_port_mapping_exception PASSED                                                                                                  [ 70%]
tests/core/discovery/upnp/test_upnp.py::test_remove_port_mapping_success PASSED                                                                                                 [ 80%]
tests/core/discovery/upnp/test_upnp.py::test_upnp_discover_invalid_igd PASSED                                                                                                   [ 90%]
tests/core/discovery/upnp/test_upnp.py::test_upnp_discover_selectigd_failure PASSED                                                                                             [100%]

================================================================================ slowest 50 durations =================================================================================
1.77s setup    tests/core/discovery/upnp/test_upnp.py::test_upnp_discover_success

(29 durations < 0.005s hidden.  Use -vv to show these durations.)
================================================================================= 10 passed in 1.81s ==================================================================================

To-Do
[x] Clean up commit history
[x] Add or update documentation related to these changes
[x] Add entry to the release notes

Signed-off-by: Gautam Manchandani <gautammanch@Gautams-MacBook-Air.local>
@GautamBytes GautamBytes changed the title feat: add type stubs for miniupnpc (#1009) feat: add type stubs for miniupnpc Feb 26, 2026
Signed-off-by: Gautam Manchandani <gautammanch@Gautams-MacBook-Air.local>
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.

1 participant