Protocol Member Order

The order of members inside a protocol class body. Settled by the owner on #2490, so a new dissector and a refactor land in the same layout.

Each group sits under its section comment, spelled as the repo spells it: # Properties., # Methods., # Data models. and # Utilities. Those comments also title the member groups on the API pages (Class Member Lists).

Every class with section comments. The four comments mean the same in any class body that uses section comments, not only a protocol’s: every @property goes under # Properties., public methods under # Methods., every dunder under # Data models., and everything else under # Utilities., which a class adds where it has none. A topic section, such as # Defaults., # Reassembly. or # Availability., keeps what it gathers. Ruled on #2911, so one rule files every class and the API pages follow from the source. No member sits above such a class’s first section comment, and an engine files unsupported_reason() and its helpers under # Availability., as EngineBase does; ruled on #2922. tests/project/test_section_meanings_2911_unit.py holds every class under pcapkit/ to the meanings and fails a member above the first comment. The order below binds protocol classes.

The Order

  1. Properties. Every @property member.

  2. Methods. Public methods: mostly id(), read(), make() and register_xxx().

  3. Data models. Every dunder name: __index__, __length_hint__, __init__, __new__, __post_init__ and any other.

  4. Utilities. Everything else, mostly private methods, in the subgroups below. Every private helper, including _check_*, sits here, after _make_data().

    1. Data. _make_data() comes first.

    2. Helpers. The helpers that read() or make() call, one helper at a time, in the order read() or make() first uses them. “Uses” means a direct reference in the body of read() or make(). Helpers reached only through getattr, or through the registry maps (__proto__, __option__, __parameter__ and similar), do not count. They are ordered by the enum value order of the corresponding code, or by the registry’s order where there is no enum. A registry fallback that read() or make() names directly, such as _read_msg_unknown() in MH, does not count either: it is a specific reader, ruled on #2490. Each helper’s _read_ and _make_ methods stay together, with the reader first: _read_addr_resolve() then _make_addr_resolve().

    3. Specific readers and makers. The option, type and parameter methods, all _read_*() before all _make_*(). Each set opens with the registry’s default, its fallback for a code with no entry of its own, as _read_opt_unassigned() opens the readers of IPv4; the rest follow the enum value of the code or type each handles, not alphabetical order. Ruled on #2490. A class with several registries puts every registry’s readers before any registry’s makers, as MH and PCAPNG do, ruled on #2772.

    4. Other helpers. Every helper that read(), make() and the registries do not reach goes last, including one that only another helper calls, such as _decode_text() in HTTPv1. Ruled on #2490.

Registry maps. The registry maps themselves (__proto__, __option__, __parameter__ and similar) are ordered by their enum values, by key value where an int key has no enum, and alphabetically where the key is a string, as the namespaces of Option.__enum__ are. PCAPNG.__option__, keyed by an option’s namespace and value, is ordered by namespace, in the order OptionType declares them, then by value. Ruled on #2772. ProtocolBase.__registry__ groups its built-in entries by layer under the comments # Link Layer, # Internet Layer, # Transport Layer, # Application Layer and # Miscellaneous, in that order, the last holding what belongs to no layer, and is alphabetical within each group, ruled on #2773.

The guard. tests/project/test_protocol_member_order_2490_unit.py holds every protocol class to the groups, _make_data() first, each _read_xxx() before its _make_xxx(), subgroups b, c and d, and the order of the registry maps, layer groups included, with a table of known exceptions (#2490). It values a key that is an enum member, an int, a string or a PCAP-NG option key, and orders a string-keyed map within each group its comment lines open; a map with a key of another shape it leaves in its own order, which rule c then follows.

Example

The utilities of ARP, reduced to its member names. read() resolves a hardware address before a protocol address, so the hardware pair comes first:

class ARP(Link[Data_ARP, Schema_ARP]):

    # Utilities.
    def _make_data(cls, data):                    # a. data first
        ...

    def _read_addr_resolve(self, addr, htype):    # b. first helper: reader ...
        ...

    def _make_addr_resolve(self, addr, htype):    # ... then its maker
        ...

    def _read_proto_resolve(self, addr, ptype):   # b. next helper: reader ...
        ...

    def _make_proto_resolve(self, addr, ptype):   # ... then its maker
        ...

The Reference

ARP is the reference for the utilities order. Its utilities are _make_data() and the helpers that read() and make() name, and nothing else, and there is more than one helper, so the first-use order of subgroup b shows as well as each reader preceding its maker. Chosen on #2614.