Skip to content

Commit

Permalink
Run no_implicit_optional to rewrite types
Browse files Browse the repository at this point in the history
PEP484 prohibits implicit Optional types, so

    def bad(x: int = None):
        pass

Is invalid. MyPy since version 0.983 prohibits this usage by default.
So rewrite all of the typing (except x : Any = None) using
https://github.com/hauntsaninja/no_implicit_optional.
  • Loading branch information
wence- committed May 11, 2023
1 parent 6ffdef6 commit 040f577
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 133 deletions.
4 changes: 2 additions & 2 deletions python/cudf/cudf/_lib/column.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Column:
size: int,
dtype: Dtype,
mask: Optional[Buffer] = None,
offset: int = None,
null_count: int = None,
offset: Optional[int] = None,
null_count: Optional[int] = None,
children: Tuple[ColumnBase, ...] = (),
) -> None: ...
@property
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/buffer/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import math
import pickle
from types import SimpleNamespace
from typing import Any, Dict, Mapping, Sequence, Tuple, Type, TypeVar
from typing import Any, Dict, Mapping, Optional, Sequence, Tuple, Type, TypeVar

import numpy

Expand Down Expand Up @@ -42,7 +42,7 @@ def host_memory_allocation(nbytes: int) -> memoryview:
def cuda_array_interface_wrapper(
ptr: int,
size: int,
owner: object = None,
owner: Optional[object] = None,
readonly=False,
typestr="|u1",
version=0,
Expand Down
6 changes: 3 additions & 3 deletions python/cudf/cudf/core/buffer/spill_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2022, NVIDIA CORPORATION.
# Copyright (c) 2022-2023, NVIDIA CORPORATION.

from __future__ import annotations

Expand Down Expand Up @@ -225,7 +225,7 @@ def __init__(
self,
*,
spill_on_demand: bool = False,
device_memory_limit: int = None,
device_memory_limit: Optional[int] = None,
statistic_level: int = 0,
) -> None:
self._lock = threading.Lock()
Expand Down Expand Up @@ -358,7 +358,7 @@ def spill_device_memory(self, nbytes: int) -> int:
buf.lock.release()
return spilled

def spill_to_device_limit(self, device_limit: int = None) -> int:
def spill_to_device_limit(self, device_limit: Optional[int] = None) -> int:
"""Try to spill device memory until device limit
Notice, by default this is a no-op.
Expand Down
8 changes: 6 additions & 2 deletions python/cudf/cudf/core/buffer/spillable_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@ def __cuda_array_interface__(self) -> dict:
"version": 0,
}

def memoryview(self, *, offset: int = 0, size: int = None) -> memoryview:
def memoryview(
self, *, offset: int = 0, size: Optional[int] = None
) -> memoryview:
size = self._size if size is None else size
with self.lock:
if self.spillable:
Expand Down Expand Up @@ -573,7 +575,9 @@ def deserialize(cls, header: dict, frames: list):
# copied.
return SpillableBuffer.deserialize(header, frames)

def memoryview(self, *, offset: int = 0, size: int = None) -> memoryview:
def memoryview(
self, *, offset: int = 0, size: Optional[int] = None
) -> memoryview:
size = self._size if size is None else size
return self._base.memoryview(offset=self._offset + offset, size=size)

Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/buffer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
def as_buffer(
data: Union[int, Any],
*,
size: int = None,
owner: object = None,
size: Optional[int] = None,
owner: Optional[object] = None,
exposed: bool = False,
) -> Buffer:
"""Factory function to wrap `data` in a Buffer object.
Expand Down
21 changes: 13 additions & 8 deletions python/cudf/cudf/core/column/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,10 @@ class CategoricalColumn(column.ColumnBase):
def __init__(
self,
dtype: CategoricalDtype,
mask: Buffer = None,
size: int = None,
mask: Optional[Buffer] = None,
size: Optional[int] = None,
offset: int = 0,
null_count: int = None,
null_count: Optional[int] = None,
children: Tuple["column.ColumnBase", ...] = (),
):

Expand Down Expand Up @@ -889,7 +889,7 @@ def _fill(
return result

def slice(
self, start: int, stop: int, stride: int = None
self, start: int, stop: int, stride: Optional[int] = None
) -> "column.ColumnBase":
codes = self.codes.slice(start, stop, stride)
return cudf.core.column.build_categorical_column(
Expand Down Expand Up @@ -962,7 +962,9 @@ def __cuda_array_interface__(self) -> Mapping[str, Any]:
" if you need this functionality."
)

def to_pandas(self, index: pd.Index = None, **kwargs) -> pd.Series:
def to_pandas(
self, index: Optional[pd.Index] = None, **kwargs
) -> pd.Series:
if self.categories.dtype.kind == "f":
new_mask = bools_to_mask(self.notnull())
col = column.build_categorical_column(
Expand Down Expand Up @@ -1219,7 +1221,10 @@ def notnull(self) -> ColumnBase:
return result

def fillna(
self, fill_value: Any = None, method: Any = None, dtype: Dtype = None
self,
fill_value: Any = None,
method: Any = None,
dtype: Optional[Dtype] = None,
) -> CategoricalColumn:
"""
Fill null values with *fill_value*
Expand All @@ -1237,7 +1242,7 @@ def fillna(
try:
fill_value = self._encode(fill_value)
fill_value = self.codes.dtype.type(fill_value)
except (ValueError) as err:
except ValueError as err:
err_msg = "fill value must be in categories"
raise ValueError(err_msg) from err
else:
Expand Down Expand Up @@ -1641,7 +1646,7 @@ def _create_empty_categorical_column(


def pandas_categorical_as_column(
categorical: ColumnLike, codes: ColumnLike = None
categorical: ColumnLike, codes: Optional[ColumnLike] = None
) -> CategoricalColumn:
"""Creates a CategoricalColumn from a pandas.Categorical
Expand Down
69 changes: 39 additions & 30 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ def __repr__(self):
f"dtype: {self.dtype}"
)

def to_pandas(self, index: pd.Index = None, **kwargs) -> "pd.Series":
def to_pandas(
self, index: Optional[pd.Index] = None, **kwargs
) -> "pd.Series":
"""Convert object to pandas type.
The default implementation falls back to PyArrow for the conversion.
Expand Down Expand Up @@ -548,7 +550,9 @@ def element_indexing(self, index: int):

return libcudf.copying.get_element(self, idx).value

def slice(self, start: int, stop: int, stride: int = None) -> ColumnBase:
def slice(
self, start: int, stop: int, stride: Optional[int] = None
) -> ColumnBase:
stride = 1 if stride is None else stride
if start < 0:
start = start + len(self)
Expand Down Expand Up @@ -699,8 +703,8 @@ def _check_scatter_key_length(
def fillna(
self: T,
value: Any = None,
method: str = None,
dtype: Dtype = None,
method: Optional[str] = None,
dtype: Optional[Dtype] = None,
) -> T:
"""Fill null values with ``value``.
Expand Down Expand Up @@ -1097,7 +1101,6 @@ def apply_boolean_mask(self, mask) -> ColumnBase:
def argsort(
self, ascending: bool = True, na_position: str = "last"
) -> "cudf.core.column.NumericalColumn":

return self.as_frame()._get_sorted_inds(
ascending=ascending, na_position=na_position
)
Expand Down Expand Up @@ -1244,14 +1247,19 @@ def normalize_binop_value(
) -> Union[ColumnBase, ScalarLike]:
raise NotImplementedError

def _minmax(self, skipna: bool = None):
def _minmax(self, skipna: Optional[bool] = None):
result_col = self._process_for_reduction(skipna=skipna)
if isinstance(result_col, ColumnBase):
return libcudf.reduce.minmax(result_col)
return result_col

def _reduce(
self, op: str, skipna: bool = None, min_count: int = 0, *args, **kwargs
self,
op: str,
skipna: Optional[bool] = None,
min_count: int = 0,
*args,
**kwargs,
) -> ScalarLike:
"""Compute {op} of column values.
Expand All @@ -1273,7 +1281,7 @@ def contains_na_entries(self) -> bool:
return self.null_count != 0

def _process_for_reduction(
self, skipna: bool = None, min_count: int = 0
self, skipna: Optional[bool] = None, min_count: int = 0
) -> Union[ColumnBase, ScalarLike]:
skipna = True if skipna is None else skipna

Expand Down Expand Up @@ -1314,8 +1322,8 @@ def _with_type_metadata(self: ColumnBase, dtype: Dtype) -> ColumnBase:
def _label_encoding(
self,
cats: ColumnBase,
dtype: Dtype = None,
na_sentinel: ScalarLike = None,
dtype: Optional[Dtype] = None,
na_sentinel: Optional[ScalarLike] = None,
):
"""
Convert each value in `self` into an integer code, with `cats`
Expand Down Expand Up @@ -1389,9 +1397,9 @@ def _return_sentinel_column():

def column_empty_like(
column: ColumnBase,
dtype: Dtype = None,
dtype: Optional[Dtype] = None,
masked: bool = False,
newsize: int = None,
newsize: Optional[int] = None,
) -> ColumnBase:
"""Allocate a new column like the given *column*"""
if dtype is None:
Expand Down Expand Up @@ -1494,10 +1502,10 @@ def build_column(
data: Union[Buffer, None],
dtype: Dtype,
*,
size: int = None,
mask: Buffer = None,
size: Optional[int] = None,
mask: Optional[Buffer] = None,
offset: int = 0,
null_count: int = None,
null_count: Optional[int] = None,
children: Tuple[ColumnBase, ...] = (),
) -> ColumnBase:
"""
Expand Down Expand Up @@ -1666,10 +1674,10 @@ def build_column(
def build_categorical_column(
categories: ColumnBase,
codes: ColumnBase,
mask: Buffer = None,
size: int = None,
mask: Optional[Buffer] = None,
size: Optional[int] = None,
offset: int = 0,
null_count: int = None,
null_count: Optional[int] = None,
ordered: bool = False,
) -> "cudf.core.column.CategoricalColumn":
"""
Expand Down Expand Up @@ -1757,10 +1765,10 @@ def build_interval_column(
def build_list_column(
indices: ColumnBase,
elements: ColumnBase,
mask: Buffer = None,
size: int = None,
mask: Optional[Buffer] = None,
size: Optional[int] = None,
offset: int = 0,
null_count: int = None,
null_count: Optional[int] = None,
) -> "cudf.core.column.ListColumn":
"""
Build a ListColumn
Expand Down Expand Up @@ -1803,10 +1811,10 @@ def build_struct_column(
names: Sequence[str],
children: Tuple[ColumnBase, ...],
dtype: Optional[Dtype] = None,
mask: Buffer = None,
size: int = None,
mask: Optional[Buffer] = None,
size: Optional[int] = None,
offset: int = 0,
null_count: int = None,
null_count: Optional[int] = None,
) -> "cudf.core.column.StructColumn":
"""
Build a StructColumn
Expand Down Expand Up @@ -1863,9 +1871,9 @@ def _make_copy_replacing_NaT_with_null(column):

def as_column(
arbitrary: Any,
nan_as_null: bool = None,
dtype: Dtype = None,
length: int = None,
nan_as_null: Optional[bool] = None,
dtype: Optional[Dtype] = None,
length: Optional[int] = None,
):
"""Create a Column from an arbitrary object
Expand Down Expand Up @@ -2106,7 +2114,6 @@ def as_column(

data = build_column(data=buffer, mask=mask, dtype=arbitrary.dtype)
elif arb_dtype.kind == "m":

time_unit = get_time_unit(arbitrary)
cast_dtype = time_unit in ("D", "W", "M", "Y")

Expand Down Expand Up @@ -2466,7 +2473,7 @@ def deserialize_columns(headers: List[dict], frames: List) -> List[ColumnBase]:

def arange(
start: Union[int, float],
stop: Union[int, float] = None,
stop: Optional[Union[int, float]] = None,
step: Union[int, float] = 1,
dtype=None,
) -> cudf.core.column.NumericalColumn:
Expand Down Expand Up @@ -2524,7 +2531,9 @@ def arange(
)


def full(size: int, fill_value: ScalarLike, dtype: Dtype = None) -> ColumnBase:
def full(
size: int, fill_value: ScalarLike, dtype: Optional[Dtype] = None
) -> ColumnBase:
"""
Returns a column of given size and dtype, filled with a given value.
Expand Down
Loading

0 comments on commit 040f577

Please sign in to comment.