Skip to content

CLN: annotations in core.dtypes #29503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def __str__(self) -> str:
return self.name

def __eq__(self, other):
"""Check whether 'other' is equal to self.
"""
Check whether 'other' is equal to self.

By default, 'other' is considered equal if either

Expand Down Expand Up @@ -115,7 +116,7 @@ def __eq__(self, other):
)
return False

def __hash__(self):
def __hash__(self) -> int:
return hash(tuple(getattr(self, attr) for attr in self._metadata))

def __ne__(self, other):
Expand Down Expand Up @@ -171,7 +172,8 @@ def name(self) -> str:

@property
def names(self) -> Optional[List[str]]:
"""Ordered list of field names, or None if there are no fields.
"""
Ordered list of field names, or None if there are no fields.

This is for compatibility with NumPy arrays, and may be removed in the
future.
Expand Down Expand Up @@ -233,16 +235,19 @@ def construct_from_string(cls, string: str):
... "'{}'".format(cls.__name__, string))
"""
if not isinstance(string, str):
raise TypeError("Expects a string, got {}".format(type(string)))
raise TypeError("Expects a string, got {typ}".format(typ=type(string)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if changing .format could now use f-strings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh golly, im still tracking down %-based usage

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had a go at upgrading some of our % usage - hopefully there isn't too much more!

https://github.com/pandas-dev/pandas/pull/29518/files

if string != cls.name:
raise TypeError(
"Cannot construct a '{}' from '{}'".format(cls.__name__, string)
"Cannot construct a '{cls}' from '{string}'".format(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

cls=cls.__name__, string=string
)
)
return cls()

@classmethod
def is_dtype(cls, dtype) -> bool:
"""Check if we match 'dtype'.
"""
Check if we match 'dtype'.

Parameters
----------
Expand Down
34 changes: 18 additions & 16 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def maybe_convert_platform(values):
return values


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

def infer_dtype_from(val, pandas_dtype: bool = False):
"""
interpret the dtype from a scalar or array. This is a convenience
routines to infer dtype from a scalar or an array
Interpret the dtype from a scalar or array.

Parameters
----------
val : object
pandas_dtype : bool, default False
whether to infer dtype including pandas extension types.
If False, scalar/array belongs to pandas extension types is inferred as
Expand All @@ -517,7 +517,7 @@ def infer_dtype_from(val, pandas_dtype: bool = False):

def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
"""
interpret the dtype from a scalar
Interpret the dtype from a scalar.

Parameters
----------
Expand Down Expand Up @@ -592,7 +592,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):

def infer_dtype_from_array(arr, pandas_dtype: bool = False):
"""
infer the dtype from a scalar or array
Infer the dtype from a scalar or array.

Parameters
----------
Expand Down Expand Up @@ -647,7 +647,8 @@ def infer_dtype_from_array(arr, pandas_dtype: bool = False):


def maybe_infer_dtype_type(element):
"""Try to infer an object's dtype, for use in arithmetic ops
"""
Try to infer an object's dtype, for use in arithmetic ops.

Uses `element.dtype` if that's available.
Objects implementing the iterator protocol are cast to a NumPy array,
Expand Down Expand Up @@ -679,8 +680,9 @@ def maybe_infer_dtype_type(element):
return tipo


def maybe_upcast(values, fill_value=np.nan, dtype=None, copy=False):
""" provide explicit type promotion and coercion
def maybe_upcast(values, fill_value=np.nan, dtype=None, copy: bool = False):
"""
Provide explicit type promotion and coercion.

Parameters
----------
Expand Down Expand Up @@ -759,7 +761,7 @@ def conv(r, dtype):
return [conv(r, dtype) for r, dtype in zip(result, dtypes)]


def astype_nansafe(arr, dtype, copy=True, skipna=False):
def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
"""
Cast the elements of an array to a given dtype a nan-safe manner.

Expand Down Expand Up @@ -982,7 +984,7 @@ def soft_convert_objects(
return values


def maybe_castable(arr):
def maybe_castable(arr) -> bool:
# return False to force a non-fastpath

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


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


def maybe_cast_to_datetime(value, dtype, errors="raise"):
def maybe_cast_to_datetime(value, dtype, errors: str = "raise"):
""" try to cast the array/value to a datetimelike dtype, converting float
nan to iNaT
"""
Expand Down Expand Up @@ -1292,7 +1294,7 @@ def find_common_type(types):

def cast_scalar_to_array(shape, value, dtype=None):
"""
create np.ndarray of specified shape and dtype, filled with values
Create np.ndarray of specified shape and dtype, filled with values.

Parameters
----------
Expand All @@ -1318,7 +1320,7 @@ def cast_scalar_to_array(shape, value, dtype=None):
return values


def construct_1d_arraylike_from_scalar(value, length, dtype):
def construct_1d_arraylike_from_scalar(value, length: int, dtype):
"""
create a np.ndarray / pandas type of specified shape and dtype
filled with values
Expand Down Expand Up @@ -1383,7 +1385,7 @@ def construct_1d_object_array_from_listlike(values):
return result


def construct_1d_ndarray_preserving_na(values, dtype=None, copy=False):
def construct_1d_ndarray_preserving_na(values, dtype=None, copy: bool = False):
"""
Construct a new ndarray, coercing `values` to `dtype`, preserving NA.

Expand Down Expand Up @@ -1424,7 +1426,7 @@ def construct_1d_ndarray_preserving_na(values, dtype=None, copy=False):
return subarr


def maybe_cast_to_integer_array(arr, dtype, copy=False):
def maybe_cast_to_integer_array(arr, dtype, copy: bool = False):
"""
Takes any dtype and returns the casted version, raising for when data is
incompatible with integer/unsigned integer dtypes.
Expand Down
Loading