Skip to content

Commit 271938b

Browse files
jbrockmendelproost
authored andcommitted
CLN: annotations in core.dtypes (pandas-dev#29503)
1 parent 66c57d5 commit 271938b

File tree

8 files changed

+97
-84
lines changed

8 files changed

+97
-84
lines changed

pandas/core/dtypes/base.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def __str__(self) -> str:
8787
return self.name
8888

8989
def __eq__(self, other):
90-
"""Check whether 'other' is equal to self.
90+
"""
91+
Check whether 'other' is equal to self.
9192
9293
By default, 'other' is considered equal if either
9394
@@ -115,7 +116,7 @@ def __eq__(self, other):
115116
)
116117
return False
117118

118-
def __hash__(self):
119+
def __hash__(self) -> int:
119120
return hash(tuple(getattr(self, attr) for attr in self._metadata))
120121

121122
def __ne__(self, other):
@@ -171,7 +172,8 @@ def name(self) -> str:
171172

172173
@property
173174
def names(self) -> Optional[List[str]]:
174-
"""Ordered list of field names, or None if there are no fields.
175+
"""
176+
Ordered list of field names, or None if there are no fields.
175177
176178
This is for compatibility with NumPy arrays, and may be removed in the
177179
future.
@@ -233,16 +235,19 @@ def construct_from_string(cls, string: str):
233235
... "'{}'".format(cls.__name__, string))
234236
"""
235237
if not isinstance(string, str):
236-
raise TypeError("Expects a string, got {}".format(type(string)))
238+
raise TypeError("Expects a string, got {typ}".format(typ=type(string)))
237239
if string != cls.name:
238240
raise TypeError(
239-
"Cannot construct a '{}' from '{}'".format(cls.__name__, string)
241+
"Cannot construct a '{cls}' from '{string}'".format(
242+
cls=cls.__name__, string=string
243+
)
240244
)
241245
return cls()
242246

243247
@classmethod
244248
def is_dtype(cls, dtype) -> bool:
245-
"""Check if we match 'dtype'.
249+
"""
250+
Check if we match 'dtype'.
246251
247252
Parameters
248253
----------

pandas/core/dtypes/cast.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def maybe_convert_platform(values):
7272
return values
7373

7474

75-
def is_nested_object(obj):
75+
def is_nested_object(obj) -> bool:
7676
"""
7777
return a boolean if we have a nested object, e.g. a Series with 1 or
7878
more Series elements
@@ -500,11 +500,11 @@ def _ensure_dtype_type(value, dtype):
500500

501501
def infer_dtype_from(val, pandas_dtype: bool = False):
502502
"""
503-
interpret the dtype from a scalar or array. This is a convenience
504-
routines to infer dtype from a scalar or an array
503+
Interpret the dtype from a scalar or array.
505504
506505
Parameters
507506
----------
507+
val : object
508508
pandas_dtype : bool, default False
509509
whether to infer dtype including pandas extension types.
510510
If False, scalar/array belongs to pandas extension types is inferred as
@@ -517,7 +517,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False):
517517

518518
def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
519519
"""
520-
interpret the dtype from a scalar
520+
Interpret the dtype from a scalar.
521521
522522
Parameters
523523
----------
@@ -592,7 +592,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
592592

593593
def infer_dtype_from_array(arr, pandas_dtype: bool = False):
594594
"""
595-
infer the dtype from a scalar or array
595+
Infer the dtype from a scalar or array.
596596
597597
Parameters
598598
----------
@@ -647,7 +647,8 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False):
647647

648648

649649
def maybe_infer_dtype_type(element):
650-
"""Try to infer an object's dtype, for use in arithmetic ops
650+
"""
651+
Try to infer an object's dtype, for use in arithmetic ops.
651652
652653
Uses `element.dtype` if that's available.
653654
Objects implementing the iterator protocol are cast to a NumPy array,
@@ -679,8 +680,9 @@ def maybe_infer_dtype_type(element):
679680
return tipo
680681

681682

682-
def maybe_upcast(values, fill_value=np.nan, dtype=None, copy=False):
683-
""" provide explicit type promotion and coercion
683+
def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
684+
"""
685+
Provide explicit type promotion and coercion.
684686
685687
Parameters
686688
----------
@@ -759,7 +761,7 @@ def conv(r, dtype):
759761
return [conv(r, dtype) for r, dtype in zip(result, dtypes)]
760762

761763

762-
def astype_nansafe(arr, dtype, copy=True, skipna=False):
764+
def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
763765
"""
764766
Cast the elements of an array to a given dtype a nan-safe manner.
765767
@@ -982,7 +984,7 @@ def soft_convert_objects(
982984
return values
983985

984986

985-
def maybe_castable(arr):
987+
def maybe_castable(arr) -> bool:
986988
# return False to force a non-fastpath
987989

988990
# check datetime64[ns]/timedelta64[ns] are valid
@@ -996,7 +998,7 @@ def maybe_castable(arr):
996998
return arr.dtype.name not in _POSSIBLY_CAST_DTYPES
997999

9981000

999-
def maybe_infer_to_datetimelike(value, convert_dates=False):
1001+
def maybe_infer_to_datetimelike(value, convert_dates: bool = False):
10001002
"""
10011003
we might have a array (or single object) that is datetime like,
10021004
and no dtype is passed don't change the value unless we find a
@@ -1103,7 +1105,7 @@ def try_timedelta(v):
11031105
return value
11041106

11051107

1106-
def maybe_cast_to_datetime(value, dtype, errors="raise"):
1108+
def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):
11071109
""" try to cast the array/value to a datetimelike dtype, converting float
11081110
nan to iNaT
11091111
"""
@@ -1292,7 +1294,7 @@ def find_common_type(types):
12921294

12931295
def cast_scalar_to_array(shape, value, dtype=None):
12941296
"""
1295-
create np.ndarray of specified shape and dtype, filled with values
1297+
Create np.ndarray of specified shape and dtype, filled with values.
12961298
12971299
Parameters
12981300
----------
@@ -1318,7 +1320,7 @@ def cast_scalar_to_array(shape, value, dtype=None):
13181320
return values
13191321

13201322

1321-
def construct_1d_arraylike_from_scalar(value, length, dtype):
1323+
def construct_1d_arraylike_from_scalar(value, length: int, dtype):
13221324
"""
13231325
create a np.ndarray / pandas type of specified shape and dtype
13241326
filled with values
@@ -1383,7 +1385,7 @@ def construct_1d_object_array_from_listlike(values):
13831385
return result
13841386

13851387

1386-
def construct_1d_ndarray_preserving_na(values, dtype=None, copy=False):
1388+
def construct_1d_ndarray_preserving_na(values, dtype=None, copy: bool = False):
13871389
"""
13881390
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.
13891391
@@ -1424,7 +1426,7 @@ def construct_1d_ndarray_preserving_na(values, dtype=None, copy=False):
14241426
return subarr
14251427

14261428

1427-
def maybe_cast_to_integer_array(arr, dtype, copy=False):
1429+
def maybe_cast_to_integer_array(arr, dtype, copy: bool = False):
14281430
"""
14291431
Takes any dtype and returns the casted version, raising for when data is
14301432
incompatible with integer/unsigned integer dtypes.

0 commit comments

Comments
 (0)