From 1177e6d90777da92c72a1e88de93312f2ca1e6ff Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Tue, 13 Jun 2017 23:21:52 +0800 Subject: [PATCH] Add typesepc_to_str function to mitmproxy/utils/typechck.py --- mitmproxy/optmanager.py | 9 +-------- mitmproxy/utils/typecheck.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/mitmproxy/optmanager.py b/mitmproxy/optmanager.py index b67949e0c..4a4552826 100644 --- a/mitmproxy/optmanager.py +++ b/mitmproxy/optmanager.py @@ -425,14 +425,7 @@ def dump_dicts(opts): options_list = [] for k in sorted(opts.keys()): o = opts._options[k] - if o.typespec in (str, int, bool): - t = o.typespec.__name__ - elif o.typespec == typing.Optional[str]: - t = 'Union' - elif o.typespec == typing.Sequence[str]: - t = 'Sequence' - else: - raise NotImplementedError + t = typecheck.typespec_to_str(o.typespec) option = { 'name': k, 'type': t, diff --git a/mitmproxy/utils/typecheck.py b/mitmproxy/utils/typecheck.py index a5f27fee5..ed58182d4 100644 --- a/mitmproxy/utils/typecheck.py +++ b/mitmproxy/utils/typecheck.py @@ -98,3 +98,15 @@ def check_option_type(name: str, value: typing.Any, typeinfo: typing.Any) -> Non return elif not isinstance(value, typeinfo): raise e + + +def typespec_to_str(typespec: typing.Any) -> str: + if typespec in (str, int, bool): + t = typespec.__name__ + elif typespec == typing.Optional[str]: + t = 'Union' + elif typespec == typing.Sequence[str]: + t = 'Sequence' + else: + raise NotImplementedError + return t