Skip to content

Commit 9ecb7ec

Browse files
committed
♻️ refactor: Use standard typing functions over old typing_inspect
1 parent 53af0fb commit 9ecb7ec

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

pandera/api/extensions.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
from enum import Enum
66
from functools import partial, wraps
77
from inspect import signature
8-
from typing import Callable, Optional, Union
9-
10-
import typing_inspect
8+
from typing import Callable, Optional, Union, get_args, get_origin
119

1210
from pandera.api.checks import Check
1311
from pandera.api.hypotheses import Hypothesis
@@ -56,11 +54,11 @@ def register_builtin_check(
5654
# object to validate is the first argument.
5755
data_type = [*fn_sig.parameters.values()][0].annotation
5856

59-
if typing_inspect.get_origin(data_type) is tuple:
60-
data_type, *_ = typing_inspect.get_args(data_type)
57+
if get_origin(data_type) is tuple:
58+
data_type, *_ = get_args(data_type)
6159

62-
if typing_inspect.get_origin(data_type) is Union:
63-
data_types = typing_inspect.get_args(data_type)
60+
if get_origin(data_type) is Union:
61+
data_types = get_args(data_type)
6462
else:
6563
data_types = (data_type,)
6664

pandera/api/function_dispatch.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""Multidispatcher implementation."""
22

33
from inspect import signature
4-
from typing import Callable, Union
5-
import typing_inspect
4+
from typing import Callable, Union, get_args, get_origin
65

76

87
class Dispatcher:
@@ -52,11 +51,11 @@ def get_first_arg_type(fn):
5251
# object to validate is the first argument.
5352
data_type = [*fn_sig.parameters.values()][0].annotation
5453

55-
if typing_inspect.get_origin(data_type) in (tuple, tuple):
56-
data_type, *_ = typing_inspect.get_args(data_type)
54+
if get_origin(data_type) in (tuple, tuple):
55+
data_type, *_ = get_args(data_type)
5756

58-
if typing_inspect.get_origin(data_type) is Union:
59-
data_types = typing_inspect.get_args(data_type)
57+
if get_origin(data_type) is Union:
58+
data_types = get_args(data_type)
6059
else:
6160
data_types = (data_type,)
6261

pandera/engines/engine.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,9 @@
66
import sys
77
from abc import ABCMeta
88
from dataclasses import dataclass
9-
from typing import (
10-
TYPE_CHECKING,
11-
Any,
12-
Callable,
13-
NamedTuple,
14-
Optional,
15-
TypeVar,
16-
)
9+
from typing import TYPE_CHECKING, Any, Callable, NamedTuple, Optional, TypeVar, get_args, get_origin, get_type_hints
1710

1811
import typing_inspect
19-
from typing import get_type_hints
2012

2113
from pandera.dtypes import DataType
2214

@@ -139,7 +131,7 @@ def _register_from_parametrized_dtype(
139131
annotations = get_type_hints(func).values()
140132
dtype = next(iter(annotations)) # get 1st annotation
141133
# parse typing.Union
142-
dtypes = typing_inspect.get_args(dtype) or [dtype]
134+
dtypes = get_args(dtype) or [dtype]
143135

144136
def _method(*args, **kwargs):
145137
return func(pandera_dtype_cls, *args, **kwargs)
@@ -243,7 +235,7 @@ def dtype(cls: "Engine", data_type: Any) -> DataType:
243235
registry = cls._registry[cls]
244236

245237
# handle python generic types, e.g. typing.Dict[str, str]
246-
datatype_origin = typing_inspect.get_origin(data_type)
238+
datatype_origin = get_origin(data_type)
247239
if datatype_origin is not None:
248240
equivalent_data_type = registry.get_equivalent(datatype_origin)
249241
return type(equivalent_data_type)(data_type) # type: ignore

pandera/typing/common.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
TypeVar,
1212
Union,
1313
_GenericAlias,
14+
get_args,
15+
get_origin,
1416
)
1517

1618
import typing_inspect
@@ -217,12 +219,12 @@ def _parse_annotation(self, raw_annotation: type) -> None:
217219
if self.optional and typing_inspect.is_union_type(raw_annotation):
218220
# Annotated with Optional or Union[..., NoneType]
219221
# get_args -> (pandera.typing.Index[str], <class 'NoneType'>)
220-
raw_annotation = typing_inspect.get_args(raw_annotation)[0]
222+
raw_annotation = get_args(raw_annotation)[0]
221223
self.raw_annotation = raw_annotation
222224

223-
self.origin = typing_inspect.get_origin(raw_annotation)
225+
self.origin = get_origin(raw_annotation)
224226
# Replace empty tuple returned from get_args by None
225-
args = typing_inspect.get_args(raw_annotation) or None
227+
args = get_args(raw_annotation) or None
226228
self.args = args
227229
self.arg = args[0] if args else args
228230

@@ -236,14 +238,14 @@ def _parse_annotation(self, raw_annotation: type) -> None:
236238
metadata = None
237239

238240
elif metadata := getattr(self.arg, "__metadata__", None):
239-
self.arg = typing_inspect.get_args(self.arg)[0]
241+
self.arg = get_args(self.arg)[0]
240242

241243
self.metadata = metadata
242244

243-
self.literal = typing_inspect.get_origin(self.arg) is typing.Literal
245+
self.literal = get_origin(self.arg) is typing.Literal
244246

245247
if self.literal:
246-
self.arg = typing_inspect.get_args(self.arg)[0]
248+
self.arg = get_args(self.arg)[0]
247249
elif self.origin is None and self.metadata is None:
248250
if isinstance(raw_annotation, type) and issubclass(
249251
raw_annotation, SeriesBase

0 commit comments

Comments
 (0)