Skip to content

typing: allow Packet classes in Packet.fields_desc type hint - #5123

Open
jankesec wants to merge 1 commit into
secdev:masterfrom
jankesec:fix/fields-desc-type-hint
Open

typing: allow Packet classes in Packet.fields_desc type hint#5123
jankesec wants to merge 1 commit into
secdev:masterfrom
jankesec:fix/fields-desc-type-hint

Conversation

@jankesec

@jankesec jankesec commented Sep 1, 2026

Copy link
Copy Markdown

Summary

This PR updates the type annotation of Packet.fields_desc to allow references to Packet classes (subclasses), resolving #5018.

Background

At runtime, Packet_metaclass.__new__ in scapy/base_classes.py explicitly resolves references to other Packet subclasses within fields_desc by inlining/flattening their fields:

if "fields_desc" in dct:
    current_fld = dct["fields_desc"]
    resolved_fld = []
    for fld_or_pkt in current_fld:
        if isinstance(fld_or_pkt, Packet_metaclass):
            for pkt_fld in fld_or_pkt.fields_desc:
                resolved_fld.append(pkt_fld)
        else:
            resolved_fld.append(fld_or_pkt)

However, Packet.fields_desc was typed only as ClassVar[List[AnyField]], causing static type checkers to raise typing errors when referencing other Packet classes in fields_desc.

Changes

  • Updated Packet.fields_desc type annotation in scapy/packet.py from ClassVar[List[AnyField]] to ClassVar[List[Union[AnyField, Type[Packet]]]].
  • Tested with UTScapy suite (fields.uts).

Fixes #5018

Packet_metaclass explicitly supports and resolves references to Packet classes
in fields_desc by inlining their fields_desc at class creation time. Update
Packet.fields_desc type annotation from ClassVar[List[AnyField]] to
ClassVar[List[Union[AnyField, Type[Packet]]]].

Fixes secdev#5018

AI-Assisted: yes (Antigravity)
@jankesec
jankesec force-pushed the fix/fields-desc-type-hint branch from 7922ebb to 7edd8df Compare September 1, 2026 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant