Skip to content

Commit e75f68e

Browse files
committed
#100 Make Series generic
1 parent 1cedf5f commit e75f68e

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

pandas_dataclasses/core/asdata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def asseries(obj: PandasClass[P, TSeries], *, factory: None = None) -> TSeries:
6262

6363

6464
@overload
65-
def asseries(obj: DataClass[P], *, factory: None = None) -> pd.Series:
65+
def asseries(obj: DataClass[P], *, factory: None = None) -> "pd.Series[Any]":
6666
...
6767

6868

pandas_dataclasses/core/mixins.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from copy import copy
66
from functools import wraps as wraps_
77
from types import FunctionType, MethodType
8-
from typing import Any, Callable, Generic, Type
8+
from typing import Any, Callable, ForwardRef, Generic, Type, cast
99

1010

1111
# dependencies
@@ -72,8 +72,15 @@ def __init_subclass__(cls, **kwargs: Any) -> None:
7272
super().__init_subclass__(**kwargs)
7373

7474
for base in cls.__orig_bases__: # type: ignore
75-
if get_origin(base) is As:
76-
cls.__pandas_factory__ = get_args(base)[0]
75+
if get_origin(base) is not As:
76+
continue
77+
78+
factory = get_args(base)[0]
79+
80+
if factory == ForwardRef("pd.Series[Any]"):
81+
cls.__pandas_factory__ = cast(Any, pd.Series)
82+
else:
83+
cls.__pandas_factory__ = factory
7784

7885
@classproperty
7986
def new(cls) -> Any:
@@ -98,5 +105,5 @@ def new(cls: Any, *args: Any, **kwargs: Any) -> Any:
98105
"""Alias of ``As[pandas.DataFrame]``."""
99106

100107

101-
AsSeries = As[pd.Series]
102-
"""Alias of ``As[pandas.Series]``."""
108+
AsSeries = As["pd.Series[Any]"]
109+
"""Alias of ``As[pandas.Series[Any]]``."""

pandas_dataclasses/core/specs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Spec:
8484
fields: Fields
8585
"""List of field specifications."""
8686

87-
factory: Optional[Type[Union[pd.DataFrame, pd.Series]]] = None
87+
factory: Optional[Type[Union[pd.DataFrame, "pd.Series[Any]"]]] = None
8888
"""Factory for pandas data creation."""
8989

9090
@classmethod

pandas_dataclasses/core/typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
# type hints (private)
3737
P = ParamSpec("P")
3838
T = TypeVar("T")
39-
TPandas = TypeVar("TPandas", bound=Union[pd.DataFrame, pd.Series])
39+
TPandas = TypeVar("TPandas", bound=Union[pd.DataFrame, "pd.Series[Any]"])
4040
TDataFrame = TypeVar("TDataFrame", bound=pd.DataFrame)
41-
TSeries = TypeVar("TSeries", bound=pd.Series)
41+
TSeries = TypeVar("TSeries", bound="pd.Series[Any]")
4242

4343

4444
class DataClass(Protocol[P]):

0 commit comments

Comments
 (0)