`Packet.fields_desc` type annotation is inconsistent with runtime behavior
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- python
- Domain
- networking
Research direction
Start with the fields_desc annotation in scapy/packet.py, then read the Packet_metaclass.__new__ handling in scapy/base_classes.py around lines 371-380. Confirm that the annotation represents both field instances and referenced Packet classes, and that it matches the runtime flattening behavior.
Written by the indexing model from the issue text.
Description
Brief description
Packet.fields_desc supports Packet / Packet_metaclass but typed as List[AnyField]
Scapy version
2.7.0
Python version
3.14
Operating system
Windows11
Additional environment information
No response
How to reproduce
The type annotation for fields_desc on Packet classes is declared as List[AnyField]:
# scapy/packet.py, line 108
fields_desc = [] # type: List[AnyField]
However, at runtime, the Packet_metaclass.__new__ method explicitly handles the case where elements in fields_desc are Packet metaclass instances (i.e., references to other Packet subclasses), not just Field instances:
# scapy/base_classes.py, lines 371-380
if "fields_desc" in dct: # perform resolution of references to other packets # noqa: E501
current_fld = dct["fields_desc"] # type: List[Union[scapy.fields.Field[Any, Any], Packet_metaclass]] # noqa: E501
resolved_fld = [] # type: List[scapy.fields.Field[Any, Any]]
for fld_or_pkt in current_fld:
if isinstance(fld_or_pkt, Packet_metaclass):
# reference to another fields_desc
for pkt_fld in fld_or_pkt.fields_desc:
resolved_fld.append(pkt_fld)
else:
resolved_fld.append(fld_or_pkt)
This means fields_desc can contain:
Fieldinstances — the documented/typed behaviorPacketclasses (metaclass instances) — which get inlined/expanded by flattening theirfields_desc
Actual result
No response
Expected result
The type should be updated to something like:
fields_desc = [] # type: List[Union[AnyField, Type[Packet]]]
Related resources
No response
- Dominant language
- Python
- Stars
- 12.6k
- Forks
- 2.2k
- Avg merge
- 1d 9h
- Merged PRs (30d)
- 60
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from secdev/scapy
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
2.8.0 release Opendiscussion major
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100