Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hinty] Type hinting: prepare tests & setup Mypy #2162

Merged
merged 2 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ build: off

install:
# Install the npcap, windump and wireshark suites
- ps: .\.appveyor\InstallNpcap.ps1
- ps: .\.appveyor\InstallWindumpNpcap.ps1
- ps: .\.config\appveyor\InstallNpcap.ps1
- ps: .\.config\appveyor\InstallWindumpNpcap.ps1
# Installs Wireshark 3.0 (and its dependencies)
# https://github.com/mkevenaar/chocolatey-packages/issues/16
- choco install -n KB3033929 KB2919355 kb2999226
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions .config/mypy/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[mypy]

[mypy-IPython]
ignore_missing_imports = True

[mypy-colorama]
ignore_missing_imports = True

[mypy-traitlets.config.loader]
ignore_missing_imports = True

[mypy-scapy.modules.six,scapy.modules.winpcapy]
ignore_errors = True
50 changes: 50 additions & 0 deletions .config/mypy/mypy_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This file is part of Scapy
# See http://www.secdev.org/projects/scapy for more information
# Copyright (C) Philippe Biondi <[email protected]>
# Copyright (C) Gabriel Potter <[email protected]>
# This program is published under a GPLv2 license

"""
Performs Static typing checks over Scapy's codebase
"""

# IMPORTANT NOTE
#
# Because we are rolling out mypy tests progressively,
# we currently use --follow-imports=skip. This means that
# mypy doesn't check consistency between the imports (different files).
#
# Once each file has been processed individually, we'll remove that to
# check the inconsistencies across the files

import io
import os
import sys

from mypy.main import main as mypy_main

# Load files

with io.open("./.config/mypy/mypy_enabled.txt") as fd:
FILES = [l.strip() for l in fd.readlines() if l.strip() and l[0] != "#"]

if not FILES:
print("No files specified. Arborting")
sys.exit(0)

# Generate mypy arguments

ARGS = [
"--py2",
"--follow-imports=skip",
"--config-file=" + os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"mypy.ini"
)
)
] + [os.path.abspath(f) for f in FILES]

# Run mypy over the files

mypy_main(None, sys.stdout, sys.stderr, ARGS)
9 changes: 9 additions & 0 deletions .config/mypy/mypy_enabled.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file registers all files that have already been processed as part of
# https://github.com/secdev/scapy/issues/2158, and therefore will be enforced
# with unit tests in future development.

# Style cheet: https://mypy.readthedocs.io/en/latest/cheat_sheet.html

scapy/__init__.py
scapy/main.py
scapy/contrib/http2.py
8 changes: 3 additions & 5 deletions .travis/install.sh → .config/travis/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
# Install on osx
if [ "$TRAVIS_OS_NAME" = "osx" ]
then
pip3 install tox
guedou marked this conversation as resolved.
Show resolved Hide resolved
if [ ! -z $SCAPY_USE_PCAPDNET ]
then
brew update
brew install libdnet libpcap
fi
exit 0
fi

# Install wireshark data
Expand All @@ -25,11 +23,11 @@ then
$SCAPY_SUDO apt-get -qy install libdumbnet-dev libpcap-dev
fi

# Check pip
sudo pip install --upgrade pip setuptools --ignore-installed
# Update pip & setuptools (tox uses those)
python -m pip install --upgrade pip setuptools --ignore-installed

# Make sure tox is installed and up to date
pip install -U tox --ignore-installed
python -m pip install -U tox --ignore-installed

# Dump Environment (so that we can check PATH, UT_FLAGS, etc.)
set
File renamed without changes.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ matrix:
- os: linux
python: 3.6
env:
- TOXENV=flake8,twine,docs,spell
- TOXENV=mypy,flake8,twine,docs,spell

# Run as a regular user
- os: linux
Expand Down Expand Up @@ -130,7 +130,7 @@ matrix:
- TOXENV=linux_warnings

install:
- bash .travis/install.sh
- bash .config/travis/install.sh
- python -c "from scapy.all import conf; print(repr(conf))"

script: bash .travis/test.sh
script: bash .config/travis/test.sh
13 changes: 12 additions & 1 deletion scapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


def _version_from_git_describe():
# type: () -> str
"""
Read the version from ``git describe``. It returns the latest tag with an
optional suffix if the current directory is not exactly on the tag.
Expand All @@ -38,6 +39,9 @@ def _version_from_git_describe():

>>> _version_from_git_describe()
'2.3.2.dev346'

:raises CalledProcessError: if git is unavailable
:return: Scapy's latest tag
"""
if not os.path.isdir(os.path.join(os.path.dirname(_SCAPY_PKG_DIR), '.git')): # noqa: E501
raise ValueError('not in scapy git repo')
Expand All @@ -62,6 +66,11 @@ def _version_from_git_describe():


def _version():
# type: () -> str
"""Returns the Scapy version from multiple methods

:return: the Scapy version
"""
version_file = os.path.join(_SCAPY_PKG_DIR, 'VERSION')
try:
tag = _version_from_git_describe()
Expand Down Expand Up @@ -91,7 +100,9 @@ def _version():


VERSION = __version__ = _version()
VERSION_MAIN = re.search(r"[0-9.]+", VERSION).group()

_tmp = re.search(r"[0-9.]+", VERSION)
VERSION_MAIN = _tmp.group() if _tmp is not None else VERSION

if __name__ == "__main__":
from scapy.main import interact
Expand Down
29 changes: 29 additions & 0 deletions scapy/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,32 @@ def gzip_compress(x):
else:
gzip_decompress = gzip.decompress
gzip_compress = gzip.compress

# Typing compatibility

try:
# Only required if using mypy-lang for static typing
from typing import Optional, List, Union, Callable, Any, Tuple, Sized, \
Dict, Pattern, cast
except ImportError:
# Let's make some fake ones.

def cast(_type, obj):
return obj

class _FakeType(object):
# make the objects subscriptable indefinetly
def __getitem__(self, item):
return _FakeType()

Optional = _FakeType()
Union = _FakeType()
Callable = _FakeType()
List = _FakeType()
Dict = _FakeType()
Any = _FakeType()
Tuple = _FakeType()
Pattern = _FakeType()

class Sized(object):
pass
Loading