Skip to content

CLN: groupby.ops follow-up cleanup #41204

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
Apr 30, 2021

Conversation

jbrockmendel
Copy link
Member

get the namespaces/privateness "right"
avoid runtime imports
fix "type: ignore"s

...

@overload
def _get_result_dtype(self, dtype: ExtensionDtype) -> ExtensionDtype:
Copy link
Member

Choose a reason for hiding this comment

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

Would a TypeVar work here? Might end up being useful elsewhere too

Copy link
Member Author

Choose a reason for hiding this comment

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

i dont think that would be accurate here

Copy link
Member

Choose a reason for hiding this comment

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

Might be misunderstanding overloads, but is this expressing ExtensionDtype -> ExtensionDtype and np.dtype -> np.dtype?

So was thinking a TypeVar like TypeVar('DtypeObjT', np.dtype, ExtensionDtype)

Copy link
Member Author

Choose a reason for hiding this comment

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

do TypeVars not preserve subclasses?

Copy link
Member

Choose a reason for hiding this comment

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

Running mypy on something like

from typing import TypeVar

AnyStr = TypeVar('AnyStr', str, bytes)


class Upper(str):
    def __new__(cls, val):
        return str.__new__(cls, val.upper())


class Lower(str):
    def __new__(cls, val):
        return str.__new__(cls, val.lower())


def swap_lower_upper(s: AnyStr) -> AnyStr:
    if isinstance(s, bytes):
        return s
    elif isinstance(s, Lower):
        return Upper(s)
    else:
        return Lower(s)

swap_lower_upper(Lower("hello"))
swap_lower_upper(Upper("hello"))

gives no error, so I don't think subclasses are preserved.

Copy link
Member Author

Choose a reason for hiding this comment

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

cool. will make a separate PR to implement DtypeObjT

Copy link
Member Author

Choose a reason for hiding this comment

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

implenting this with DtypeObjT and getting rid of the overloads im seeing

error: Incompatible return value type (got "dtype[signedinteger[_64Bit]]", expected "ExtensionDtype")  [return-value]

Copy link
Member

Choose a reason for hiding this comment

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

Will have time to look in more detail later, initial guess would be that this return is the problem

if how in ["add", "cumsum", "sum", "prod"]:
if dtype == np.dtype(bool):
return np.dtype(np.int64)

IIRC mypy can't narrow based on type equality, so it's thinking that an ExtensionDtype can hit that return. But then not sure why it wouldn't complain with the overload, but those are confusing :)

@jreback jreback added this to the 1.3 milestone Apr 29, 2021
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

looks fine

cc @simonjayhawkins if comments

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @jbrockmendel lgtm. TypeVars with a union of types do have different semantics than the bound= variant that we normally use, so can address @mzeitlin11 comments here or as follow-up.

@jreback jreback merged commit 95ec3f6 into pandas-dev:master Apr 30, 2021
@jbrockmendel jbrockmendel deleted the ref-gbops-followups branch April 30, 2021 15:22
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request May 6, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants