Skip to content

Commit

Permalink
switch from typing.Callable to collections.abc.Callable (#16670)
Browse files Browse the repository at this point in the history
Follow-up to #16637.

Once this project's minimum support Python version was bumped up to Python 3.10, `ruff` started raising this error from `pyupgrade`:

```text
Import from `collections.abc` instead: `Callable`
```

* ruff docs: https://docs.astral.sh/ruff/rules/deprecated-import/
* `typing` docs saying that `typing.Callable` is deprecated starting in Python 3.9 https://docs.python.org/3/library/typing.html#typing.Callable
* context: #16637 (comment)

This proposes accepting that suggestion, so that `cudf` won't be broken whenever `Callable` is removed from the `typing` module.

Authors:
  - James Lamb (https://github.com/jameslamb)

Approvers:
  - Lawrence Mitchell (https://github.com/wence-)
  - Kyle Edwards (https://github.com/KyleFromNVIDIA)

URL: #16670
  • Loading branch information
jameslamb authored Aug 28, 2024
1 parent 569939f commit 5491b39
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 22 deletions.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ select = [
# non-pep585-annotation
"UP006",
# non-pep604-annotation
"UP007"
"UP007",
# Import from `collections.abc` instead: `Callable`
"UP035",
]
ignore = [
# whitespace before :
Expand Down
3 changes: 2 additions & 1 deletion python/cudf/cudf/_typing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright (c) 2021-2024, NVIDIA CORPORATION.

import sys
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, TypeVar, Union
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, Dict, Iterable, TypeVar, Union

import numpy as np
from pandas import Period, Timedelta, Timestamp
Expand Down
4 changes: 3 additions & 1 deletion python/cudf/cudf/core/column/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import functools
from typing import TYPE_CHECKING, Any, Callable, Sequence, cast
from typing import TYPE_CHECKING, Any, Sequence, cast

import numpy as np
import pandas as pd
Expand All @@ -28,6 +28,8 @@
from .numerical_base import NumericalBaseColumn

if TYPE_CHECKING:
from collections.abc import Callable

from cudf._typing import (
ColumnBinaryOperand,
ColumnLike,
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/column_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
from collections import abc
from functools import cached_property, reduce
from typing import TYPE_CHECKING, Any, Callable, Mapping, cast
from typing import TYPE_CHECKING, Any, Mapping, cast

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -639,7 +639,7 @@ def _pad_key(

def rename_levels(
self,
mapper: Mapping[abc.Hashable, abc.Hashable] | Callable,
mapper: Mapping[abc.Hashable, abc.Hashable] | abc.Callable,
level: int | None = None,
) -> Self:
"""
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import textwrap
import warnings
from collections import abc, defaultdict
from collections.abc import Iterator
from typing import TYPE_CHECKING, Any, Callable, Literal, MutableMapping, cast
from collections.abc import Callable, Iterator
from typing import TYPE_CHECKING, Any, Literal, MutableMapping, cast

import cupy
import numba
Expand Down
4 changes: 3 additions & 1 deletion python/cudf/cudf/core/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import textwrap
import warnings
from functools import cached_property
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any

import numpy as np
import pandas as pd
Expand All @@ -27,6 +27,8 @@
PANDAS_NUMPY_DTYPE = pd.core.dtypes.dtypes.PandasDtype

if TYPE_CHECKING:
from collections.abc import Callable

from cudf._typing import Dtype
from cudf.core.buffer import Buffer

Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pickle
import warnings
from collections import abc
from typing import TYPE_CHECKING, Any, Callable, Literal, MutableMapping
from typing import TYPE_CHECKING, Any, Literal, MutableMapping

# TODO: The `numpy` import is needed for typing purposes during doc builds
# only, need to figure out why the `np` alias is insufficient then remove.
Expand Down Expand Up @@ -403,7 +403,7 @@ def __arrow_array__(self, type=None):
@_performance_tracking
def _to_array(
self,
get_array: Callable,
get_array: abc.Callable,
module: ModuleType,
copy: bool,
dtype: Dtype | None = None,
Expand Down
5 changes: 4 additions & 1 deletion python/cudf/cudf/core/udf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import functools
import os
from typing import Any, Callable
from typing import TYPE_CHECKING, Any

import cachetools
import cupy as cp
Expand Down Expand Up @@ -41,6 +41,9 @@
from cudf.utils.performance_tracking import _performance_tracking
from cudf.utils.utils import initfunc

if TYPE_CHECKING:
from collections.abc import Callable

# Maximum size of a string column is 2 GiB
_STRINGS_UDF_DEFAULT_HEAP_SIZE = os.environ.get("STRINGS_UDF_HEAP_SIZE", 2**31)
_heap_size = 0
Expand Down
6 changes: 5 additions & 1 deletion python/cudf/cudf/io/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from collections import defaultdict
from contextlib import ExitStack
from functools import partial, reduce
from typing import Callable
from typing import TYPE_CHECKING
from uuid import uuid4

import numpy as np
Expand All @@ -24,6 +24,10 @@
from cudf.utils import ioutils
from cudf.utils.performance_tracking import _performance_tracking

if TYPE_CHECKING:
from collections.abc import Callable


BYTE_SIZES = {
"kb": 1000,
"mb": 1000000,
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import textwrap
from contextlib import ContextDecorator
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from collections.abc import Container
from collections.abc import Callable, Container


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/pandas/fast_slow_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import pickle
import types
import warnings
from collections.abc import Iterator
from collections.abc import Callable, Iterator
from enum import IntEnum
from typing import Any, Callable, Literal, Mapping
from typing import Any, Literal, Mapping

import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/utils/ioutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import os
import urllib
import warnings
from collections.abc import Callable
from io import BufferedWriter, BytesIO, IOBase, TextIOWrapper
from threading import Thread
from typing import Callable

import fsspec
import fsspec.implementations.local
Expand Down
4 changes: 2 additions & 2 deletions python/cudf_polars/cudf_polars/dsl/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import types
from functools import cache
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar

import pyarrow as pa
import pylibcudf as plc
Expand All @@ -31,7 +31,7 @@
from cudf_polars.utils import sorting

if TYPE_CHECKING:
from collections.abc import MutableMapping
from collections.abc import Callable, MutableMapping
from typing import Literal

from cudf_polars.typing import Schema
Expand Down
3 changes: 2 additions & 1 deletion python/cudf_polars/cudf_polars/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from polars.polars import _expr_nodes as pl_expr, _ir_nodes as pl_ir

if TYPE_CHECKING:
from typing import Callable, TypeAlias
from collections.abc import Callable
from typing import TypeAlias

import polars as pl

Expand Down
1 change: 0 additions & 1 deletion python/cudf_polars/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ ignore = [
# tryceratops
"TRY003", # Avoid specifying long messages outside the exception class
# pyupgrade
"UP035", # Import from `collections.abc` instead: `Callable`
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
# Lints below are turned off because of conflicts with the ruff
# formatter
Expand Down
2 changes: 1 addition & 1 deletion python/dask_cudf/dask_cudf/io/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def read_json(
If str, this value will be used as the ``engine`` argument
when :func:`cudf.read_json` is used to create each partition.
If a :obj:`~typing.Callable`, this value will be used as the
If a :obj:`~collections.abc.Callable`, this value will be used as the
underlying function used to create each partition from JSON
data. The default value is "auto", so that
``engine=partial(cudf.read_json, engine="auto")`` will be
Expand Down

0 comments on commit 5491b39

Please sign in to comment.