Skip to content

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

Open
jankesec wants to merge 2 commits 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 2 commits 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
@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.63%. Comparing base (5f60db0) to head (7edd8df).
⚠️ Report is 10 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5123   +/-   ##
=======================================
  Coverage   80.63%   80.63%           
=======================================
  Files         390      390           
  Lines       96903    96918   +15     
=======================================
+ Hits        78135    78152   +17     
+ Misses      18768    18766    -2     
Files with missing lines Coverage Δ
scapy/packet.py 84.80% <100.00%> (ø)

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gpotter2

gpotter2 commented Sep 3, 2026

Copy link
Copy Markdown
Member

This breaks mypy. You might need to add some more type hints or #ignore maybe. I don't know what would be the best

Since Packet_metaclass resolves and inlines Packet classes into fields
at class definition time, cast internal iterations of self.fields_desc
to List[AnyField] in scapy/packet.py to satisfy mypy.
@jankesec

jankesec commented Sep 3, 2026

Copy link
Copy Markdown
Author

Hey @gpotter2, good catch!

I dug into the mypy failures: since Packet_metaclass unpacks and flattens Type[Packet] into fields when the class is defined, self.fields_desc is strictly List[AnyField] once initialized. However, mypy was complaining across packet.py because it assumed self.fields_desc could still hold packet classes at runtime.

I've updated the internal loops in scapy/packet.py with cast(List[AnyField], ...) to make that guarantee explicit to mypy. I tested it locally against both the linux / win32 mypy runs and the UTScapy suite (fields.uts and regression.uts), and everything is green with 0 errors now.

Just pushed the update — let me know what you think!

Sign up for free to 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.

Packet.fields_desc type annotation is inconsistent with runtime behavior

2 participants