Skip to content

Add typed *_enum accessors alongside every enum-backed field - #134

Merged
EONRaider merged 1 commit into
masterfrom
claude/decoder-depth-polish-8m54to
Sep 4, 2026
Merged

Add typed *_enum accessors alongside every enum-backed field#134
EONRaider merged 1 commit into
masterfrom
claude/decoder-depth-polish-8m54to

Conversation

@EONRaider

Copy link
Copy Markdown
Owner

Summary

_enums.py defines EtherType, IPProtocol, ARPOperation and ARPHardwareType, but until now no decoded field exposed them as a typed value — only the *_name display strings, with the enums themselves used only for internal dispatch. reveal_type(Ethernet.decode(b"...").ethertype) was int.

1.3.0 established the right pattern for this with addresses: IPv4.src stays a str and IPv4.src_address returns an ipaddress.IPv4Address. This does the same for the enum registries: the wire field stays a plain int (so bytes(decode(x)) == x holds for values this library does not enumerate), and a new *_enum property returns the typed enum member or None — never raises — for a value it doesn't recognize.

What's included

  • Ethernet.ethertype_enum, VLAN.ethertype_enum, GRE.protocol_enumEtherType | None
  • IPv4.protocol_enum, IPv6.next_header_enum (and the three IPv6 extension headers that share the field: IPv6HopByHopOptions, IPv6DestinationOptions, IPv6Routing, IPv6Fragment) → IPProtocol | None
  • ARP.oper_enumARPOperation | None, ARP.ptype_enumEtherType | None
  • ARP.htype_name / ARP.htype_enum and DHCP.htype_name / DHCP.htype_enumARPHardwareType | None (new — ARPHardwareType was exported but referenced nowhere in src/ before this)
  • Docstring cross-references (:param: ... (see :class:...)) added wherever a field gained an enum accessor but didn't already point at its enum class
  • Test coverage for the known-value and unknown-value (None-degrades) case of every new accessor
  • CHANGELOG.md entry under a new ## [Unreleased] section (this repo's changelog had none yet since 2.0.0 shipped)

Scope note: issue #95's text names six accessors explicitly (ethertype_enum, protocol_enum, next_header_enum, oper_enum, htype_name/htype_enum on ARP). This PR also adds VLAN.ethertype_enum, GRE.protocol_enum, the three extra IPv6 extension-header next_header_enums, ARP.ptype_enum, and DHCP.htype_name/htype_enum — every other field in the codebase that maps onto the exact same four enum registries the issue targets, for consistency rather than leaving an identical gap one field over.

Verification

  • uv run --frozen ruff check . and uv run --frozen ruff format --check . are clean
  • uv run --frozen mypy is clean (strict, src/ only — 30 source files)
  • uv run --frozen pytest passes locally (582 tests)
  • uv run --frozen python scripts/benchmark.py --check --threshold 15 — 126,825 f/s, +12.9% vs. baseline, within threshold
  • CHANGELOG.md has an entry under ## [Unreleased]

New protocol or dispatch change — also:

Not applicable — no new protocol, no dispatch change. Deleted this block's checklist since it doesn't apply.

Notes

No breaking change: every new accessor is additive (a new property alongside existing fields), consistent with 2.1.0 being a minor version per the roadmap (#105, part of #107).

Closes#95.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CP7X7H4k3pBWoiAkBdxATM


Generated by Claude Code

_enums.py has defined EtherType, IPProtocol, ARPOperation and
ARPHardwareType since early on, but no decoded field ever exposed them
as a typed value -- only the *_name display strings, with the enums
themselves used solely for internal dispatch. mypy could type-check
ip.protocol as int and nothing caught a typo like ip.proto, but nobody
writing match/case or comparing against IPProtocol.TCP got a real enum
back without doing the try/except themselves.
Mirrors the src/src_address precedent 1.3.0 established for IP
addresses: the raw int field stays canonical (bytes(decode(x)) == x is
unaffected either way), and a new *_enum property returns the typed
value or None -- never raises -- for a wire value this library does
not enumerate. Covers every field with a fixed wire vocabulary, not
just the six issue #95 named explicitly: also VLAN.ethertype_enum and
GRE.protocol_enum (same EtherType as Ethernet.ethertype), the three
IPv6 extension headers that share IPv6.next_header's IPProtocol
registry, and DHCP.htype_name/htype_enum (same ARPHardwareType ARP
already used) -- all four use the identical enum a named field on this
tier already gets, so leaving them out would just move the same
documented gap sideways.
Closes#95.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CP7X7H4k3pBWoiAkBdxATM
@EONRaider
EONRaider merged commit d38409c into masterSep 4, 2026
7 checks passed
Sign up for freeto 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.

Enum accessors alongside the int fields

2 participants

@EONRaider@claude