Skip to content

[Hinty] Add type hints to Scapy #2158

Closed
@gpotter2

Description

@gpotter2

Project "Hinty" aims at adding Type hints to Scapy. It will help discover bugs, improve the API, and make Scapy up-to-date with the high standards of Python libraries.

Implementation

We use mypy to ensure automatic testing of the work that has already been completed. PRs that fall under project Hinty will process one (or a few) files and register them into the checks. The file .config/mypy/mypy_enabled.txt lists the files in which mypy checks are enabled. This process has been added by #2162

Because we support Python 2.7, the format of the Type hints should be the following:
https://mypy.readthedocs.io/en/latest/cheat_sheet.html

Guide on how to contribute

  1. 💿 Make sure you've "git clonned" the latest version of scapy and are working from within it !

  2. ✏️ Pick a file you want to work on. If you're new on scapy, pick a file from within the layers/ or contrib/ folder that is small in size and complexity: at this point in time, the remaining layers in Scapy's core are often hard to type.

  3. ✏️ Add that file to .config/mypy/mypy_enabled.txt. This will enable it in our test suite.

  4. ➡️ pip install pyannotate cryptography tox mock (install tox, pyannotate and cryptography)

  5. ➡️ Run the tests for your file: tox --sitepackages -- -x -K tcpdump -K manufdb -K wireshark -K tshark -K brotli -K zstd -K ci_only -K not_pyannotate -N
    You shall specify a -e py[38]-[linux]_non_root (fill the brackets with your OS infos) option before the --sitepackages option if you have multiple versions of python installed. To see all available tox tags (the whole py...non_root string), type tox -l. The -x option tells our test suite to use pyannotate.
    This will create test/pyannotate_results

  6. ➡️ pyannotate --type-info test/pyannotate_results -w [the file.py you are working on]

  7. ✏️ The file has been automatically processed. Now, edit it to fix the mistakes, and check your work with tox -e mypy. This might take time

    • ℹ️ Remember that you can use cast(type, obj) from typing to help mypy without affecting the code.
    • ⚠️ Import any types you might need from scapy.compat instead of typing. It provides a fallback if typing isn't installed (python < 3.5). I know it's annoying.
    • ℹ️ If a class extends Field directly (very few do) it will require to be passed the internal and machine representation of the field. See this if you don't know what I'm talking about. For instance: class subField(Field[float, int]): is a field where i=float and m=int. In most cases, it should be [int, int] (a normal integer field).
  8. ⚠️ If you get error: No library stub file for module ..., add an exception for it in .config/mypy/mypy.ini (follow the existing format)

  9. ✅ Check that the files still pass PEP8: tox -e flake8

  10. 🥇 When everything passes, submit a PR. 😄

Active hinty related issues

Documentation for advanced typing (that I always lose the links for..)

Hinty status

MyPy Support: 25.44%

Module Typed code (lines) Typed files
[core] 100.00% 33/33
arch 100.00% 13/13
layers 3.06% 2/92
contrib 14.28% 27/167
libs 23.30% 6/8
asn1 100.00% 4/4
tools 11.08% 1/9
modules 0.00% 0/8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions