Typing Stubs for netaddr
This package provides static type checking stubs for the `netaddr` library, enabling tools like MyPy or Pyright to perform comprehensive type analysis on code that uses `netaddr`. It is part of the Python typeshed project, which maintains type hints for popular Python packages. The current version is `1.3.0.20260408` and it follows the release cadence of the typeshed project, updating frequently to reflect changes in `netaddr`.
Common errors
-
ModuleNotFoundError: No module named 'types_netaddr'
cause Attempting to import directly from the `types_netaddr` package.fixThe `types-netaddr` package provides stubs, not a runtime module to be imported. You should import from the actual `netaddr` library (e.g., `from netaddr import IPAddress`). Type checkers will automatically use the stubs. -
mypy: Missing type stubs for 'netaddr' (or similar error from other type checkers) even after 'pip install netaddr'
cause The `netaddr` library itself does not ship with its own type stubs, and `types-netaddr` has not been installed.fixInstall the separate stub package: `pip install types-netaddr`.
Warnings
- gotcha Installing `types-netaddr` does NOT install the `netaddr` library itself. It only provides typing stubs. You must install `netaddr` separately if you intend to run the code.
- gotcha You should never import directly from `types_netaddr`. The stubs are automatically discovered by type checkers for imports made from the actual `netaddr` library.
- gotcha These stubs are only useful for static type checking tools (e.g., MyPy, Pyright). They do not alter runtime behavior or provide any new functionality at runtime.
Install
-
pip install types-netaddr
Imports
- IPAddress
from types_netaddr import IPAddress
from netaddr import IPAddress
- IPNetwork
import types_netaddr
from netaddr import IPNetwork
Quickstart
from netaddr import IPAddress
def check_ip_type(ip_str: str) -> bool:
ip = IPAddress(ip_str)
return ip.is_unicast()
# Example usage (runtime behaviour is handled by netaddr)
# Type checkers will use types-netaddr to validate the above type hints.
print(check_ip_type('192.168.1.1'))
print(check_ip_type('224.0.0.1'))