Skip to content

CLN: add typing for dtype arg in core/arrays (GH38808) #38886

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 6 commits into from
Jan 5, 2021

Conversation

avinashpancham
Copy link
Contributor

Follow on PR for #38808

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

@jreback jreback added Dtype Conversions Unexpected or buggy dtype conversions Typing type annotations, mypy/pyright type checking labels Jan 1, 2021
Comment on lines +232 to +237
if is_interval_dtype(dtype):
dtype = cast(IntervalDtype, dtype)
if dtype.subtype is not None:
left = left.astype(dtype.subtype)
right = right.astype(dtype.subtype)
else:
Copy link
Member

Choose a reason for hiding this comment

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

why change the logic here? If the rest of the PR just adds type hints, maybe it would be safer to keep refactorings to a separate PR?

Copy link
Contributor Author

@avinashpancham avinashpancham Jan 3, 2021

Choose a reason for hiding this comment

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

I changed the code because otherwise the Mypy checks will not pass. But do note the logic itself did not change, it is just the order of operations

Copy link
Contributor

Choose a reason for hiding this comment

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

this is fine (you just moved and changed the logical test), but puzzled why we don't need the clause on L229

Copy link
Contributor

Choose a reason for hiding this comment

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

cc @jbrockmendel if any idea here

Comment on lines +232 to +237
if is_interval_dtype(dtype):
dtype = cast(IntervalDtype, dtype)
if dtype.subtype is not None:
left = left.astype(dtype.subtype)
right = right.astype(dtype.subtype)
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

this is fine (you just moved and changed the logical test), but puzzled why we don't need the clause on L229

Comment on lines +232 to +237
if is_interval_dtype(dtype):
dtype = cast(IntervalDtype, dtype)
if dtype.subtype is not None:
left = left.astype(dtype.subtype)
right = right.astype(dtype.subtype)
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

cc @jbrockmendel if any idea here

right,
closed=None,
copy=False,
dtype: Optional[Dtype] = None,
Copy link
Member

Choose a reason for hiding this comment

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

by the time we get here we should have a DtypeObj shouldnt we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The _simple_new method is a.o. used in the __new__ method on L169 of this file. The __new__ method accepts the dtype values of type Dtype and passes it without modification to the dtype arg of _simple_new. Therefore I also gave it type Dtype here.

@@ -223,12 +229,14 @@ def _simple_new(
if dtype is not None:
# GH 19262: dtype must be an IntervalDtype to override inferred
dtype = pandas_dtype(dtype)
if not is_interval_dtype(dtype):
if is_interval_dtype(dtype):
Copy link
Member

Choose a reason for hiding this comment

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

could just do isinstance(dtype, IntervalDtype) and avoid cast

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In #38680, we discussed the usage of isinstance vs is_* methods in combination with cast and the latter was preferred. Therefore I continued with it here.

@@ -279,7 +287,7 @@ def _simple_new(
return result

@classmethod
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
def _from_sequence(cls, scalars, *, dtype: Optional[Dtype] = None, copy=False):
Copy link
Member

Choose a reason for hiding this comment

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

copy: bool

@@ -338,7 +346,9 @@ def _from_factorized(cls, values, original):
),
}
)
def from_breaks(cls, breaks, closed="right", copy=False, dtype=None):
def from_breaks(
Copy link
Member

Choose a reason for hiding this comment

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

copy: bool

@jreback jreback added this to the 1.3 milestone Jan 5, 2021
@jreback jreback merged commit 27ae7d1 into pandas-dev:master Jan 5, 2021
@jreback
Copy link
Contributor

jreback commented Jan 5, 2021

thanks @avinashpancham

luckyvs1 pushed a commit to luckyvs1/pandas that referenced this pull request Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants