forked from pytest-dev/pytest
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
59 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from .compat import TYPE_CHECKING | ||
|
||
# from more_itertools import collapse | ||
|
||
if TYPE_CHECKING: | ||
from typing import Optional | ||
from typing import Tuple | ||
from typing import TypeVar | ||
from typing import Union | ||
|
||
_T = TypeVar("_T", bound="type") | ||
|
||
|
||
def collapse_tuples(obj) -> "Tuple": | ||
def walk(node): | ||
if isinstance(node, tuple): | ||
for child in node: | ||
yield from walk(child) | ||
else: | ||
yield node | ||
|
||
return tuple(x for x in walk(obj)) | ||
|
||
|
||
def validate_tup_type( | ||
type_or_types: "Union[_T, Tuple[_T, ...]]", base_type: "_T" | ||
) -> "Optional[Tuple[_T, ...]]": | ||
if type_or_types is None: | ||
return None | ||
types = collapse_tuples(type_or_types) | ||
for exc in types: | ||
if not isinstance(exc, type) or not issubclass(exc, base_type): | ||
raise TypeError( | ||
"exceptions must be derived from {}, not {}".format( | ||
base_type.__name__, | ||
exc.__name__ if isinstance(exc, type) else type(exc).__name__, | ||
) | ||
) | ||
return types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters