Fix fields_desc type hint to allow Packet class references#5020
Open
T3pp31 wants to merge 1 commit into
Open
Conversation
Packet.fields_desc accepts Field instances and Packet subclass references at class definition time. Align the type annotation with Packet_metaclass resolution behavior and cast at runtime usage sites. Closes secdev#5018 AI-Assisted: yes (Cursor Composer) Co-authored-by: Cursor <cursoragent@cursor.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5020 +/- ##
=======================================
Coverage 80.34% 80.34%
=======================================
Files 386 386
Lines 96012 96014 +2
=======================================
+ Hits 77137 77143 +6
+ Misses 18875 18871 -4
🚀 New features to boost your workflow:
|
Member
|
Hi & thanks for the PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Packet.fields_desctype annotation toClassVar[List[Union[AnyField, Type[Packet]]]]_resolved_fields_desc()helper to cast resolved field lists for mypy at runtime usage sitesCloses #5018
Why
_resolved_fields_desc()was addedThe type annotation alone is sufficient to fix #5018, but widening
fields_desctoList[Union[AnyField, Type[Packet]]]causes mypy to treat runtime iteration overself.fields_descas possibly containingType[Packet]. That breaks CI (tox -e mypyon Python 3.12): 22 new errors in
scapy/packet.pywith the one-line change alone.At runtime,
Packet_metaclass.__new__already resolves Packet references to Fieldinstances before any instance method runs.
_resolved_fields_desc()is therefore atyping-only helper (
cast+list) with no behavioral change; it documents thatcall sites operate on the post-metaclass, field-only list.
Why no new UTScapy tests
Per CONTRIBUTING.md, this is a typing-only fix with no runtime behavior change:
fields_descpattern is already exercised by existing tests, e.g.test/contrib/loraphy2wan.uts(LinkADRReqinlinesDataRate_TXPowerandRedundancyPacket classes infields_desc)tox -e mypyandtox -e flake8Happy to add a minimal
.utstest if maintainers prefer explicit coverage.Test plan
tox -e flake8tox -e mypy(Python 3.12)Made with Cursor