Skip to content

TYP: stubs for reshape, ops, ops_dispatch, hashing #40455

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
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions pandas/_libs/hashing.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import numpy as np

def hash_object_array(
arr: np.ndarray, # np.ndarray[object]
key: str,
encoding: str = "utf8",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
encoding: str = "utf8",
encoding: str = ...,

) -> np.ndarray: ... # np.ndarray[np.uint64]
43 changes: 43 additions & 0 deletions pandas/_libs/ops.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import (
Any,
Callable,
)

import numpy as np

binop = Callable[[Any, Any], Any]
bool_op = Callable[[Any, Any], bool]
Copy link
Member

Choose a reason for hiding this comment

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

from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions

Type variables and aliases you introduce purely for legibility reasons should be prefixed with an underscore to make it obvious to the reader they are not part of the stubbed API.

we normally capitalize as well



def scalar_compare(
values: np.ndarray, # object[:]
val: object,
op: bool_op, # {operator.eq, operator.ne, ...}
) -> np.ndarray: ... # np.ndarray[bool]

def vec_compare(
left: np.ndarray, # np.ndarray[object]
right: np.ndarray, # np.ndarray[object]
op: bool_op, # {operator.eq, operator.ne, ...}
) -> np.ndarray: ... # np.ndarray[bool]


def scalar_binop(
values: np.ndarray, # object[:]
val: object,
op: binop, # binary operator
) -> np.ndarray: ...


def vec_binop(
left: np.ndarray, # object[:]
right: np.ndarray, # object[:]
op: binop, # binary operator
) -> np.ndarray: ...


def maybe_convert_bool(
arr: np.ndarray, # np.ndarray[object]
true_values=None,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
true_values=None,
true_values=...,

false_values=None
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
false_values=None
false_values=...

) -> np.ndarray: ...
9 changes: 9 additions & 0 deletions pandas/_libs/ops_dispatch.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import numpy as np

REVERSED_NAMES: dict[str, str]
UFUNC_ALIASES: dict[str, str]
DISPATCHED_UFUNCS: set[str]
Copy link
Member

Choose a reason for hiding this comment

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

These are not accessed from python code.

so I don't think is covered by https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#what-to-include

and from https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions

The general rule is that they should be as concise as possible.

so I think we should perhaps not include these.

once we activate ignore_missing_imports = False, we'll know if we missed anything.


side note: for the stub files we could consider activating more of the strictness flags for incomplete function annotations and dynamic typing etc.


def maybe_dispatch_ufunc_to_dunder_op(
self, ufunc: np.ufunc, method: str, *inputs, **kwargs
Copy link
Member

Choose a reason for hiding this comment

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

maybe type self, since function is not a method

Copy link
Member Author

Choose a reason for hiding this comment

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

will take some tracking down, since it is called via arraylike.array_ufunc, which doesnt type self

): ...
19 changes: 19 additions & 0 deletions pandas/_libs/reshape.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import numpy as np

def unstack(
values: np.ndarray, # reshape_t[:, :]
mask: np.ndarray, # const uint8_t[:]
stride: int,
length: int,
width: int,
new_values: np.ndarray, # reshape_t[:, :]
new_mask: np.ndarray, # uint8_t[:, :]
) -> None: ...


def explode(
values: np.ndarray, # np.ndarray[object]
) -> tuple[
np.ndarray, # np.ndarray[object]
np.ndarray, # np.ndarray[np.int64]
]: ...