From 695d528112d29a78f020a87fc1da348df15acee7 Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Fri, 12 May 2023 13:00:27 +0100 Subject: [PATCH 1/3] Remove need for most TypeVar(bound="foo") usage Now that we support mypy 1.3, we can use the PEP674 Self type annotation. --- python/cudf/cudf/_lib/column.pyi | 9 ++-- python/cudf/cudf/core/_base_index.py | 10 ++--- python/cudf/cudf/core/buffer/buffer.py | 11 +++-- python/cudf/cudf/core/buffer/cow_buffer.py | 12 +++--- .../cudf/cudf/core/buffer/spillable_buffer.py | 21 ++------- python/cudf/cudf/core/column/categorical.py | 29 +++++++------ python/cudf/cudf/core/column/column.py | 43 ++++++++++--------- python/cudf/cudf/core/dataframe.py | 7 +-- python/cudf/cudf/core/frame.py | 15 +++---- python/cudf/cudf/core/indexed_frame.py | 21 +++++---- python/cudf/cudf/core/single_column_frame.py | 4 +- python/cudf/cudf/core/tools/datetimes.py | 14 +++--- 12 files changed, 87 insertions(+), 109 deletions(-) diff --git a/python/cudf/cudf/_lib/column.pyi b/python/cudf/cudf/_lib/column.pyi index bd53801a972..93edef311db 100644 --- a/python/cudf/cudf/_lib/column.pyi +++ b/python/cudf/cudf/_lib/column.pyi @@ -4,12 +4,12 @@ from __future__ import annotations from typing import Dict, Optional, Tuple, TypeVar +from typing_extensions import Self + from cudf._typing import Dtype, DtypeObj, ScalarLike from cudf.core.buffer import Buffer from cudf.core.column import ColumnBase -T = TypeVar("T") - class Column: _data: Optional[Buffer] _mask: Optional[Buffer] @@ -56,7 +56,7 @@ class Column: @property def mask_ptr(self) -> int: ... def set_base_mask(self, value: Optional[Buffer]) -> None: ... - def set_mask(self: T, value: Optional[Buffer]) -> T: ... + def set_mask(self, value: Optional[Buffer]) -> Self: ... @property def null_count(self) -> int: ... @property @@ -68,7 +68,8 @@ class Column: def set_base_children(self, value: Tuple[ColumnBase, ...]) -> None: ... def _mimic_inplace( self, other_col: ColumnBase, inplace=False - ) -> Optional[ColumnBase]: ... + ) -> Optional[Self]: ... + # TODO: The val parameter should be Scalar, not ScalarLike @staticmethod def from_scalar(val: ScalarLike, size: int) -> ColumnBase: ... diff --git a/python/cudf/cudf/core/_base_index.py b/python/cudf/cudf/core/_base_index.py index 68c52a8b9e8..a2e3bc44f3a 100644 --- a/python/cudf/cudf/core/_base_index.py +++ b/python/cudf/cudf/core/_base_index.py @@ -5,9 +5,10 @@ import pickle import warnings from functools import cached_property -from typing import Any, Set, TypeVar +from typing import Any, Set import pandas as pd +from typing_extensions import Self import cudf from cudf._lib.copying import _gather_map_is_valid, gather @@ -62,8 +63,6 @@ Float64Index([1.0, 2.0, 3.0], dtype='float64') """ -BaseIndexT = TypeVar("BaseIndexT", bound="BaseIndex") - class BaseIndex(Serializable): """Base class for all cudf Index types.""" @@ -101,8 +100,8 @@ def __contains__(self, item): return item in self._values def _copy_type_metadata( - self: BaseIndexT, other: BaseIndexT, *, override_dtypes=None - ) -> BaseIndexT: + self, other: Self, *, override_dtypes=None + ) -> Self: raise NotImplementedError def get_level_values(self, level): @@ -1451,7 +1450,6 @@ def get_slice_bound(self, label, side, kind=None): raise NotImplementedError def __array_function__(self, func, types, args, kwargs): - # check if the function is implemented for the current type cudf_index_module = type(self) for submodule in func.__module__.split(".")[1:]: diff --git a/python/cudf/cudf/core/buffer/buffer.py b/python/cudf/cudf/core/buffer/buffer.py index abf1ec47e3d..97f3b16bec8 100644 --- a/python/cudf/cudf/core/buffer/buffer.py +++ b/python/cudf/cudf/core/buffer/buffer.py @@ -5,9 +5,10 @@ import math import pickle from types import SimpleNamespace -from typing import Any, Dict, Mapping, Optional, Sequence, Tuple, Type, TypeVar +from typing import Any, Dict, Mapping, Optional, Sequence, Tuple import numpy +from typing_extensions import Self import rmm @@ -15,8 +16,6 @@ from cudf.core.abc import Serializable from cudf.utils.string import format_bytes -T = TypeVar("T", bound="Buffer") - def host_memory_allocation(nbytes: int) -> memoryview: """Allocate host memory using NumPy @@ -108,7 +107,7 @@ def __init__(self): ) @classmethod - def _from_device_memory(cls: Type[T], data: Any) -> T: + def _from_device_memory(cls, data: Any) -> Self: """Create a Buffer from an object exposing `__cuda_array_interface__`. No data is being copied. @@ -139,7 +138,7 @@ def _from_device_memory(cls: Type[T], data: Any) -> T: return ret @classmethod - def _from_host_memory(cls: Type[T], data: Any) -> T: + def _from_host_memory(cls, data: Any) -> Self: """Create a Buffer from a buffer or array like object Data must implement `__array_interface__`, the buffer protocol, and/or @@ -310,7 +309,7 @@ def serialize(self) -> Tuple[dict, list]: return header, frames @classmethod - def deserialize(cls: Type[T], header: dict, frames: list) -> T: + def deserialize(cls, header: dict, frames: list) -> Self: """Create an Buffer from a serialized representation. Parameters diff --git a/python/cudf/cudf/core/buffer/cow_buffer.py b/python/cudf/cudf/core/buffer/cow_buffer.py index 16a1e3942e7..6243916b91b 100644 --- a/python/cudf/cudf/core/buffer/cow_buffer.py +++ b/python/cudf/cudf/core/buffer/cow_buffer.py @@ -4,15 +4,15 @@ import weakref from collections import defaultdict -from typing import Any, DefaultDict, Tuple, Type, TypeVar +from typing import Any, DefaultDict, Tuple from weakref import WeakSet +from typing_extensions import Self + import rmm from cudf.core.buffer.buffer import Buffer -T = TypeVar("T", bound="CopyOnWriteBuffer") - def _keys_cleanup(ptr): weak_set_values = CopyOnWriteBuffer._instances[ptr] @@ -55,9 +55,7 @@ def _finalize_init(self): weakref.finalize(self, _keys_cleanup, self._ptr) @classmethod - def _from_device_memory( - cls: Type[T], data: Any, *, exposed: bool = False - ) -> T: + def _from_device_memory(cls, data: Any, *, exposed: bool = False) -> Self: """Create a Buffer from an object exposing `__cuda_array_interface__`. No data is being copied. @@ -82,7 +80,7 @@ def _from_device_memory( return ret @classmethod - def _from_host_memory(cls: Type[T], data: Any) -> T: + def _from_host_memory(cls, data: Any) -> Self: ret = super()._from_host_memory(data) ret._finalize_init() return ret diff --git a/python/cudf/cudf/core/buffer/spillable_buffer.py b/python/cudf/cudf/core/buffer/spillable_buffer.py index 169b52b828e..c1bc49c7a9e 100644 --- a/python/cudf/cudf/core/buffer/spillable_buffer.py +++ b/python/cudf/cudf/core/buffer/spillable_buffer.py @@ -7,18 +7,10 @@ import time import weakref from threading import RLock -from typing import ( - TYPE_CHECKING, - Any, - Dict, - List, - Optional, - Tuple, - Type, - TypeVar, -) +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple import numpy +from typing_extensions import Self import rmm @@ -34,9 +26,6 @@ from cudf.core.buffer.spill_manager import SpillManager -T = TypeVar("T", bound="SpillableBuffer") - - def get_spillable_owner(data) -> Optional[SpillableBuffer]: """Get the spillable owner of `data`, if any exist @@ -212,9 +201,7 @@ def _finalize_init(self, ptr_desc: Dict[str, Any], exposed: bool) -> None: self._manager.add(self) @classmethod - def _from_device_memory( - cls: Type[T], data: Any, *, exposed: bool = False - ) -> T: + def _from_device_memory(cls, data: Any, *, exposed: bool = False) -> Self: """Create a spillabe buffer from device memory. No data is being copied. @@ -236,7 +223,7 @@ def _from_device_memory( return ret @classmethod - def _from_host_memory(cls: Type[T], data: Any) -> T: + def _from_host_memory(cls, data: Any) -> Self: """Create a spillabe buffer from host memory. Data must implement `__array_interface__`, the buffer protocol, and/or diff --git a/python/cudf/cudf/core/column/categorical.py b/python/cudf/cudf/core/column/categorical.py index c6d7f779884..39332807139 100644 --- a/python/cudf/cudf/core/column/categorical.py +++ b/python/cudf/cudf/core/column/categorical.py @@ -11,6 +11,7 @@ import pandas as pd import pyarrow as pa from numba import cuda +from typing_extensions import Self import cudf from cudf import _lib as libcudf @@ -716,7 +717,6 @@ def __init__( null_count: Optional[int] = None, children: Tuple["column.ColumnBase", ...] = (), ): - if size is None: for child in children: assert child.offset == 0 @@ -874,7 +874,7 @@ def _fill( begin: int, end: int, inplace: bool = False, - ) -> "column.ColumnBase": + ) -> Self: if end <= begin or begin >= self.size: return self if inplace else self.copy() @@ -890,17 +890,20 @@ def _fill( def slice( self, start: int, stop: int, stride: Optional[int] = None - ) -> "column.ColumnBase": + ) -> Self: codes = self.codes.slice(start, stop, stride) - return cudf.core.column.build_categorical_column( - categories=self.categories, - codes=cudf.core.column.build_column( - codes.base_data, dtype=codes.dtype + return cast( + Self, + cudf.core.column.build_categorical_column( + categories=self.categories, + codes=cudf.core.column.build_column( + codes.base_data, dtype=codes.dtype + ), + mask=codes.base_mask, + ordered=self.ordered, + size=codes.size, + offset=codes.offset, ), - mask=codes.base_mask, - ordered=self.ordered, - size=codes.size, - offset=codes.offset, ) def _binaryop(self, other: ColumnBinaryOperand, op: str) -> ColumnBase: @@ -1356,7 +1359,7 @@ def _get_decategorized_column(self) -> ColumnBase: out = out.set_mask(self.mask) return out - def copy(self, deep: bool = True) -> CategoricalColumn: + def copy(self, deep: bool = True) -> Self: result_col = super().copy(deep=deep) if deep: result_col.categories = libcudf.copying.copy_column( @@ -1370,7 +1373,7 @@ def memory_usage(self) -> int: def _mimic_inplace( self, other_col: ColumnBase, inplace: bool = False - ) -> Optional[ColumnBase]: + ) -> Optional[Self]: out = super()._mimic_inplace(other_col, inplace=inplace) if inplace and isinstance(other_col, CategoricalColumn): self._codes = other_col._codes diff --git a/python/cudf/cudf/core/column/column.py b/python/cudf/cudf/core/column/column.py index 6557001f884..5ca3cf43e97 100644 --- a/python/cudf/cudf/core/column/column.py +++ b/python/cudf/cudf/core/column/column.py @@ -2,6 +2,7 @@ from __future__ import annotations +import builtins import pickle import warnings from functools import cached_property @@ -25,6 +26,7 @@ import pandas as pd import pyarrow as pa from numba import cuda +from typing_extensions import Self import rmm @@ -98,9 +100,6 @@ from pandas.core.arrays._arrow_utils import ArrowIntervalType T = TypeVar("T", bound="ColumnBase") -# TODO: This workaround allows type hints for `slice`, since `slice` is a -# method in ColumnBase. -Slice = TypeVar("Slice", bound=slice) class ColumnBase(Column, Serializable, BinaryOperand, Reducible): @@ -246,11 +245,11 @@ def values(self) -> "cupy.ndarray": return cupy.asarray(self.data_array_view(mode="write")) def find_and_replace( - self: T, + self, to_replace: ColumnLike, replacement: ColumnLike, all_nan: bool = False, - ) -> T: + ) -> Self: raise NotImplementedError def clip(self, lo: ScalarLike, hi: ScalarLike) -> ColumnBase: @@ -399,7 +398,7 @@ def _fill( begin: int, end: int, inplace: bool = False, - ) -> Optional[ColumnBase]: + ) -> Optional[Self]: if end <= begin or begin >= self.size: return self if inplace else self.copy() @@ -439,15 +438,15 @@ def nullmask(self) -> Buffer: raise ValueError("Column has no null mask") return self.mask_array_view(mode="read") - def force_deep_copy(self: T) -> T: + def force_deep_copy(self) -> Self: """ A method to create deep copy irrespective of whether `copy-on-write` is enabled. """ result = libcudf.copying.copy_column(self) - return cast(T, result._with_type_metadata(self.dtype)) + return result._with_type_metadata(self.dtype) - def copy(self: T, deep: bool = True) -> T: + def copy(self, deep: bool = True) -> Self: """ Makes a copy of the Column. @@ -469,7 +468,7 @@ def copy(self: T, deep: bool = True) -> T: return self.force_deep_copy() else: return cast( - T, + Self, build_column( data=self.base_data if self.base_data is None @@ -611,8 +610,10 @@ def _wrap_binop_normalization(self, other): return self.normalize_binop_value(other) def _scatter_by_slice( - self, key: Slice, value: Union[cudf.core.scalar.Scalar, ColumnBase] - ) -> Optional[ColumnBase]: + self, + key: builtins.slice, + value: Union[cudf.core.scalar.Scalar, ColumnBase], + ) -> Optional[Self]: """If this function returns None, it's either a no-op (slice is empty), or the inplace replacement is already performed (fill-in-place). """ @@ -650,7 +651,7 @@ def _scatter_by_column( self, key: cudf.core.column.NumericalColumn, value: Union[cudf.core.scalar.Scalar, ColumnBase], - ) -> ColumnBase: + ) -> Self: if is_bool_dtype(key.dtype): # `key` is boolean mask if len(key) != len(self): @@ -701,11 +702,11 @@ def _check_scatter_key_length( raise ValueError(msg) def fillna( - self: T, + self, value: Any = None, method: Optional[str] = None, dtype: Optional[Dtype] = None, - ) -> T: + ) -> Self: """Fill null values with ``value``. Returns a copy with null filled. @@ -773,8 +774,8 @@ def quantile( raise TypeError(f"cannot perform quantile with type {self.dtype}") def take( - self: T, indices: ColumnBase, nullify: bool = False, check_bounds=True - ) -> T: + self, indices: ColumnBase, nullify: bool = False, check_bounds=True + ) -> Self: """Return Column by taking values from the corresponding *indices*. Skip bounds checking if check_bounds is False. @@ -782,7 +783,7 @@ def take( """ # Handle zero size if indices.size == 0: - return cast(T, column_empty_like(self, newsize=0)) + return cast(Self, column_empty_like(self, newsize=0)) # TODO: For performance, the check and conversion of gather map should # be done by the caller. This check will be removed in future release. @@ -1411,8 +1412,10 @@ def column_empty_like( and is_categorical_dtype(column.dtype) and dtype == column.dtype ): - column = cast("cudf.core.column.CategoricalColumn", column) - codes = column_empty_like(column.codes, masked=masked, newsize=newsize) + catcolumn = cast("cudf.core.column.CategoricalColumn", column) + codes = column_empty_like( + catcolumn.codes, masked=masked, newsize=newsize + ) return build_column( data=None, dtype=dtype, diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index 0fb82b8583c..2aa370ac8e5 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -22,7 +22,6 @@ Optional, Set, Tuple, - TypeVar, Union, ) @@ -37,6 +36,7 @@ from pandas.core.dtypes.common import is_float, is_integer from pandas.io.formats import console from pandas.io.formats.printing import pprint_thing +from typing_extensions import Self import cudf import cudf.core.common @@ -105,9 +105,6 @@ _external_only_api, ) -T = TypeVar("T", bound="DataFrame") - - _cupy_nan_methods_map = { "min": "nanmin", "max": "nanmax", @@ -1306,7 +1303,7 @@ def __delitem__(self, name): self._drop_column(name) @_cudf_nvtx_annotate - def _slice(self: T, arg: slice) -> T: + def _slice(self, arg: slice) -> Self: """ _slice : slice the frame as per the arg diff --git a/python/cudf/cudf/core/frame.py b/python/cudf/cudf/core/frame.py index 230a5054a00..26b1ee971da 100644 --- a/python/cudf/cudf/core/frame.py +++ b/python/cudf/cudf/core/frame.py @@ -16,7 +16,6 @@ MutableMapping, Optional, Tuple, - TypeVar, Union, ) @@ -24,6 +23,7 @@ import numpy as np import pandas as pd import pyarrow as pa +from typing_extensions import Self import cudf from cudf import _lib as libcudf @@ -45,8 +45,6 @@ from cudf.utils.dtypes import find_common_type from cudf.utils.utils import _array_ufunc, _cudf_nvtx_annotate -T = TypeVar("T", bound="Frame") - # TODO: It looks like Frame is missing a declaration of `copy`, need to add class Frame(BinaryOperand, Scannable): @@ -146,8 +144,8 @@ def _from_columns_like_self( return frame._copy_type_metadata(self, override_dtypes=override_dtypes) def _mimic_inplace( - self: T, result: T, inplace: bool = False - ) -> Optional[Frame]: + self, result: Self, inplace: bool = False + ) -> Optional[Self]: if inplace: for col in self._data: if col in result._data: @@ -1069,7 +1067,6 @@ def from_arrow(cls, data): # Handle dict arrays cudf_category_frame = {} if len(dict_indices): - dict_indices_table = pa.table(dict_indices) data = data.drop(dict_indices_table.column_names) indices_columns = libcudf.interop.from_arrow(dict_indices_table) @@ -1191,11 +1188,11 @@ def _positions_from_column_names(self, column_names): ] def _copy_type_metadata( - self: T, - other: T, + self, + other: Self, *, override_dtypes: Optional[abc.Iterable[Optional[Dtype]]] = None, - ) -> T: + ) -> Self: """ Copy type metadata from each column of `other` to the corresponding column of `self`. diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index 95931af038c..3c4d8d84c34 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -27,6 +27,7 @@ import cupy as cp import numpy as np import pandas as pd +from typing_extensions import Self import cudf import cudf._lib as libcudf @@ -224,8 +225,6 @@ def __init__(self, frame): _LocIndexerClass = TypeVar("_LocIndexerClass", bound="_FrameIndexer") _IlocIndexerClass = TypeVar("_IlocIndexerClass", bound="_FrameIndexer") -T = TypeVar("T", bound="IndexedFrame") - class IndexedFrame(Frame): """A frame containing an index. @@ -357,8 +356,8 @@ def _from_columns_like_self( ) def _mimic_inplace( - self: T, result: T, inplace: bool = False - ) -> Optional[Frame]: + self, result: Self, inplace: bool = False + ) -> Optional[Self]: if inplace: self._index = result._index return super()._mimic_inplace(result, inplace) @@ -441,7 +440,7 @@ def _scan(self, op, axis=None, skipna=True): results[name] = getattr(result_col, op)() return self._from_data(results, self._index) - def _check_data_index_length_match(self: T) -> None: + def _check_data_index_length_match(self) -> None: # Validate that the number of rows in the data matches the index if the # data is not empty. This is a helper for the constructor. if self._data.nrows > 0 and self._data.nrows != len(self._index): @@ -450,7 +449,7 @@ def _check_data_index_length_match(self: T) -> None: f"match length of index ({len(self._index)})" ) - def copy(self: T, deep: bool = True) -> T: + def copy(self, deep: bool = True) -> Self: """Make a copy of this object's indices and data. When ``deep=True`` (default), a new object will be created with a @@ -918,12 +917,12 @@ def clip(self, lower=None, upper=None, inplace=False, axis=1): return self._mimic_inplace(output, inplace=inplace) def _copy_type_metadata( - self: T, - other: T, + self, + other: Self, include_index: bool = True, *, override_dtypes: Optional[abc.Iterable[Optional[Dtype]]] = None, - ) -> T: + ) -> Self: """ Copy type metadata from each column of `other` to the corresponding column of `self`. @@ -2322,12 +2321,12 @@ def _n_largest_or_smallest(self, largest, n, columns, keep): raise ValueError('keep must be either "first", "last"') def _align_to_index( - self: T, + self, index: ColumnLike, how: str = "outer", sort: bool = True, allow_non_unique: bool = False, - ) -> T: + ) -> Self: index = cudf.core.index.as_index(index) if self.index.equals(index): diff --git a/python/cudf/cudf/core/single_column_frame.py b/python/cudf/cudf/core/single_column_frame.py index 037ac9c378e..5f07c57fc80 100644 --- a/python/cudf/cudf/core/single_column_frame.py +++ b/python/cudf/cudf/core/single_column_frame.py @@ -4,7 +4,7 @@ from __future__ import annotations import warnings -from typing import Any, Dict, Optional, Tuple, TypeVar, Union +from typing import Any, Dict, Optional, Tuple, Union import cupy import numpy as np @@ -20,8 +20,6 @@ from cudf.core.frame import Frame from cudf.utils.utils import NotIterable, _cudf_nvtx_annotate -T = TypeVar("T", bound="Frame") - class SingleColumnFrame(Frame, NotIterable): """A one-dimensional frame. diff --git a/python/cudf/cudf/core/tools/datetimes.py b/python/cudf/cudf/core/tools/datetimes.py index 92ef49e92d9..0ee9f511061 100644 --- a/python/cudf/cudf/core/tools/datetimes.py +++ b/python/cudf/cudf/core/tools/datetimes.py @@ -1,15 +1,16 @@ -# Copyright (c) 2019-2022, NVIDIA CORPORATION. +# Copyright (c) 2019-2023, NVIDIA CORPORATION. import math import re import warnings -from typing import Sequence, Type, TypeVar, Union +from typing import Sequence, Union import cupy as cp import numpy as np import pandas as pd import pandas.tseries.offsets as pd_offset from pandas.core.tools.datetimes import _unit_map +from typing_extensions import Self import cudf from cudf import _lib as libcudf @@ -373,9 +374,6 @@ def get_units(value): return value -_T = TypeVar("_T", bound="DateOffset") - - class DateOffset: """ An object used for binary ops where calendrical arithmetic @@ -647,7 +645,7 @@ def __repr__(self): return repr_str @classmethod - def _from_freqstr(cls: Type[_T], freqstr: str) -> _T: + def _from_freqstr(cls, freqstr: str) -> Self: """ Parse a string and return a DateOffset object expects strings of the form 3D, 25W, 10ms, 42ns, etc. @@ -669,9 +667,9 @@ def _from_freqstr(cls: Type[_T], freqstr: str) -> _T: @classmethod def _from_pandas_ticks_or_weeks( - cls: Type[_T], + cls, tick: Union[pd.tseries.offsets.Tick, pd.tseries.offsets.Week], - ) -> _T: + ) -> Self: return cls(**{cls._TICK_OR_WEEK_TO_UNITS[type(tick)]: tick.n}) def _maybe_as_fast_pandas_offset(self): From 1fd7214b3661e1afd0df228486425e2cbe4ea7ce Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Fri, 12 May 2023 14:30:19 +0100 Subject: [PATCH 2/3] Remove unnecessary type quotes --- python/cudf/cudf/core/column/column.py | 2 +- python/cudf/cudf/core/column/interval.py | 3 +-- python/cudf/cudf/core/column/numerical.py | 2 +- python/cudf/cudf/core/column/string.py | 2 +- python/cudf/cudf/core/column/struct.py | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/python/cudf/cudf/core/column/column.py b/python/cudf/cudf/core/column/column.py index 5ca3cf43e97..62920c78f79 100644 --- a/python/cudf/cudf/core/column/column.py +++ b/python/cudf/cudf/core/column/column.py @@ -202,7 +202,7 @@ def __repr__(self): def to_pandas( self, index: Optional[pd.Index] = None, **kwargs - ) -> "pd.Series": + ) -> pd.Series: """Convert object to pandas type. The default implementation falls back to PyArrow for the conversion. diff --git a/python/cudf/cudf/core/column/interval.py b/python/cudf/cudf/core/column/interval.py index 1b9caa42ecf..38384d09126 100644 --- a/python/cudf/cudf/core/column/interval.py +++ b/python/cudf/cudf/core/column/interval.py @@ -21,7 +21,6 @@ def __init__( children=(), closed="right", ): - super().__init__( data=None, dtype=dtype, @@ -128,7 +127,7 @@ def as_interval_column(self, dtype, **kwargs): def to_pandas( self, index: Optional[pd.Index] = None, **kwargs - ) -> "pd.Series": + ) -> pd.Series: # Note: This does not handle null values in the interval column. # However, this exact sequence (calling __from_arrow__ on the output of # self.to_arrow) is currently the best known way to convert interval diff --git a/python/cudf/cudf/core/column/numerical.py b/python/cudf/cudf/core/column/numerical.py index 840858c4bdb..39798320f88 100644 --- a/python/cudf/cudf/core/column/numerical.py +++ b/python/cudf/cudf/core/column/numerical.py @@ -723,7 +723,7 @@ def to_pandas( index: Optional[pd.Index] = None, nullable: bool = False, **kwargs, - ) -> "pd.Series": + ) -> pd.Series: if nullable and self.dtype in np_dtypes_to_pandas_dtypes: pandas_nullable_dtype = np_dtypes_to_pandas_dtypes[self.dtype] arrow_array = self.to_arrow() diff --git a/python/cudf/cudf/core/column/string.py b/python/cudf/cudf/core/column/string.py index 8e83d0c72b6..a3163f1cebe 100644 --- a/python/cudf/cudf/core/column/string.py +++ b/python/cudf/cudf/core/column/string.py @@ -5632,7 +5632,7 @@ def to_pandas( index: Optional[pd.Index] = None, nullable: bool = False, **kwargs, - ) -> "pd.Series": + ) -> pd.Series: if nullable: pandas_array = pd.StringDtype().__from_arrow__(self.to_arrow()) pd_series = pd.Series(pandas_array, copy=False) diff --git a/python/cudf/cudf/core/column/struct.py b/python/cudf/cudf/core/column/struct.py index 6306bd1f32d..8558faa7f24 100644 --- a/python/cudf/cudf/core/column/struct.py +++ b/python/cudf/cudf/core/column/struct.py @@ -60,7 +60,7 @@ def to_arrow(self): def to_pandas( self, index: Optional[pd.Index] = None, **kwargs - ) -> "pd.Series": + ) -> pd.Series: # We cannot go via Arrow's `to_pandas` because of the following issue: # https://issues.apache.org/jira/browse/ARROW-12680 From 9796934ac9a4f8cbce9cf1f860942cf3fcafae32 Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Fri, 12 May 2023 14:34:48 +0100 Subject: [PATCH 3/3] Remove more TypeVar usage --- python/cudf/cudf/core/column/column.py | 3 --- python/cudf/cudf/core/index.py | 5 +---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/python/cudf/cudf/core/column/column.py b/python/cudf/cudf/core/column/column.py index 62920c78f79..607bf83ff6c 100644 --- a/python/cudf/cudf/core/column/column.py +++ b/python/cudf/cudf/core/column/column.py @@ -16,7 +16,6 @@ Optional, Sequence, Tuple, - TypeVar, Union, cast, ) @@ -99,8 +98,6 @@ else: from pandas.core.arrays._arrow_utils import ArrowIntervalType -T = TypeVar("T", bound="ColumnBase") - class ColumnBase(Column, Serializable, BinaryOperand, Reducible): _VALID_REDUCTIONS = { diff --git a/python/cudf/cudf/core/index.py b/python/cudf/cudf/core/index.py index 0ec06f8d81f..f5dc8298bd2 100644 --- a/python/cudf/cudf/core/index.py +++ b/python/cudf/cudf/core/index.py @@ -15,7 +15,6 @@ Optional, Tuple, Type, - TypeVar, Union, ) @@ -65,8 +64,6 @@ ) from cudf.utils.utils import _cudf_nvtx_annotate, search_range -T = TypeVar("T", bound="Frame") - def _lexsorted_equal_range( idx: Union[GenericIndex, cudf.MultiIndex], @@ -1048,7 +1045,7 @@ def _from_data( def _binaryop( self, - other: T, + other: Frame, op: str, fill_value: Any = None, *args,