mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 08:11:00 +00:00
Merge pull request #1896 from mhils/3.5.0-compat
Fix compat with Python 3.5.0
This commit is contained in:
commit
973406f327
@ -1,5 +1,4 @@
|
|||||||
import typing
|
import typing
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None:
|
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)
|
typename = str(typeinfo)
|
||||||
|
|
||||||
if typename.startswith("typing.Union"):
|
if typename.startswith("typing.Union"):
|
||||||
if sys.version_info < (3, 6):
|
try:
|
||||||
types = typeinfo.__union_params__
|
|
||||||
else:
|
|
||||||
types = typeinfo.__args__
|
types = typeinfo.__args__
|
||||||
|
except AttributeError:
|
||||||
|
# Python 3.5.x
|
||||||
|
types = typeinfo.__union_params__
|
||||||
|
|
||||||
for T in types:
|
for T in types:
|
||||||
try:
|
try:
|
||||||
@ -39,10 +39,11 @@ def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None:
|
|||||||
return
|
return
|
||||||
raise e
|
raise e
|
||||||
elif typename.startswith("typing.Tuple"):
|
elif typename.startswith("typing.Tuple"):
|
||||||
if sys.version_info < (3, 6):
|
try:
|
||||||
types = typeinfo.__tuple_params__
|
|
||||||
else:
|
|
||||||
types = typeinfo.__args__
|
types = typeinfo.__args__
|
||||||
|
except AttributeError:
|
||||||
|
# Python 3.5.x
|
||||||
|
types = typeinfo.__tuple_params__
|
||||||
|
|
||||||
if not isinstance(value, (tuple, list)):
|
if not isinstance(value, (tuple, list)):
|
||||||
raise e
|
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)
|
check_type("{}[{}]".format(attr_name, i), x, T)
|
||||||
return
|
return
|
||||||
elif typename.startswith("typing.Sequence"):
|
elif typename.startswith("typing.Sequence"):
|
||||||
|
try:
|
||||||
T = typeinfo.__args__[0]
|
T = typeinfo.__args__[0]
|
||||||
|
except AttributeError:
|
||||||
|
# Python 3.5.0
|
||||||
|
T = typeinfo.__parameters__[0]
|
||||||
if not isinstance(value, (tuple, list)):
|
if not isinstance(value, (tuple, list)):
|
||||||
raise e
|
raise e
|
||||||
for v in value:
|
for v in value:
|
||||||
@ -60,6 +65,8 @@ def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None:
|
|||||||
elif typename.startswith("typing.IO"):
|
elif typename.startswith("typing.IO"):
|
||||||
if hasattr(value, "read"):
|
if hasattr(value, "read"):
|
||||||
return
|
return
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
elif not isinstance(value, typeinfo):
|
elif not isinstance(value, typeinfo):
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
import io
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from mitmproxy.utils import typecheck
|
from mitmproxy.utils import typecheck
|
||||||
|
|
||||||
|
|
||||||
@ -57,3 +60,17 @@ def test_check_sequence():
|
|||||||
typecheck.check_type("foo", [10, "foo"], typing.Sequence[int])
|
typecheck.check_type("foo", [10, "foo"], typing.Sequence[int])
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
typecheck.check_type("foo", [b"foo"], typing.Sequence[str])
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
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])
|
||||||
|
Loading…
Reference in New Issue
Block a user