|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import dataclasses
|
4 |
| -from typing import Any, Optional, Type, TypeVar, Union |
5 |
| - |
6 |
| -from typing_extensions import get_args, get_origin |
| 4 | +from typing import Any, Optional, Type, TypeVar, Union, _GenericAlias |
7 | 5 |
|
8 | 6 | from flask_inputfilter.exceptions import ValidationError
|
9 | 7 | from flask_inputfilter.validators import BaseValidator
|
10 | 8 |
|
11 | 9 | T = TypeVar("T")
|
12 | 10 |
|
13 | 11 |
|
| 12 | +# TODO: Replace with typing.get_origin when Python 3.7 support is dropped. |
| 13 | +def get_origin(tp: Any) -> Optional[Type[Any]]: |
| 14 | + """Get the unsubscripted version of a type. |
| 15 | +
|
| 16 | + This supports typing types like List, Dict, etc. and their |
| 17 | + typing_extensions equivalents. |
| 18 | + """ |
| 19 | + if isinstance(tp, _GenericAlias): |
| 20 | + return tp.__origin__ |
| 21 | + return None |
| 22 | + |
| 23 | + |
| 24 | +# TODO: Replace with typing.get_args when Python 3.7 support is dropped. |
| 25 | +def get_args(tp: Any) -> tuple[Any, ...]: |
| 26 | + """Get type arguments with all substitutions performed. |
| 27 | +
|
| 28 | + For unions, basic types, and special typing forms, returns |
| 29 | + the type arguments. For example, for List[int] returns (int,). |
| 30 | + """ |
| 31 | + if isinstance(tp, _GenericAlias): |
| 32 | + return tp.__args__ |
| 33 | + return () |
| 34 | + |
| 35 | + |
14 | 36 | class IsDataclassValidator(BaseValidator):
|
15 | 37 | """
|
16 | 38 | Validates that the provided value conforms to a specific
|
|
0 commit comments