Typing Stubs for ipaddress

1.0.8 · active · verified Sat Apr 11

types-ipaddress is a PEP 561 compliant stub-only package providing static type hints for Python's built-in `ipaddress` module. It allows type-checking tools like MyPy, PyCharm, and Pyright to perform static analysis on code that uses `ipaddress` functionality. The package tracks the `ipaddress` stubs within the `typeshed` project and is released upon changes to those stubs. The current version is 1.0.8.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of the `ipaddress` module, including creating IP address and network objects, and checking for membership. When `types-ipaddress` is installed, a static type checker would verify the type annotations in `check_ip_in_network` against the provided stubs for `ipaddress` objects.

import ipaddress

def check_ip_in_network(ip_str: str, network_str: str) -> bool:
    try:
        ip_addr = ipaddress.ip_address(ip_str)
        network = ipaddress.ip_network(network_str, strict=False)
        return ip_addr in network
    except ValueError as e:
        print(f"Error: {e}")
        return False

if __name__ == "__main__":
    test_ip_v4 = "192.168.1.5"
    test_network_v4 = "192.168.1.0/24"
    print(f"Is {test_ip_v4} in {test_network_v4}? {check_ip_in_network(test_ip_v4, test_network_v4)}")

    test_ip_v6 = "2001:db8::1"
    test_network_v6 = "2001:db8::/64"
    print(f"Is {test_ip_v6} in {test_network_v6}? {check_ip_in_network(test_ip_v6, test_network_v6)}")

    invalid_ip = "999.999.999.999"
    valid_network = "10.0.0.0/8"
    print(f"Is {invalid_ip} in {valid_network}? {check_ip_in_network(invalid_ip, valid_network)}")

view raw JSON →