Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1fb9a4f5fe | |||
| 1668c80bed |
@@ -289,6 +289,17 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
|
||||
values.append(item_value)
|
||||
return True, tuple(values)
|
||||
|
||||
if origin is set:
|
||||
if not isinstance(value, set):
|
||||
return False, value
|
||||
if not hasattr(t, "__args__"):
|
||||
return True, value
|
||||
t_items = t.__args__[0] # type: ignore
|
||||
first_item_valid, _ = _validate_type(t_items, next(iter(value)))
|
||||
if first_item_valid:
|
||||
return True, value
|
||||
return False, value
|
||||
|
||||
if isinstance(value, t):
|
||||
return True, value
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from enum import Enum, IntEnum
|
||||
from typing import Any, Dict, List, Optional, Tuple, Union
|
||||
from typing import Any, Dict, List, Optional, Set, Tuple, Union
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -88,6 +88,14 @@ fake_bases = [FakeBase("foo"), FakeBase("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")),
|
||||
(Set[bool], set([1, 2]), False, set([1, 2])),
|
||||
(Set[int], set([1, 2]), True, set([1, 2])),
|
||||
(Set[int], set([None, 2]), True, set([None, 2])),
|
||||
# not testing this, since order of input iterables in sets are not preserved
|
||||
# easily produces false reports since we're only checking the type of the
|
||||
# first item
|
||||
# (Set[int], set(["None", 2]), False, set(["None", 2])),
|
||||
(Set[Optional[int]], set([None, 2]), True, set([None, 2])),
|
||||
(Optional[Union[List[int], List[FakeBase]]], None, True, None),
|
||||
(Optional[Union[List[int], List[FakeBase]]], "foo", False, "foo"),
|
||||
(Union[List[int], List[FakeBase], None], "foo", False, "foo"),
|
||||
|
||||
Reference in New Issue
Block a user