From 0929e74b4e82e3ee9ba1d6ddb7a54a68240a4282 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 28 Dec 2016 13:39:27 +0100 Subject: [PATCH 1/2] fix compat with Python 3.5.0 --- mitmproxy/utils/typecheck.py | 21 +++++++++++++-------- test/mitmproxy/utils/test_typecheck.py | 9 +++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/mitmproxy/utils/typecheck.py b/mitmproxy/utils/typecheck.py index 0b29f6a41..20af0150c 100644 --- a/mitmproxy/utils/typecheck.py +++ b/mitmproxy/utils/typecheck.py @@ -1,5 +1,4 @@ import typing -import sys def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None: @@ -25,10 +24,11 @@ def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None: typename = str(typeinfo) if typename.startswith("typing.Union"): - if sys.version_info < (3, 6): - types = typeinfo.__union_params__ - else: + try: types = typeinfo.__args__ + except AttributeError: + # Python 3.5.x + types = typeinfo.__union_params__ for T in types: try: @@ -39,10 +39,11 @@ def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None: return raise e elif typename.startswith("typing.Tuple"): - if sys.version_info < (3, 6): - types = typeinfo.__tuple_params__ - else: + try: types = typeinfo.__args__ + except AttributeError: + # Python 3.5.x + types = typeinfo.__tuple_params__ if not isinstance(value, (tuple, list)): raise e @@ -52,7 +53,11 @@ def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None: check_type("{}[{}]".format(attr_name, i), x, T) return elif typename.startswith("typing.Sequence"): - T = typeinfo.__args__[0] + try: + T = typeinfo.__args__[0] + except AttributeError: + # Python 3.5.0 + T = typeinfo.__parameters__[0] if not isinstance(value, (tuple, list)): raise e for v in value: diff --git a/test/mitmproxy/utils/test_typecheck.py b/test/mitmproxy/utils/test_typecheck.py index 3ec74b205..75c932e5d 100644 --- a/test/mitmproxy/utils/test_typecheck.py +++ b/test/mitmproxy/utils/test_typecheck.py @@ -1,5 +1,6 @@ import typing +import mock import pytest from mitmproxy.utils import typecheck @@ -57,3 +58,11 @@ def test_check_sequence(): typecheck.check_type("foo", [10, "foo"], typing.Sequence[int]) with pytest.raises(TypeError): typecheck.check_type("foo", [b"foo"], typing.Sequence[str]) + with pytest.raises(TypeError): + typecheck.check_type("foo", "foo", typing.Sequence[str]) + + # Python 3.5.0 only defines __parameters__ + m = mock.Mock() + m.__str__ = lambda self: "typing.Sequence" + m.__parameters__ = (int,) + typecheck.check_type("foo", [10], m) From eab360a02b13b0dcc659546ddde383cc5a5b88bb Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 28 Dec 2016 14:21:19 +0100 Subject: [PATCH 2/2] fix IO type checking --- mitmproxy/utils/typecheck.py | 2 ++ test/mitmproxy/utils/test_typecheck.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/mitmproxy/utils/typecheck.py b/mitmproxy/utils/typecheck.py index 20af0150c..ae1a507d0 100644 --- a/mitmproxy/utils/typecheck.py +++ b/mitmproxy/utils/typecheck.py @@ -65,6 +65,8 @@ def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None: elif typename.startswith("typing.IO"): if hasattr(value, "read"): return + else: + raise e elif not isinstance(value, typeinfo): raise e diff --git a/test/mitmproxy/utils/test_typecheck.py b/test/mitmproxy/utils/test_typecheck.py index 75c932e5d..0ed440ebc 100644 --- a/test/mitmproxy/utils/test_typecheck.py +++ b/test/mitmproxy/utils/test_typecheck.py @@ -1,7 +1,9 @@ +import io import typing import mock import pytest + from mitmproxy.utils import typecheck @@ -66,3 +68,9 @@ def test_check_sequence(): m.__str__ = lambda self: "typing.Sequence" m.__parameters__ = (int,) typecheck.check_type("foo", [10], m) + + +def test_check_io(): + typecheck.check_type("foo", io.StringIO(), typing.IO[str]) + with pytest.raises(TypeError): + typecheck.check_type("foo", "foo", typing.IO[str])