Skip to content

Commit

Permalink
Improve use of isinstance. (#10734)
Browse files Browse the repository at this point in the history
This PR combines a few `isinstance` checks to reduce the complexity of the logic.

Note: Some of these were identified by https://codereview.doctor/rapidsai/cudf. In some places, the bot correctly identified a problem but its suggestions were invalid or incomplete. I identified steps for improvement beyond what the bot suggested for most of these cases.

Authors:
  - Bradley Dice (https://github.com/bdice)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)
  - Ram (Ramakrishna Prabhu) (https://github.com/rgsl888prabhu)

URL: #10734
  • Loading branch information
bdice authored Apr 26, 2022
1 parent 1fdca07 commit 47740bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
30 changes: 14 additions & 16 deletions python/cudf/cudf/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,12 @@ def is_numeric_dtype(obj):
if issubclass(obj, _BaseDtype):
return False
else:
if isinstance(obj, cudf.Decimal128Dtype) or isinstance(
getattr(obj, "dtype", None), cudf.Decimal128Dtype
):
return True
if isinstance(obj, cudf.Decimal64Dtype) or isinstance(
getattr(obj, "dtype", None), cudf.Decimal64Dtype
):
return True
if isinstance(obj, cudf.Decimal32Dtype) or isinstance(
getattr(obj, "dtype", None), cudf.Decimal32Dtype
if isinstance(
obj,
(cudf.Decimal128Dtype, cudf.Decimal64Dtype, cudf.Decimal32Dtype),
) or isinstance(
getattr(obj, "dtype", None),
(cudf.Decimal128Dtype, cudf.Decimal64Dtype, cudf.Decimal32Dtype),
):
return True
if isinstance(obj, _BaseDtype) or isinstance(
Expand Down Expand Up @@ -129,12 +125,14 @@ def is_scalar(val):
bool
Return True if given object is scalar.
"""
return (
isinstance(val, cudf._lib.scalar.DeviceScalar)
or isinstance(val, cudf.Scalar)
or isinstance(val, cudf.core.tools.datetimes.DateOffset)
or pd_types.is_scalar(val)
)
return isinstance(
val,
(
cudf.Scalar,
cudf._lib.scalar.DeviceScalar,
cudf.core.tools.datetimes.DateOffset,
),
) or pd_types.is_scalar(val)


def _is_scalar_or_zero_d_array(val):
Expand Down
9 changes: 7 additions & 2 deletions python/cudf/cudf/core/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,13 @@ def is_interval_dtype(obj):
# TODO: Should there be any branch in this function that calls
# pd.api.types.is_interval_dtype?
return (
isinstance(obj, cudf.core.dtypes.IntervalDtype)
or isinstance(obj, pd.core.dtypes.dtypes.IntervalDtype)
isinstance(
obj,
(
cudf.core.dtypes.IntervalDtype,
pd.core.dtypes.dtypes.IntervalDtype,
),
)
or obj is cudf.core.dtypes.IntervalDtype
or (
isinstance(obj, str) and obj == cudf.core.dtypes.IntervalDtype.name
Expand Down

0 comments on commit 47740bc

Please sign in to comment.