Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for additional metaclasses of proxies and use for ExcelWriter #15399

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions python/cudf/cudf/pandas/_wrappers/pandas.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# SPDX-FileCopyrightText: Copyright (c) 2023-2024, NVIDIA CORPORATION & AFFILIATES.
# All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import abc
import copyreg
import importlib
import os
import pickle
import sys

Expand Down Expand Up @@ -857,7 +859,12 @@ def Index__new__(cls, *args, **kwargs):
pd.ExcelWriter,
fast_to_slow=_Unusable(),
slow_to_fast=_Unusable(),
additional_attributes={"__hash__": _FastSlowAttribute("__hash__")},
additional_attributes={
"__hash__": _FastSlowAttribute("__hash__"),
"__fspath__": _FastSlowAttribute("__fspath__"),
},
bases=(os.PathLike,),
metaclasses=(abc.ABCMeta,),
)

try:
Expand Down Expand Up @@ -1032,7 +1039,7 @@ def holiday_calendar_factory_wrapper(*args, **kwargs):
fast_to_slow=_Unusable(),
slow_to_fast=_Unusable(),
additional_attributes={"__hash__": _FastSlowAttribute("__hash__")},
meta_class=pd_HolidayCalendarMetaClass,
metaclasses=(pd_HolidayCalendarMetaClass,),
)

Holiday = make_final_proxy_type(
Expand Down
30 changes: 11 additions & 19 deletions python/cudf/cudf/pandas/fast_slow_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,6 @@ def __call__(self):
_DELETE = object()


def create_composite_metaclass(base_meta, additional_meta):
"""
Dynamically creates a composite metaclass that inherits from both provided metaclasses.
This ensures that the metaclass behaviors of both base_meta and additional_meta are preserved.
"""

class CompositeMeta(base_meta, additional_meta):
def __new__(cls, name, bases, namespace):
return super().__new__(cls, name, bases, namespace)

return CompositeMeta


def make_final_proxy_type(
name: str,
fast_type: type,
Expand All @@ -130,7 +117,7 @@ def make_final_proxy_type(
additional_attributes: Mapping[str, Any] | None = None,
postprocess: Callable[[_FinalProxy, Any, Any], Any] | None = None,
bases: Tuple = (),
meta_class=None,
metaclasses: Tuple = (),
) -> Type[_FinalProxy]:
"""
Defines a fast-slow proxy type for a pair of "final" fast and slow
Expand Down Expand Up @@ -161,6 +148,8 @@ def make_final_proxy_type(
construct said unwrapped object. See also `_maybe_wrap_result`.
bases
Optional tuple of base classes to insert into the mro.
metaclasses
Optional tuple of metaclasses to unify with the base proxy metaclass.
Notes
-----
Expand Down Expand Up @@ -241,15 +230,18 @@ def _fsproxy_state(self) -> _State:
cls_dict[slow_name] = _FastSlowAttribute(
slow_name, private=slow_name.startswith("_")
)
if meta_class is None:
meta_class = _FastSlowProxyMeta
else:
meta_class = create_composite_metaclass(_FastSlowProxyMeta, meta_class)

metaclass = _FastSlowProxyMeta
if metaclasses:
metaclass = types.new_class( # type: ignore
f"{name}_Meta",
metaclasses + (_FastSlowProxyMeta,),
{},
)
cls = types.new_class(
name,
(*bases, _FinalProxy),
{"metaclass": meta_class},
{"metaclass": metaclass},
lambda ns: ns.update(cls_dict),
)
functools.update_wrapper(
Expand Down
5 changes: 5 additions & 0 deletions python/cudf/cudf_pandas_tests/test_cudf_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import copy
import datetime
import operator
import os
import pathlib
import pickle
import tempfile
Expand Down Expand Up @@ -1421,3 +1422,7 @@ def test_holidays_within_dates(holiday, start, expected):
utc.localize(xpd.Timestamp(start)),
)
) == [utc.localize(dt) for dt in expected]


def test_excelwriter_pathlike():
assert isinstance(pd.ExcelWriter("foo.xlsx"), os.PathLike)
Loading