Skip to content

Commit

Permalink
Add ruff rules to avoid importing from typing (rapidsai#16040)
Browse files Browse the repository at this point in the history
Enabled the following ruff rules to update typing annotations according to PEP585 and PEP604

https://docs.astral.sh/ruff/rules/future-rewritable-type-annotation/
https://docs.astral.sh/ruff/rules/non-pep604-annotation/
https://docs.astral.sh/ruff/rules/non-pep585-annotation/

The changes were made by running `pre-commit run ruff --all-files` with `fix = True` and `unsafe-fixes = True` locally

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - Lawrence Mitchell (https://github.com/wence-)
  - Mike Sarahan (https://github.com/msarahan)

URL: rapidsai#16040
  • Loading branch information
mroeschke authored Jun 17, 2024
1 parent 107753c commit 87f6a7e
Show file tree
Hide file tree
Showing 58 changed files with 504 additions and 610 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ quiet-level = 3
line-length = 79

[tool.ruff.lint]
select = ["E", "F", "W", "D201", "D204", "D206", "D207", "D208", "D209", "D210", "D211", "D214", "D215", "D300", "D301", "D403", "D405", "D406", "D407", "D408", "D409", "D410", "D411", "D412", "D414", "D418", "TCH"]
select = ["E", "F", "W", "D201", "D204", "D206", "D207", "D208", "D209", "D210", "D211", "D214", "D215", "D300", "D301", "D403", "D405", "D406", "D407", "D408", "D409", "D410", "D411", "D412", "D414", "D418", "TCH", "FA", "UP006", "UP007"]
ignore = [
# whitespace before :
"E203",
Expand Down
46 changes: 22 additions & 24 deletions python/cudf/cudf/_lib/column.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@

from __future__ import annotations

from typing import Dict, Optional, Tuple

from typing_extensions import Self

from cudf._typing import Dtype, DtypeObj, ScalarLike
from cudf.core.buffer import Buffer
from cudf.core.column import ColumnBase

class Column:
_data: Optional[Buffer]
_mask: Optional[Buffer]
_base_data: Optional[Buffer]
_base_mask: Optional[Buffer]
_data: Buffer | None
_mask: Buffer | None
_base_data: Buffer | None
_base_mask: Buffer | None
_dtype: DtypeObj
_size: int
_offset: int
_null_count: int
_children: Tuple[ColumnBase, ...]
_base_children: Tuple[ColumnBase, ...]
_distinct_count: Dict[bool, int]
_children: tuple[ColumnBase, ...]
_base_children: tuple[ColumnBase, ...]
_distinct_count: dict[bool, int]

def __init__(
self,
data: Optional[Buffer],
data: Buffer | None,
size: int,
dtype: Dtype,
mask: Optional[Buffer] = None,
offset: Optional[int] = None,
null_count: Optional[int] = None,
children: Tuple[ColumnBase, ...] = (),
mask: Buffer | None = None,
offset: int | None = None,
null_count: int | None = None,
children: tuple[ColumnBase, ...] = (),
) -> None: ...
@property
def base_size(self) -> int: ...
Expand All @@ -40,35 +38,35 @@ class Column:
@property
def size(self) -> int: ...
@property
def base_data(self) -> Optional[Buffer]: ...
def base_data(self) -> Buffer | None: ...
@property
def data(self) -> Optional[Buffer]: ...
def data(self) -> Buffer | None: ...
@property
def data_ptr(self) -> int: ...
def set_base_data(self, value: Buffer) -> None: ...
@property
def nullable(self) -> bool: ...
def has_nulls(self, include_nan: bool = False) -> bool: ...
@property
def base_mask(self) -> Optional[Buffer]: ...
def base_mask(self) -> Buffer | None: ...
@property
def mask(self) -> Optional[Buffer]: ...
def mask(self) -> Buffer | None: ...
@property
def mask_ptr(self) -> int: ...
def set_base_mask(self, value: Optional[Buffer]) -> None: ...
def set_mask(self, value: Optional[Buffer]) -> Self: ...
def set_base_mask(self, value: Buffer | None) -> None: ...
def set_mask(self, value: Buffer | None) -> Self: ...
@property
def null_count(self) -> int: ...
@property
def offset(self) -> int: ...
@property
def base_children(self) -> Tuple[ColumnBase, ...]: ...
def base_children(self) -> tuple[ColumnBase, ...]: ...
@property
def children(self) -> Tuple[ColumnBase, ...]: ...
def set_base_children(self, value: Tuple[ColumnBase, ...]) -> None: ...
def children(self) -> tuple[ColumnBase, ...]: ...
def set_base_children(self, value: tuple[ColumnBase, ...]) -> None: ...
def _mimic_inplace(
self, other_col: ColumnBase, inplace=False
) -> Optional[Self]: ...
) -> Self | None: ...

# TODO: The val parameter should be Scalar, not ScalarLike
@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import abc
from functools import wraps
from inspect import isclass
from typing import List, Union, cast
from typing import cast

import cupy as cp
import numpy as np
Expand Down Expand Up @@ -219,7 +219,7 @@ def wrapped_func(obj):


def _union_categoricals(
to_union: List[Union[cudf.Series, cudf.CategoricalIndex]],
to_union: list[cudf.Series | cudf.CategoricalIndex],
sort_categories: bool = False,
ignore_order: bool = False,
):
Expand Down
6 changes: 3 additions & 3 deletions python/cudf/cudf/core/_base_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pickle
import warnings
from functools import cached_property
from typing import TYPE_CHECKING, Any, Literal, Set, Tuple
from typing import TYPE_CHECKING, Any, Literal

import pandas as pd
from typing_extensions import Self
Expand Down Expand Up @@ -44,11 +44,11 @@
class BaseIndex(Serializable):
"""Base class for all cudf Index types."""

_accessors: Set[Any] = set()
_accessors: set[Any] = set()
_data: ColumnAccessor

@property
def _columns(self) -> Tuple[Any, ...]:
def _columns(self) -> tuple[Any, ...]:
raise NotImplementedError

@cached_property
Expand Down
12 changes: 6 additions & 6 deletions python/cudf/cudf/core/_internals/expressions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright (c) 2022-2023, NVIDIA CORPORATION.
# Copyright (c) 2022-2024, NVIDIA CORPORATION.
from __future__ import annotations

import ast
import functools
from typing import List, Tuple

from cudf._lib.expressions import (
ASTOperator,
Expand Down Expand Up @@ -98,9 +98,9 @@ class libcudfASTVisitor(ast.NodeVisitor):
The column names used to map the names in an expression.
"""

def __init__(self, col_names: Tuple[str]):
self.stack: List[Expression] = []
self.nodes: List[Expression] = []
def __init__(self, col_names: tuple[str]):
self.stack: list[Expression] = []
self.nodes: list[Expression] = []
self.col_names = col_names

@property
Expand Down Expand Up @@ -218,7 +218,7 @@ def visit_Call(self, node):


@functools.lru_cache(256)
def parse_expression(expr: str, col_names: Tuple[str]):
def parse_expression(expr: str, col_names: tuple[str]):
visitor = libcudfASTVisitor(col_names)
visitor.visit(ast.parse(expr))
return visitor
19 changes: 11 additions & 8 deletions python/cudf/cudf/core/_internals/timezones.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
from __future__ import annotations

import os
import zoneinfo
from functools import lru_cache
from typing import Literal, Tuple
from typing import TYPE_CHECKING, Literal

import numpy as np

from cudf._lib.timezone import make_timezone_transition_table
from cudf.core.column.column import as_column
from cudf.core.column.datetime import DatetimeColumn
from cudf.core.column.timedelta import TimeDeltaColumn

if TYPE_CHECKING:
from cudf.core.column.datetime import DatetimeColumn
from cudf.core.column.timedelta import TimeDeltaColumn


@lru_cache(maxsize=20)
def get_tz_data(zone_name: str) -> Tuple[DatetimeColumn, TimeDeltaColumn]:
def get_tz_data(zone_name: str) -> tuple[DatetimeColumn, TimeDeltaColumn]:
"""
Return timezone data (transition times and UTC offsets) for the
given IANA time zone.
Expand All @@ -40,7 +43,7 @@ def get_tz_data(zone_name: str) -> Tuple[DatetimeColumn, TimeDeltaColumn]:

def _find_and_read_tzfile_tzpath(
zone_name: str,
) -> Tuple[DatetimeColumn, TimeDeltaColumn]:
) -> tuple[DatetimeColumn, TimeDeltaColumn]:
for search_path in zoneinfo.TZPATH:
if os.path.isfile(os.path.join(search_path, zone_name)):
return _read_tzfile_as_columns(search_path, zone_name)
Expand All @@ -49,7 +52,7 @@ def _find_and_read_tzfile_tzpath(

def _find_and_read_tzfile_tzdata(
zone_name: str,
) -> Tuple[DatetimeColumn, TimeDeltaColumn]:
) -> tuple[DatetimeColumn, TimeDeltaColumn]:
import importlib.resources

package_base = "tzdata.zoneinfo"
Expand Down Expand Up @@ -78,7 +81,7 @@ def _find_and_read_tzfile_tzdata(

def _read_tzfile_as_columns(
tzdir, zone_name: str
) -> Tuple[DatetimeColumn, TimeDeltaColumn]:
) -> tuple[DatetimeColumn, TimeDeltaColumn]:
transition_times_and_offsets = make_timezone_transition_table(
tzdir, zone_name
)
Expand All @@ -92,7 +95,7 @@ def _read_tzfile_as_columns(

def check_ambiguous_and_nonexistent(
ambiguous: Literal["NaT"], nonexistent: Literal["NaT"]
) -> Tuple[Literal["NaT"], Literal["NaT"]]:
) -> tuple[Literal["NaT"], Literal["NaT"]]:
if ambiguous != "NaT":
raise NotImplementedError(
"Only ambiguous='NaT' is currently supported"
Expand Down
15 changes: 9 additions & 6 deletions python/cudf/cudf/core/_internals/where.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
from __future__ import annotations

import warnings
from typing import Tuple, Union
from typing import TYPE_CHECKING

import numpy as np

import cudf
from cudf._typing import ScalarLike
from cudf.api.types import (
_is_non_decimal_numeric_dtype,
is_bool_dtype,
is_scalar,
)
from cudf.core.column import ColumnBase
from cudf.core.dtypes import CategoricalDtype
from cudf.utils.dtypes import (
_can_cast,
Expand All @@ -21,6 +20,10 @@
is_mixed_with_object_dtype,
)

if TYPE_CHECKING:
from cudf._typing import ScalarLike
from cudf.core.column import ColumnBase


def _normalize_categorical(input_col, other):
if isinstance(input_col, cudf.core.column.CategoricalColumn):
Expand All @@ -41,9 +44,9 @@ def _normalize_categorical(input_col, other):

def _check_and_cast_columns_with_other(
source_col: ColumnBase,
other: Union[ScalarLike, ColumnBase],
other: ScalarLike | ColumnBase,
inplace: bool,
) -> Tuple[ColumnBase, Union[ScalarLike, ColumnBase]]:
) -> tuple[ColumnBase, ScalarLike | ColumnBase]:
# Returns type-casted `source_col` & `other` based on `inplace`.
source_dtype = source_col.dtype
if isinstance(source_dtype, CategoricalDtype):
Expand Down
14 changes: 7 additions & 7 deletions python/cudf/cudf/core/buffer/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pickle
import weakref
from types import SimpleNamespace
from typing import Any, Dict, Literal, Mapping, Optional, Tuple
from typing import Any, Literal, Mapping

import numpy
from typing_extensions import Self
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: Optional[object] = None,
owner: object | None = None,
readonly=False,
typestr="|u1",
version=0,
Expand Down Expand Up @@ -278,7 +278,7 @@ def get_ptr(self, *, mode: Literal["read", "write"]) -> int:
return self._ptr

def memoryview(
self, *, offset: int = 0, size: Optional[int] = None
self, *, offset: int = 0, size: int | None = None
) -> memoryview:
"""Read-only access to the buffer through host memory."""
size = self._size if size is None else size
Expand Down Expand Up @@ -319,7 +319,7 @@ def __init__(
*,
owner: BufferOwner,
offset: int = 0,
size: Optional[int] = None,
size: int | None = None,
) -> None:
size = owner.size if size is None else size
if size < 0:
Expand Down Expand Up @@ -414,7 +414,7 @@ def __cuda_array_interface__(self) -> Mapping:
"version": 0,
}

def serialize(self) -> Tuple[dict, list]:
def serialize(self) -> tuple[dict, list]:
"""Serialize the buffer into header and frames.
The frames can be a mixture of memoryview, Buffer, and BufferOwner
Expand All @@ -427,7 +427,7 @@ def serialize(self) -> Tuple[dict, list]:
serializable metadata required to reconstruct the object. The
second element is a list containing single frame.
"""
header: Dict[str, Any] = {}
header: dict[str, Any] = {}
header["type-serialized"] = pickle.dumps(type(self))
header["owner-type-serialized"] = pickle.dumps(type(self._owner))
header["frame_count"] = 1
Expand Down Expand Up @@ -480,7 +480,7 @@ def __str__(self) -> str:
)


def get_ptr_and_size(array_interface: Mapping) -> Tuple[int, int]:
def get_ptr_and_size(array_interface: Mapping) -> tuple[int, int]:
"""Retrieve the pointer and size from an array interface.
Raises ValueError if array isn't C-contiguous.
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/buffer/exposure_tracked_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import Literal, Mapping, Optional
from typing import Literal, Mapping

from typing_extensions import Self

Expand All @@ -27,7 +27,7 @@ def __init__(
self,
owner: BufferOwner,
offset: int = 0,
size: Optional[int] = None,
size: int | None = None,
) -> None:
super().__init__(owner=owner, offset=offset, size=size)
self.owner._slices.add(self)
Expand Down
Loading

0 comments on commit 87f6a7e

Please sign in to comment.