py36: fix type information

This commit is contained in:
Thomas Kriechbaumer 2016-12-26 19:59:29 +01:00 committed by Maximilian Hils
parent 0bde932b78
commit 51d57cfd4a

View File

@ -1,4 +1,5 @@
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:
@ -21,8 +22,15 @@ def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None:
type(value) type(value)
)) ))
if typeinfo.__qualname__ == "Union": typename = str(typeinfo)
for T in typeinfo.__union_params__:
if typename.startswith("typing.Union"):
if sys.version_info < (3, 6):
types = typeinfo.__union_params__
else:
types = typeinfo.__args__
for T in types:
try: try:
check_type(attr_name, value, T) check_type(attr_name, value, T)
except TypeError: except TypeError:
@ -30,21 +38,26 @@ def check_type(attr_name: str, value: typing.Any, typeinfo: type) -> None:
else: else:
return return
raise e raise e
elif typeinfo.__qualname__ == "Tuple": elif typename.startswith("typing.Tuple"):
if sys.version_info < (3, 6):
types = typeinfo.__tuple_params__
else:
types = typeinfo.__args__
if not isinstance(value, (tuple, list)): if not isinstance(value, (tuple, list)):
raise e raise e
if len(typeinfo.__tuple_params__) != len(value): if len(types) != len(value):
raise e raise e
for i, (x, T) in enumerate(zip(value, typeinfo.__tuple_params__)): for i, (x, T) in enumerate(zip(value, types)):
check_type("{}[{}]".format(attr_name, i), x, T) check_type("{}[{}]".format(attr_name, i), x, T)
return return
elif typeinfo.__qualname__ == "Sequence": elif typename.startswith("typing.Sequence"):
T = typeinfo.__args__[0] T = typeinfo.__args__[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:
check_type(attr_name, v, T) check_type(attr_name, v, T)
elif typeinfo.__qualname__ == "IO": elif typename.startswith("typing.IO"):
if hasattr(value, "read"): if hasattr(value, "read"):
return return
elif not isinstance(value, typeinfo): elif not isinstance(value, typeinfo):