From 0f6c1e2a69315523aafeb25716323f11e5e36f97 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 8 Nov 2023 16:34:22 -0800 Subject: [PATCH 1/4] Ensure slow private attrs are maybe proxies --- python/cudf/cudf/pandas/fast_slow_proxy.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/python/cudf/cudf/pandas/fast_slow_proxy.py b/python/cudf/cudf/pandas/fast_slow_proxy.py index 3dc6a59cc16..cbc4b7a6616 100644 --- a/python/cudf/cudf/pandas/fast_slow_proxy.py +++ b/python/cudf/cudf/pandas/fast_slow_proxy.py @@ -584,7 +584,24 @@ def __getattr__(self, name: str) -> Any: _raise_attribute_error(self.__class__.__name__, name) if name.startswith("_"): # private attributes always come from `._fsproxy_slow`: - return getattr(self._fsproxy_slow, name) + obj = getattr(self._fsproxy_slow, name) + if name.startswith("__array"): + # TODO: numpy methods raise when given proxy ndarray objects + # https://numpy.org/doc/stable/reference/arrays.classes.html#special-attributes-and-methods # noqa:E501 + return obj + + if not _is_function_or_method(obj): + return _maybe_wrap_result( + obj, getattr, self._fsproxy_slow, name + ) + + @functools.wraps(obj) + def _wrapped_private_slow(*args, **kwargs): + slow_args, slow_kwargs = _slow_arg(args), _slow_arg(kwargs) + result = obj(*slow_args, **slow_kwargs) + return _maybe_wrap_result(result, obj, *args, **kwargs) + + return _wrapped_private_slow attr = _FastSlowAttribute(name) return attr.__get__(self) From ab0fa8cbce7f3736a40709caaf043cf53e48ed8c Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 8 Nov 2023 16:38:15 -0800 Subject: [PATCH 2/4] Ensure slow private attrs are maybe proxies --- .../cudf_pandas_tests/test_cudf_pandas.py | 7 +++++ .../cudf_pandas_tests/test_fast_slow_proxy.py | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/python/cudf/cudf_pandas_tests/test_cudf_pandas.py b/python/cudf/cudf_pandas_tests/test_cudf_pandas.py index 2500ba07bd9..0ba489ab4b1 100644 --- a/python/cudf/cudf_pandas_tests/test_cudf_pandas.py +++ b/python/cudf/cudf_pandas_tests/test_cudf_pandas.py @@ -1103,6 +1103,13 @@ def test_dataframe_query(): tm.assert_equal(actual, expected) +def test_private_method_result_wrapped(): + xoffset = xpd.offsets.Day() + dt = datetime.datetime(2020, 1, 1) + result = xoffset._apply(dt) + assert type(result) is xpd.Timestamp + + def test_numpy_var(): np.random.seed(42) data = np.random.rand(1000) diff --git a/python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py b/python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py index b964dfde4ed..b78d290e3ff 100644 --- a/python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py +++ b/python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py @@ -445,6 +445,35 @@ def __radd__(self, other): assert BarProxy() + Foo() == "sum" +def test_slow_attr_still_proxy(): + class A: + pass + + class B: + @property + def _private(self): + return A() + + pxy_a = make_final_proxy_type( + "A", + _Unusable, + A, + fast_to_slow=_Unusable(), + slow_to_fast=_Unusable(), + ) + + pxy_b = make_final_proxy_type( + "B", + _Unusable, + B, + fast_to_slow=_Unusable(), + slow_to_fast=_Unusable(), + ) + + result = pxy_b()._private + assert isinstance(result, pxy_a) + + def tuple_with_attrs(name, fields: list[str], extra_fields: set[str]): # Build a tuple-like class with some extra attributes and a custom # pickling scheme with __getnewargs_ex__ From e53fefb5c2e092cbaac58dcb49ee9276449f32bd Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 31 Jan 2024 13:48:35 -0800 Subject: [PATCH 3/4] Add copyright --- python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py b/python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py index b78d290e3ff..631ad2f37b2 100644 --- a/python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py +++ b/python/cudf/cudf_pandas_tests/test_fast_slow_proxy.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. +# SPDX-FileCopyrightText: Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. # All rights reserved. # SPDX-License-Identifier: Apache-2.0 From 6a36cc312a71044b163c5b9d7313f787ca86e939 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 21 Feb 2024 15:30:38 -0800 Subject: [PATCH 4/4] Update python/cudf/cudf_pandas_tests/test_cudf_pandas.py Co-authored-by: Bradley Dice --- python/cudf/cudf_pandas_tests/test_cudf_pandas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/cudf/cudf_pandas_tests/test_cudf_pandas.py b/python/cudf/cudf_pandas_tests/test_cudf_pandas.py index ac2fa8cd3f1..0386ec434da 100644 --- a/python/cudf/cudf_pandas_tests/test_cudf_pandas.py +++ b/python/cudf/cudf_pandas_tests/test_cudf_pandas.py @@ -1082,7 +1082,7 @@ def test_private_method_result_wrapped(): xoffset = xpd.offsets.Day() dt = datetime.datetime(2020, 1, 1) result = xoffset._apply(dt) - assert type(result) is xpd.Timestamp + assert isinstance(result, xpd.Timestamp) def test_numpy_var():