forked from pandas-dev/pandas-stubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
27 lines (20 loc) · 805 Bytes
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from __future__ import annotations
from typing import (
TYPE_CHECKING,
Final,
)
from pandas._typing import T
TYPE_CHECKING_INVALID_USAGE: Final = TYPE_CHECKING
def check(actual: T, klass: type, dtype: type | None = None, attr: str = "left") -> T:
if not isinstance(actual, klass):
raise RuntimeError(f"Expected type '{klass}' but got '{type(actual)}'")
if dtype is None:
return actual # type: ignore[return-value]
if hasattr(actual, "__iter__"):
value = next(iter(actual)) # type: ignore[call-overload]
else:
assert hasattr(actual, attr)
value = getattr(actual, attr)
if not isinstance(value, dtype):
raise RuntimeError(f"Expected type '{dtype}' but got '{type(value)}'")
return actual # type: ignore[return-value]