scapy icon indicating copy to clipboard operation
scapy copied to clipboard

Fix fields_desc type hint to allow Packet class references

Open T3pp31 opened this issue 5 months ago • 2 comments

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

  • [x] tox -e flake8
  • [x] tox -e mypy (Python 3.12)

Made with Cursor

T3pp31 avatar Jun 21 '26 12:06 T3pp31

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%> (ø)

... and 19 files with indirect coverage changes

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

codecov[bot] avatar Jun 21 '26 12:06 codecov[bot]

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.

gpotter2 avatar Jun 22 '26 16:06 gpotter2

I don't think that this PR is in good state, closing.

gpotter2 avatar Jul 22 '26 08:07 gpotter2