Fix fields_desc type hint to allow Packet class references
Summary
- widen
Packet.fields_desctype annotation toClassVar[List[Union[AnyField, Type[Packet]]]] - add
_resolved_fields_desc()helper to cast resolved field lists for mypy at runtime usage sites - no change to build/dissect behavior
Closes #5018
Why _resolved_fields_desc() was added
The type annotation alone is sufficient to fix #5018, but widening fields_desc to
List[Union[AnyField, Type[Packet]]] causes mypy to treat runtime iteration over
self.fields_desc as possibly containing Type[Packet]. That breaks CI (tox -e mypy
on Python 3.12): 22 new errors in scapy/packet.py with the one-line change alone.
At runtime, Packet_metaclass.__new__ already resolves Packet references to Field
instances before any instance method runs. _resolved_fields_desc() is therefore a
typing-only helper (cast + list) with no behavioral change; it documents that
call 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:
- build/dissect/show paths are unchanged
- the Packet-in-
fields_descpattern is already exercised by existing tests, e.g.test/contrib/loraphy2wan.uts(LinkADRReqinlinesDataRate_TXPowerandRedundancyPacket classes infields_desc) - regression coverage for this change is provided by
tox -e mypyandtox -e flake8
Happy to add a minimal .uts test if maintainers prefer explicit coverage.
Test plan
- [x]
tox -e flake8 - [x]
tox -e mypy(Python 3.12)
Made with Cursor
Codecov Report
:x: Patch coverage is 95.00000% with 1 line in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 80.21%. Comparing base (14cc726) to head (f031589).
:warning: Report is 13 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| scapy/packet.py | 95.00% | 1 Missing :warning: |
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
| Files with missing lines | Coverage Δ | |
|---|---|---|
| scapy/packet.py | 84.70% <95.00%> (ø) |
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Hi & thanks for the PR.
The fix provided in the original issue looked more correct: don't add a function, just update the one typing that's incorrect and use # type: ignore if absolutely necessary elsewhere.
I don't think that this PR is in good state, closing.