Skip to content

Fix fields_desc type hint to allow Packet class references#5020

Open
T3pp31 wants to merge 1 commit into
secdev:masterfrom
T3pp31:fix/5018-fields-desc-type
Open

Fix fields_desc type hint to allow Packet class references#5020
T3pp31 wants to merge 1 commit into
secdev:masterfrom
T3pp31:fix/5018-fields-desc-type

Conversation

@T3pp31

@T3pp31 T3pp31 commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • widen Packet.fields_desc type annotation to ClassVar[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_desc pattern is already exercised by existing tests, e.g.
    test/contrib/loraphy2wan.uts (LinkADRReq inlines DataRate_TXPower and
    Redundancy Packet classes in fields_desc)
  • regression coverage for this change is provided by tox -e mypy and tox -e flake8

Happy to add a minimal .uts test if maintainers prefer explicit coverage.

Test plan

  • tox -e flake8
  • tox -e mypy (Python 3.12)

Made with Cursor

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

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.34%. Comparing base (14cc726) to head (118ed51).

Files with missing lines Patch % Lines
scapy/packet.py 94.73% 1 Missing ⚠️
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     
Files with missing lines Coverage Δ
scapy/packet.py 84.72% <94.73%> (+0.02%) ⬆️

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gpotter2

Copy link
Copy Markdown
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Packet.fields_desc type annotation is inconsistent with runtime behavior

2 participants