typing: allow Packet classes in Packet.fields_desc type hint - #5123
typing: allow Packet classes in Packet.fields_desc type hint#5123jankesec wants to merge 2 commits into
Conversation
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)
7922ebb to
7edd8df
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
|
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.
|
Hey @gpotter2, good catch! I dug into the mypy failures: since I've updated the internal loops in Just pushed the update — let me know what you think! |
Summary
This PR updates the type annotation of
Packet.fields_descto allow references toPacketclasses (subclasses), resolving #5018.Background
At runtime,
Packet_metaclass.__new__inscapy/base_classes.pyexplicitly resolves references to otherPacketsubclasses withinfields_descby inlining/flattening their fields:However,
Packet.fields_descwas typed only asClassVar[List[AnyField]], causing static type checkers to raise typing errors when referencing otherPacketclasses infields_desc.Changes
Packet.fields_desctype annotation inscapy/packet.pyfromClassVar[List[AnyField]]toClassVar[List[Union[AnyField, Type[Packet]]]].fields.uts).Fixes #5018