Skip to content

Commit 4934a35

Browse files
committed
48 | Remove typing_extensions dep
1 parent 8e0bb6d commit 4934a35

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

flask_inputfilter/validators/is_dataclass_validator.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
from __future__ import annotations
22

33
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
75

86
from flask_inputfilter.exceptions import ValidationError
97
from flask_inputfilter.validators import BaseValidator
108

119
T = TypeVar("T")
1210

1311

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+
1436
class IsDataclassValidator(BaseValidator):
1537
"""
1638
Validates that the provided value conforms to a specific

0 commit comments

Comments
 (0)