Skip to main content

PyTCP-net_addr

An alternative to Python's standard-library ipaddress module — originally built for the PyTCP TCP/IP stack, fully usable on its own.

from net_addr import Ip6Address, Ip4Network, Ip4Address, MacAddress

Ip6Address("2001:db8::1").is_global                 # True
Ip4Network("10.0.0.0/24")[10]                       # Ip4Address("10.0.0.10")
MacAddress("02:00:00:00:00:07").is_multicast        # False
Ip4Address("10.0.0.1") in Ip4Network("10.0.0.0/8")  # True

Why

A correct, immutable, strict-parsing address library with no runtime dependencies and a single clean exception tree — suitable for protocol stacks, network tooling, and anywhere ipaddress is too loose or does not give you hashable / orderable / MAC / wildcard / interface-address value types.

Features

  • Value typesIp4Address, Ip6Address, MacAddress, Ip4Network / Ip6Network, Ip4Mask / Ip6Mask, Ip4Wildcard / Ip6Wildcard (ACL / firewall, non-contiguous), Ip4IfAddr / Ip6IfAddr.
  • Immutable & efficient__slots__, hashable, totally ordered, @final leaves; equality never crosses address families.
  • Multi-form constructors — string, integer, bytes / bytearray / memoryview, copy, or unspecified, via one positional argument.
  • Strict parsing — POSIX inet_pton only; legacy octal / hex / short-form leniencies and hybrid MAC separators are rejected.
  • RFC-accurate classification — global / private / link-local / loopback / multicast / documentation / reserved; RFC 4007 IPv6 zone identifiers; IPv4-mapped, 6to4 and Teredo extraction.
  • CIDR arithmeticsubnets(), supernet(), address_exclude(), hosts(), RFC 4632 summarize(), containment and overlap.
  • IPv6 interface-identifier generators — EUI-64, RFC 7217 stable-privacy, RFC 8981 temporary; solicited-node multicast and multicast-MAC mapping.
  • Rich formatting — expanded form, MAC hyphen / Cisco notations, zero-padded radix, reverse-DNS PTR names.
  • One exception tree — every failure is a NetAddrError subclass; never a bare builtin.
  • Fully typed — ships py.typed (PEP 561); strict-mypy clean.

Type hierarchy

Two ABC roots — Base (the value-type contract: __str__ / __repr__ / __eq__ / __hash__) and Ip (the IP-version mixin: version / is_ip4 / is_ip6, mixed into every IP-versioned family but not MacAddress). Concrete leaves are @final:

Base                         value-type contract
Ip                           IP-version mixin

Address(Base)
├── IpAddress(Address, Ip)
│   ├── Ip4Address  @final
│   └── Ip6Address  @final
└── MacAddress      @final   (no Ip mixin — a MAC has no IP version)

IpNetwork[A, M](Base, Ip)    ├── Ip4Network  @final   └── Ip6Network  @final
IfAddr[A, N](Base, Ip)       ├── Ip4IfAddr   @final   └── Ip6IfAddr   @final
IpMask(Base, Ip)             ├── Ip4Mask     @final   └── Ip6Mask     @final
IpWildcard(Base, Ip)         ├── Ip4Wildcard @final   └── Ip6Wildcard @final

IpVersion                    IntEnum (IP4 / IP6)

The generics use PEP 695 syntax: IpNetwork[A, M] is parameterised over its address and mask type, IfAddr[A, N] over its address and network type. @final on every leaf is load-bearing — the isinstance(other, type(self))-based __eq__ / __hash__ is symmetric and hash-consistent only for non-subclassable leaves.

Public API

from net_addr import … exposes the concrete value types, the family ABCs IpAddress / IfAddr / IpMask / IpNetwork / IpWildcard (for typing — the Base / Ip / Address roots stay internal to their submodules), the IpVersion enum, the length constants (IP4__ADDRESS_LEN, IP6__ADDRESS_LEN, MAC__ADDRESS_LEN), and the full *Error tree. The opt-in ClickType* argument types (ClickTypeIp4Address, ClickTypeIp6IfAddr, …) are re-exported lazily — from net_addr import ClickTypeIp4Address works but pulls in click only on first access, so the base import stays stdlib-only.

