Fix fields_desc type hint to allow Packet class references#5020
Open
T3pp31 wants to merge 1 commit into
Open
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5020 +/- ##
==========================================
- Coverage 80.34% 80.21% -0.13%
==========================================
Files 386 387 +1
Lines 96012 96446 +434
==========================================
+ Hits 77137 77366 +229
- Misses 18875 19080 +205
🚀 New features to boost your workflow:
|
Member
|
Hi & thanks for the PR. |
118ed51 to
9c7d09c
Compare
Packet.fields_desc accepts Field instances and Packet subclass references at class definition time. Align the type annotation with Packet_metaclass resolution behavior and add # type: ignore at runtime usage sites where mypy cannot infer the post-metaclass type. Closes secdev#5018 AI-Assisted: yes (Cursor Composer)
9c7d09c to
f031589
Compare
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]]]]# type: ignoreat runtime usage sites where mypy cannot infer the post-metaclass typeCloses #5018
Approach
Per reviewer feedback, the fix follows the original issue's suggestion: just
update the one typing that's incorrect and use
# type: ignorewhere absolutelynecessary elsewhere. No helper function is added.
At runtime,
Packet_metaclass.__new__resolves Packet references to Fieldinstances before any instance method runs. The widened annotation causes mypy
to treat
self.fields_desciteration as possibly containingType[Packet],so
# type: ignoreis added at the 17 call sites where mypy reports errors.Two backslash-continued lines were converted to parenthesized form to allow
# type: ignoreon the continued expression.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 flake8Test plan
tox -e flake8tox -e mypy(Python 3.12, linux and win32)