Skip to content

Commit

Permalink
fix Python 3.6 variable type annotations mitmproxy#3053
Browse files Browse the repository at this point in the history
  • Loading branch information
obscure76 committed Apr 14, 2018
1 parent 5eb17bb commit 0e984e1
Show file tree
Hide file tree
Showing 60 changed files with 149 additions and 149 deletions.
8 changes: 4 additions & 4 deletions examples/complex/har_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
from mitmproxy.utils import strutils
from mitmproxy.net.http import cookies

HAR = {} # type: typing.Dict
HAR: typing.Dict = {}

# A list of server seen till now is maintained so we can avoid
# using 'connect' time for entries that use an existing connection.
SERVERS_SEEN = set() # type: typing.Set[connections.ServerConnection]
SERVERS_SEEN: typing.Set[connections.ServerConnection] = set()


def load(l):
Expand Down Expand Up @@ -155,12 +155,12 @@ def done():
Called once on script shutdown, after any other events.
"""
if ctx.options.hardump:
json_dump = json.dumps(HAR, indent=2) # type: str
json_dump: str = json.dumps(HAR, indent=2)

if ctx.options.hardump == '-':
mitmproxy.ctx.log(json_dump)
else:
raw = json_dump.encode() # type: bytes
raw: bytes = json_dump.encode()
if ctx.options.hardump.endswith('.zhar'):
raw = zlib.compress(raw, 9)

Expand Down
2 changes: 1 addition & 1 deletion examples/complex/sslstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mitmproxy import http

# set of SSL/TLS capable hosts
secure_hosts = set() # type: typing.Set[str]
secure_hosts: typing.Set[str] = set()


def request(flow: http.HTTPFlow) -> None:
Expand Down
4 changes: 2 additions & 2 deletions examples/complex/xss_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def getValue(attrs: List[Tuple[str, str]], attrName: str) -> Optional[str]:
return None

class ScriptURLExtractor(HTMLParser):
script_URLs = [] # type: List[str]
script_URLs: List[str] = []

def handle_starttag(self, tag, attrs):
if (tag == "script" or tag == "iframe") and "src" in [name for name, value in attrs]:
Expand Down Expand Up @@ -254,7 +254,7 @@ def remove_last_occurence_of_sub_string(string: str, substr: str) -> str:

class PathHTMLParser(HTMLParser):
currentPath = ""
paths = [] # type: List[str]
paths: List[str] = []

def handle_starttag(self, tag, attrs):
self.currentPath += ("/" + tag)
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/filter_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Filter:
def __init__(self):
self.filter = None # type: flowfilter.TFilter
self.filter: flowfilter.TFilter = None

def configure(self, updated):
self.filter = flowfilter.parse(ctx.options.flowfilter)
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/io_write_dumpfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class Writer:
def __init__(self, path: str) -> None:
self.f = open(path, "wb") # type: typing.IO[bytes]
self.f: typing.IO[bytes] = open(path, "wb")
self.w = io.FlowWriter(self.f)

def response(self, flow: http.HTTPFlow) -> None:
Expand Down
2 changes: 1 addition & 1 deletion mitmproxy/addons/clientplayback.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class ClientPlayback:
def __init__(self):
self.flows = [] # type: typing.List[flow.Flow]
self.flows: typing.List[flow.Flow] = []
self.current_thread = None
self.configured = False

Expand Down
2 changes: 1 addition & 1 deletion mitmproxy/addons/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def flow_set(
"""
Quickly set a number of common values on flows.
"""
val = sval # type: typing.Union[int, str]
val: typing.Union[int, str] = sval
if spec == "status_code":
try:
val = int(val) # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/addons/cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def is_addr(v):

def extract(cut: str, f: flow.Flow) -> typing.Union[str, bytes]:
path = cut.split(".")
current = f # type: typing.Any
current: typing.Any = f
for i, spec in enumerate(path):
if spec.startswith("_"):
raise exceptions.CommandError("Can't access internal attribute %s" % spec)
Expand Down Expand Up @@ -65,7 +65,7 @@ def cut(
or "false", "bytes" are preserved, and all other values are
converted to strings.
"""
ret = [] # type:typing.List[typing.List[typing.Union[str, bytes]]]
ret: typing.List[typing.List[typing.Union[str, bytes]]] = []
for f in flows:
ret.append([extract(c, f) for c in cuts])
return ret # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/addons/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def colorful(line, styles):

class Dumper:
def __init__(self, outfile=sys.stdout):
self.filter = None # type: flowfilter.TFilter
self.outfp = outfile # type: typing.io.TextIO
self.filter: flowfilter.TFilter = None
self.outfp: typing.io.TextIO = outfile

