fix(typing): fix non generic typedefed lists and tuples

This commit is contained in:
Gergő Jedlicska
2023-01-09 15:10:23 +01:00
parent 65c829404a
commit f075988e4b
2 changed files with 6 additions and 0 deletions
+4
View File
@@ -251,6 +251,8 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
return False, value
if value == []:
return True, value
if not hasattr(t, "__args__"):
return True, value
t_items = t.__args__[0] # type: ignore
first_item_valid, _ = _validate_type(t_items, value[0])
if first_item_valid:
@@ -260,6 +262,8 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
if origin is tuple:
if not isinstance(value, tuple):
return False, value
if not hasattr(t, "__args__"):
return True, value
args = t.__args__ # type: ignore
# we're not checking for empty tuple, cause tuple lengths must match
if len(args) != len(value):
+2
View File
@@ -64,6 +64,7 @@ class FakeIntEnum(IntEnum):
# same as the dict typing below...
(List[int], [None, 2], True, [None, 2]),
(List[Optional[int]], [None, 2], True, [None, 2]),
(List, ["foo", 2, "bar"], True, ["foo", 2, "bar"]),
(Dict[str, int], {"foo": 1}, True, {"foo": 1}),
(Dict[str, Optional[int]], {"foo": None}, True, {"foo": None}),
# this case should be
@@ -72,6 +73,7 @@ class FakeIntEnum(IntEnum):
(Dict[int, Base], {1: None}, True, {1: None}),
(Dict[int, Base], {1: test_base}, True, {1: test_base}),
(Tuple[int, str, str], (1, "foo", "bar"), True, (1, "foo", "bar")),
(Tuple, (1, "foo", "bar"), True, (1, "foo", "bar")),
# given our current rules, this is the reality. Its just sad...
(Tuple[str, str, str], (1, "foo", "bar"), True, ("1", "foo", "bar")),
(Tuple[str, Optional[str], str], (1, None, "bar"), True, ("1", None, "bar")),