From e24b4cc1b64455b8b9b5d1265103054bb8b3a8af Mon Sep 17 00:00:00 2001 From: harsh vijay Date: Tue, 2 May 2017 05:19:25 +0530 Subject: Extend Mypy checking to pathod * mypy checking pathod * initial commit , fixed errors * tox: mypy checking to pathod * Fixed mypy test failed * issue was with args in custom_contentview.py * tox: mypy checking to #2221 * follow-import=skip since we cant provide args to custom_contentview.py during mypy testing * Lint , Typo Fixed * code style: module import --- pathod/language/actions.py | 6 ++---- pathod/language/base.py | 15 +++++++-------- pathod/language/http.py | 4 ++-- pathod/language/http2.py | 6 +++--- pathod/language/message.py | 3 ++- pathod/language/websockets.py | 14 +++++++++++--- pathod/pathod.py | 6 ++---- pathod/test.py | 14 +++++++------- pathod/utils.py | 7 ++++--- 9 files changed, 40 insertions(+), 35 deletions(-) (limited to 'pathod') diff --git a/pathod/language/actions.py b/pathod/language/actions.py index e85affac..fc57a18b 100644 --- a/pathod/language/actions.py +++ b/pathod/language/actions.py @@ -2,9 +2,7 @@ import abc import copy import random from functools import total_ordering - import pyparsing as pp - from . import base @@ -52,7 +50,7 @@ class _Action(base.Token): class PauseAt(_Action): - unique_name = None + unique_name = None # type: ignore def __init__(self, offset, seconds): _Action.__init__(self, offset) @@ -103,7 +101,7 @@ class DisconnectAt(_Action): class InjectAt(_Action): - unique_name = None + unique_name = None # type: ignore def __init__(self, offset, value): _Action.__init__(self, offset) diff --git a/pathod/language/base.py b/pathod/language/base.py index 3a810ef0..c8892748 100644 --- a/pathod/language/base.py +++ b/pathod/language/base.py @@ -3,10 +3,9 @@ import os import abc import functools import pyparsing as pp - from mitmproxy.utils import strutils from mitmproxy.utils import human - +import typing # noqa from . import generators, exceptions @@ -84,7 +83,7 @@ class Token: return None @property - def unique_name(self): + def unique_name(self) -> typing.Optional[str]: """ Controls uniqueness constraints for tokens. No two tokens with the same name will be allowed. If no uniquness should be applied, this @@ -334,7 +333,7 @@ class OptionsOrValue(_Component): Can be any of a specified set of options, or a value specifier. """ preamble = "" - options = [] + options = [] # type: typing.List[str] def __init__(self, value): # If it's a string, we were passed one of the options, so we lower-case @@ -376,7 +375,7 @@ class OptionsOrValue(_Component): class Integer(_Component): - bounds = (None, None) + bounds = (None, None) # type: typing.Tuple[typing.Union[int, None], typing.Union[int , None]] preamble = "" def __init__(self, value): @@ -442,7 +441,7 @@ class FixedLengthValue(Value): A value component lead by an optional preamble. """ preamble = "" - length = None + length = None # type: typing.Optional[int] def __init__(self, value): Value.__init__(self, value) @@ -511,7 +510,7 @@ class IntField(_Component): """ An integer field, where values can optionally specified by name. """ - names = {} + names = {} # type: typing.Dict[str, int] max = 16 preamble = "" @@ -546,7 +545,7 @@ class NestedMessage(Token): A nested message, as an escaped string with a preamble. """ preamble = "" - nest_type = None + nest_type = None # type: ignore def __init__(self, value): Token.__init__(self) diff --git a/pathod/language/http.py b/pathod/language/http.py index 8fcf9edc..5cd717a9 100644 --- a/pathod/language/http.py +++ b/pathod/language/http.py @@ -54,7 +54,7 @@ class Method(base.OptionsOrValue): class _HeaderMixin: - unique_name = None + unique_name = None # type: ignore def format_header(self, key, value): return [key, b": ", value, b"\r\n"] @@ -143,7 +143,7 @@ class _HTTPMessage(message.Message): class Response(_HTTPMessage): - unique_name = None + unique_name = None # type: ignore comps = ( Header, ShortcutContentType, diff --git a/pathod/language/http2.py b/pathod/language/http2.py index 08c5f6d7..47d6e370 100644 --- a/pathod/language/http2.py +++ b/pathod/language/http2.py @@ -1,9 +1,9 @@ import pyparsing as pp - from mitmproxy.net import http from mitmproxy.net.http import user_agents, Headers from . import base, message + """ Normal HTTP requests: ::
: @@ -41,7 +41,7 @@ def get_header(val, headers): class _HeaderMixin: - unique_name = None + unique_name = None # type: ignore def values(self, settings): return ( @@ -146,7 +146,7 @@ class Times(base.Integer): class Response(_HTTP2Message): - unique_name = None + unique_name = None # type: ignore comps = ( Header, Body, diff --git a/pathod/language/message.py b/pathod/language/message.py index 6cdaaa0b..6b4c5021 100644 --- a/pathod/language/message.py +++ b/pathod/language/message.py @@ -1,13 +1,14 @@ import abc from . import actions, exceptions from mitmproxy.utils import strutils +import typing # noqa LOG_TRUNCATE = 1024 class Message: __metaclass__ = abc.ABCMeta - logattrs = [] + logattrs = [] # type: typing.List[str] def __init__(self, tokens): track = set([]) diff --git a/pathod/language/websockets.py b/pathod/language/websockets.py index a237381c..b4faf59b 100644 --- a/pathod/language/websockets.py +++ b/pathod/language/websockets.py @@ -4,6 +4,7 @@ import mitmproxy.net.websockets from mitmproxy.utils import strutils import pyparsing as pp from . import base, generators, actions, message +import typing # noqa NESTED_LEADER = b"pathod!" @@ -20,7 +21,7 @@ class OpCode(base.IntField): "close": mitmproxy.net.websockets.OPCODE.CLOSE, "ping": mitmproxy.net.websockets.OPCODE.PING, "pong": mitmproxy.net.websockets.OPCODE.PONG, - } + } # type: typing.Dict[str, int] max = 15 preamble = "c" @@ -239,7 +240,14 @@ class NestedFrame(base.NestedMessage): nest_type = WebsocketFrame +COMP = typing.Tuple[ + typing.Type[OpCode], typing.Type[Length], typing.Type[Fin], typing.Type[RSV1], typing.Type[RSV2], typing.Type[RSV3], typing.Type[Mask], + typing.Type[actions.PauseAt], typing.Type[actions.DisconnectAt], typing.Type[actions.InjectAt], typing.Type[KeyNone], typing.Type[Key], + typing.Type[Times], typing.Type[Body], typing.Type[RawBody] +] + + class WebsocketClientFrame(WebsocketFrame): - components = COMPONENTS + ( + components = typing.cast(COMP, COMPONENTS + ( NestedFrame, - ) + )) diff --git a/pathod/pathod.py b/pathod/pathod.py index 7416d325..7c773c3b 100644 --- a/pathod/pathod.py +++ b/pathod/pathod.py @@ -3,19 +3,17 @@ import logging import os import sys import threading - from mitmproxy.net import tcp from mitmproxy import certs as mcerts from mitmproxy.net import websockets from mitmproxy import version - import urllib from mitmproxy import exceptions - from pathod import language from pathod import utils from pathod import log from pathod import protocols +import typing # noqa DEFAULT_CERT_DOMAIN = b"pathod.net" @@ -71,7 +69,7 @@ class SSLOptions: class PathodHandler(tcp.BaseHandler): wbufsize = 0 - sni = None + sni = None # type: typing.Union[str, None, bool] def __init__( self, diff --git a/pathod/test.py b/pathod/test.py index 81f5805f..52f3ba02 100644 --- a/pathod/test.py +++ b/pathod/test.py @@ -1,16 +1,16 @@ import io import time import queue - from . import pathod from mitmproxy.types import basethread +import typing # noqa class Daemon: IFACE = "127.0.0.1" - def __init__(self, ssl=None, **daemonargs): - self.q = queue.Queue() + def __init__(self, ssl=None, **daemonargs) -> None: + self.q = queue.Queue() # type: queue.Queue self.logfp = io.StringIO() daemonargs["logfp"] = self.logfp self.thread = _PaThread(self.IFACE, self.q, ssl, daemonargs) @@ -25,18 +25,18 @@ class Daemon: def __enter__(self): return self - def __exit__(self, type, value, traceback): + def __exit__(self, type, value, traceback) -> bool: self.logfp.truncate(0) self.shutdown() return False - def p(self, spec): + def p(self, spec: str) -> str: """ Return a URL that will render the response in spec. """ return "%s/p/%s" % (self.urlbase, spec) - def text_log(self): + def text_log(self) -> str: return self.logfp.getvalue() def wait_for_silence(self, timeout=5): @@ -62,7 +62,7 @@ class Daemon: return None return l[-1] - def log(self): + def log(self) -> typing.List[typing.Dict]: """ Return the log buffer as a list of dictionaries. """ diff --git a/pathod/utils.py b/pathod/utils.py index 44ad1f87..11b1dccd 100644 --- a/pathod/utils.py +++ b/pathod/utils.py @@ -1,6 +1,7 @@ import os import sys from mitmproxy.utils import data as mdata +import typing # noqa class MemBool: @@ -9,10 +10,10 @@ class MemBool: Truth-checking with a memory, for use in chained if statements. """ - def __init__(self): - self.v = None + def __init__(self) -> None: + self.v = None # type: typing.Optional[bool] - def __call__(self, v): + def __call__(self, v: bool) -> bool: self.v = v return bool(v) -- cgit v1.2.3