fix IO type checking

This commit is contained in:
Maximilian Hils
2016-12-28 14:21:19 +01:00
parent 0929e74b4e
commit eab360a02b
2 changed files with 10 additions and 0 deletions

View File

@@ -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

View File

@@ -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])