def load(self, loader):
loader.add_option(
Expand Down
2 changes: 1 addition & 1 deletion mitmproxy/addons/eventstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class EventStore:
def __init__(self, size=10000):
self.data = collections.deque(maxlen=size) # type: typing.Deque[LogEntry]
self.data: typing.Deque[LogEntry] = collections.deque(maxlen=size)
self.sig_add = blinker.Signal()
self.sig_refresh = blinker.Signal()

Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/addons/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def file(self, fmt: str, f: flow.Flow, path: mitmproxy.types.Path) -> None:
"""
if fmt not in formats:
raise exceptions.CommandError("No such export format: %s" % fmt)
func = formats[fmt] # type: typing.Any
func: typing.Any = formats[fmt]
v = func(f)
try:
with open(path, "wb") as fp:
Expand All @@ -95,7 +95,7 @@ def clip(self, fmt: str, f: flow.Flow) -> None:
"""
if fmt not in formats:
raise exceptions.CommandError("No such export format: %s" % fmt)
func = formats[fmt] # type: typing.Any
func: typing.Any = formats[fmt]
v = strutils.always_str(func(f))
try:
pyperclip.copy(v)
Expand Down
2 changes: 1 addition & 1 deletion mitmproxy/addons/proxyauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self):
self.singleuser = None
self.ldapconn = None
self.ldapserver = None
self.authenticated = weakref.WeakKeyDictionary() # type: MutableMapping[connections.ClientConnection, Tuple[str, str]]
self.authenticated: MutableMapping[connections.ClientConnection, Tuple[str, str]] = weakref.WeakKeyDictionary()
"""Contains all connections that are permanently authenticated after an HTTP CONNECT"""

def load(self, loader):
Expand Down
2 changes: 1 addition & 1 deletion mitmproxy/addons/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Save:
def __init__(self):
self.stream = None
self.filt = None
self.active_flows = set() # type: Set[flow.Flow]
self.active_flows: typing.Set[flow.Flow] = set()

