Compare commits

..

4 Commits

Author SHA1 Message Date
Gergő Jedlicska 40956927c8 Merge pull request #250 from specklesystems/gergo/nonGenericTFix
fix(typing): fix non generic typedefed lists and tuples
2023-01-09 15:54:36 +01:00
Gergő Jedlicska 4628f111ba fix(type-checking): fix py >= 3.9 dict type checking 2023-01-09 15:53:15 +01:00
Gergő Jedlicska 9c952b432d fix(typing): fix non specificed generic types for py 3.7 py 3.8 2023-01-09 15:39:45 +01:00
Gergő Jedlicska f075988e4b fix(typing): fix non generic typedefed lists and tuples 2023-01-09 15:10:23 +01:00
2 changed files with 19 additions and 0 deletions
+16
View File
@@ -235,7 +235,15 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
return False, value
if value == {}:
return True, value
if not getattr(t, "__args__", None):
return True, value
t_key, t_value = t.__args__ # type: ignore
if (
getattr(t_key, "__name__", None),
getattr(t_value, "__name__", None),
) == ("KT", "VT"):
return True, value
# we're only checking the first item, but the for loop and return after
# evaluating the first item is the fastest way
for dict_key, dict_value in value.items():
@@ -251,7 +259,11 @@ 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
if getattr(t_items, "__name__", None) == "T":
return True, value
first_item_valid, _ = _validate_type(t_items, value[0])
if first_item_valid:
return True, value
@@ -260,7 +272,11 @@ 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
if args == tuple():
return True, value
# we're not checking for empty tuple, cause tuple lengths must match
if len(args) != len(value):
return False, value
+3
View File
@@ -64,7 +64,9 @@ 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, {"foo": 1}, True, {"foo": 1}),
(Dict[str, Optional[int]], {"foo": None}, True, {"foo": None}),
# this case should be
# (Dict[int, Base], {1: None}, False, {1: None}),
@@ -72,6 +74,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")),