Ip4IfAddr / Ip6IfAddr are pure (address, network) value pairs — there is no mutable per-address metadata. A default gateway is not interface-address state: it is routing state, owned by the consuming stack's routing table (FIB) / Route API. Mutating a value type means constructing a fresh instance, never assigning to a field.

Exception model

One tree, three catchable supersets per leaf, never a bare builtin:

NetAddrError
├── IpAddressError / IpNetworkError / IfAddrError / IpMaskError / IpWildcardError
│     ├── <axis> base ...FormatError   (construction failed — bad literal)
│     ├── <axis> base ...SanityError   (precondition / invariant / range / format-code)
│     └── per-type umbrellas: Ip4AddressError, Ip6NetworkError, ...
└── MacAddressError → MacAddressFormatError / MacAddressSanityError

*FormatError is construction failure; *SanityError is everything else (a precondition such as multicast_mac on a non-multicast address, a bad subnets() / address_exclude() argument, an out-of-range network[i], an unknown __format__ code). Mask and wildcard types have a Format axis only — they can fail only at construction. PyTCP does not mirror stdlib ipaddress's ValueError / IndexError: catch NetAddrError (or a precise subclass) instead.

Install

pip install PyTCP-net_addr

Stdlib-only. The optional Click argument types are an extra:

pip install "PyTCP-net_addr[cli]"   # adds click; exposes the net_addr ClickType* params

Requirements

Python 3.14+ (the library uses PEP 695 generics and modern typing).

Current state (3.0.8)

  • 24 source modules; 2636 unit tests, ~99% source coverage (the remaining lines are @abstractmethod stub bodies, a documented unreachable NoReturn guard, and the lazy-click __getattr__ exercised in subprocess probes).
  • Addressing-RFC fidelity (RFC 4291 scopes, 5952 canonical text, 4007 zones, 1918 / 4193 classification, 7217 / 8981 IID generation) is encoded in code + unit tests, not in docs/rfc — by design, since this is a value-type library, not a wire protocol. The authoring contract lives in .claude/rules/net_addr.md.

Changelog

See CHANGELOG.md.

License

GPL-3.0-or-later. Part of the PyTCP project by Sebastian Majewski.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytcp_net_addr-3.0.9.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytcp_net_addr-3.0.9-py3-none-any.whl (67.1 kB view details)

Uploaded Python 3

File details

Details for the file pytcp_net_addr-3.0.9.tar.gz.

File metadata

  • Download URL: pytcp_net_addr-3.0.9.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytcp_net_addr-3.0.9.tar.gz
Algorithm Hash digest
SHA256 ba4e9326ba7c34913c086bffcbebfc2642175319c641071b29295f0c3866270b
MD5 336c4c217b64098ff4b7778126edbbdf
BLAKE2b-256 2c21d38e1bd12628d0d7a22f0553a7632d99479ddb7b386273061c3c7da5d784

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytcp_net_addr-3.0.9.tar.gz:

Publisher: publish.yml on ccie18643/PyTCP

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytcp_net_addr-3.0.9-py3-none-any.whl.

File metadata

  • Download URL: pytcp_net_addr-3.0.9-py3-none-any.whl
  • Upload date:
  • Size: 67.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytcp_net_addr-3.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 9066c35408dbce593299a902288ab7a7548b8831118407492bc27dad6dfc327b
MD5 a4a663d51a2b4cf1a29efe9f7e822f49
BLAKE2b-256 b0ede815bdea94ea52e56fa073ba995e72abc96c580643e4ce2cfcaabff7fab8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytcp_net_addr-3.0.9-py3-none-any.whl:

Publisher: publish.yml on ccie18643/PyTCP

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

3.0.9 This release

2 files

3.0.8

2 files

3.0.7

2 files

3.0.6

2 files

3.0.5

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page