def load(self, loader):
loader.add_option(
Expand Down
2 changes: 1 addition & 1 deletion mitmproxy/addons/serverplayback.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _hash(self, flow):
_, _, path, _, query, _ = urllib.parse.urlparse(r.url)
queriesArray = urllib.parse.parse_qsl(query, keep_blank_values=True)

key = [str(r.port), str(r.scheme), str(r.method), str(path)] # type: List[Any]
key: typing.List[typing.Any] = [str(r.port), str(r.scheme), str(r.method), str(path)]
if not ctx.options.server_replay_ignore_content:
if ctx.options.server_replay_ignore_payload_params and r.multipart_form:
key.extend(
Expand Down
6 changes: 3 additions & 3 deletions mitmproxy/addons/stickycookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def domain_match(a: str, b: str) -> bool:

class StickyCookie:
def __init__(self):
self.jar = collections.defaultdict(dict) # type: Dict[TOrigin, Dict[str, str]]
self.flt = None # type: Optional[flowfilter.TFilter]
self.jar: Dict[TOrigin, Dict[str, str]] = collections.defaultdict(dict)
self.flt: Optional[flowfilter.TFilter] = None

def load(self, loader):
loader.add_option(
Expand Down Expand Up @@ -73,7 +73,7 @@ def response(self, flow: http.HTTPFlow):

def request(self, flow: http.HTTPFlow):
if self.flt:
cookie_list = [] # type: List[Tuple[str,str]]
cookie_list: List[Tuple[str, str]] = []
if flowfilter.match(self.flt, flow):
for (domain, port, path), c in self.jar.items():
match = [
Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/addons/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ class Focus:
"""
def __init__(self, v: View) -> None:
self.view = v
self._flow = None # type: mitmproxy.flow.Flow
self._flow: mitmproxy.flow.Flow = None
self.sig_change = blinker.Signal()
if len(self.view):
self.flow = self.view[0]
Expand Down Expand Up @@ -589,7 +589,7 @@ def _sig_view_add(self, view, flow):
class Settings(collections.Mapping):
def __init__(self, view: View) -> None:
self.view = view
self._values = {} # type: typing.MutableMapping[str, typing.Dict]
self._values: typing.MutableMapping[str, typing.Dict] = {}
view.sig_store_remove.connect(self._sig_store_remove)
view.sig_store_refresh.connect(self._sig_store_refresh)

Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __init__(
self.default_ca = default_ca
self.default_chain_file = default_chain_file
self.dhparams = dhparams
self.certs = {} # type: typing.Dict[TCertId, CertStoreEntry]
self.certs: typing.Dict[TCertId, CertStoreEntry] = {}
self.expire_queue = []

def expire(self, entry):
Expand Down Expand Up @@ -298,7 +298,7 @@ def get_cert(self, commonname: typing.Optional[bytes], sans: typing.List[bytes])
sans: A list of Subject Alternate Names.
"""

potential_keys = [] # type: typing.List[TCertId]
potential_keys: typing.List[TCertId] = []
if commonname:
potential_keys.extend(self.asterisk_forms(commonname))
for s in sans:
Expand Down
14 changes: 7 additions & 7 deletions mitmproxy/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def signature_help(self) -> str:
def prepare_args(self, args: typing.Sequence[str]) -> typing.List[typing.Any]:
verify_arg_signature(self.func, list(args), {})

remainder = [] # type: typing.Sequence[str]
remainder: typing.Sequence[str] = []
if self.has_positional:
remainder = args[len(self.paramtypes) - 1:]
args = args[:len(self.paramtypes) - 1]
Expand Down Expand Up @@ -121,7 +121,7 @@ def call(self, args: typing.Sequence[str]) -> typing.Any:
class CommandManager(mitmproxy.types._CommandBase):
def __init__(self, master):
self.master = master
self.commands = {} # type: typing.Dict[str, Command]
self.commands: typing.Dict[str, Command] = {}

def collect_commands(self, addon):
for i in dir(addon):
Expand All @@ -146,7 +146,7 @@ def parse_partial(
Parse a possibly partial command. Return a sequence of ParseResults and a sequence of remainder type help items.
"""
buf = io.StringIO(cmdstr)
parts = [] # type: typing.List[str]
parts: typing.List[str] = []
lex = lexer(buf)
while 1:
remainder = cmdstr[buf.tell():]
Expand All @@ -163,9 +163,9 @@ def parse_partial(
elif cmdstr.endswith(" "):
parts.append("")

parse = [] # type: typing.List[ParseResult]
params = [] # type: typing.List[type]
typ = None # type: typing.Type
parse: typing.List[ParseResult] = []
params: typing.List[type] = []
typ: typing.Type = None
for i in range(len(parts)):
if i == 0:
typ = mitmproxy.types.Cmd
Expand Down Expand Up @@ -197,7 +197,7 @@ def parse_partial(
)
)

remhelp = [] # type: typing.List[str]
remhelp: typing.List[str] = []
for x in params:
remt = mitmproxy.types.CommandTypes.get(x, None)
remhelp.append(remt.display)
Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/contentviews/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
)
from .base import View, VIEW_CUTOFF, KEY_MAX, format_text, format_dict, TViewResult

views = [] # type: List[View]
content_types_map = {} # type: Dict[str, List[View]]
views: List[View] = []
content_types_map: Dict[str, List[View]] = {}


def get(name: str) -> Optional[View]:
Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/contentviews/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


class View:
name = None # type: str
content_types = [] # type: typing.List[str]
name: str = None
content_types: typing.List[str] = []

def __call__(self, data: bytes, **metadata) -> TViewResult:
"""
Expand Down
2 changes: 1 addition & 1 deletion mitmproxy/contentviews/xml_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def done(self):


def tokenize(data: str) -> Iterable[Token]:
token = Text("") # type: Token
token: Token = Text("")

i = 0

Expand Down
4 changes: 2 additions & 2 deletions mitmproxy/ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import mitmproxy.options # noqa

master = None # type: mitmproxy.master.Master
log = None # type: mitmproxy.log.Log
options = None # type: mitmproxy.options.Options
log: mitmproxy.log.Log = None
options: mitmproxy.options.Options = None
4 changes: 2 additions & 2 deletions mitmproxy/eventsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def _iterate_tcp(f: tcp.TCPFlow) -> TEventGenerator:
yield "tcp_end", f


_iterate_map = {
_iterate_map: typing.Dict[typing.Type[flow.Flow], typing.Callable[[typing.Any], TEventGenerator]] = {
http.HTTPFlow: _iterate_http,
websocket.WebSocketFlow: _iterate_websocket,
tcp.TCPFlow: _iterate_tcp,
} # type: typing.Dict[typing.Type[flow.Flow], typing.Callable[[typing.Any], TEventGenerator]]
}


def iterate(f: flow.Flow) -> TEventGenerator:
Expand Down
12 changes: 6 additions & 6 deletions mitmproxy/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ def __init__(
self.server_conn = server_conn
self.live = live

self.error = None # type: typing.Optional[Error]
self.intercepted = False # type: bool
self._backup = None # type: typing.Optional[Flow]
self.reply = None # type: typing.Optional[controller.Reply]
self.marked = False # type: bool
self.metadata = dict() # type: typing.Dict[str, typing.Any]
self.error: typing.Optional[Error] = None
self.intercepted: bool = False
self._backup: typing.Optional[Flow] = None
self.reply: typing.Optional[controller.Reply] = None
self.marked: bool = False
self.metadata: typing.Dict[str, typing.Any] = dict()

_stateobject_attributes = dict(
id=str,
Expand Down
12 changes: 6 additions & 6 deletions mitmproxy/flowfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def dump(self, indent=0, fp=sys.stdout):


class _Action(_Token):
code = None # type: str
help = None # type: str
code: str = None
help: str = None

@classmethod
def make(klass, s, loc, toks):
Expand Down Expand Up @@ -434,7 +434,7 @@ def __call__(self, f):
return not self.itm(f)


filter_unary = [
filter_unary: Sequence[Type[_Action]] = [
FAsset,
FErr,
FHTTP,
Expand All @@ -443,8 +443,8 @@ def __call__(self, f):
FResp,
FTCP,
FWebSocket,
] # type: Sequence[Type[_Action]]
filter_rex = [
]
filter_rex: Sequence[Type[_Rex]] = [
FBod,
FBodRequest,
FBodResponse,
Expand All @@ -459,7 +459,7 @@ def __call__(self, f):
FMethod,
FSrc,
FUrl,
] # type: Sequence[Type[_Rex]]
]
filter_int = [
FCode
]
Expand Down
Loading

0 comments on commit 0e984e1

Please sign in to comment.