Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 40956927c8 | |||
| 4628f111ba | |||
| 9c952b432d | |||
| f075988e4b |
@@ -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
|
||||
|
||||
@@ -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")),
|
||||
|
||||
Reference in New Issue
Block a user