10000 TYP: Avoid upcasting ``float64`` in the set-ops by charris · Pull Request #28243 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: Avoid upcasting float64 in the set-ops #28243

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 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
TYP: Avoid upcasting float64 in the set-ops
  • Loading branch information
jorenham authored and charris committed Jan 28, 2025
commit 3e5f2560d1581e87a0388eaee1244f04672324c1
96 changes: 26 additions & 70 deletions numpy/lib/_arraysetops_impl.pyi
10000
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,7 @@ from typing import (
from typing_extensions import deprecated

import numpy as np
from numpy import (
generic,
number,
ushort,
ubyte,
uintc,
uint,
ulonglong,
short,
int8,
byte,
intc,
int_,
intp,
longlong,
half,
single,
double,
longdouble,
csingle,
cdouble,
clongdouble,
timedelta64,
datetime64,
object_,
str_,
bytes_,
void,
)
from numpy import generic, number, int8, intp, timedelta64, object_

from numpy._typing import (
ArrayLike,
Expand Down Expand Up @@ -75,33 +47,17 @@ _NumberType = TypeVar("_NumberType", bound=number[Any])
# Only relevant if two or more arguments are parametrized, (e.g. `setdiff1d`)
# which could result in, for example, `int64` and `float64`producing a
# `number[_64Bit]` array
_SCTNoCast = TypeVar(
"_SCTNoCast",
_EitherSCT = TypeVar(
"_EitherSCT",
np.bool,
ushort,
ubyte,
uintc,
uint,
ulonglong,
short,
byte,
intc,
int_,
longlong,
half,
single,
double,
longdouble,
csingle,
cdouble,
clongdouble,
timedelta64,
datetime64,
object_,
str_,
bytes_,
void,
)
np.int8, np.int16, np.int32, np.int64, np.intp,
np.uint8, np.uint16, np.uint32, np.uint64, np.uintp,
np.float16, np.float32, np.float64, np.longdouble,
np.complex64, np.complex128, np.clongdouble,
np.timedelta64, np.datetime64,
np.bytes_, np.str_, np.void, np.object_,
np.integer, np.floating, np.complexfloating, np.character,
) # fmt: skip

class UniqueAllResult(NamedTuple, Generic[_SCT]):
values: NDArray[_SCT]
Expand Down Expand Up @@ -339,11 +295,11 @@ def unique_values(x: ArrayLike, /) -> NDArray[Any]: ...

@overload
def intersect1d(
ar1: _ArrayLike[_SCTNoCast],
ar2: _ArrayLike[_SCTNoCast],
ar1: _ArrayLike[_EitherSCT],
ar2: _ArrayLike[_EitherSCT],
assume_unique: bool = ...,
return_indices: L[False] = ...,
) -> NDArray[_SCTNoCast]: ...
) -> NDArray[_EitherSCT]: ...
@overload
def intersect1d(
ar1: ArrayLike,
Expand All @@ -353,11 +309,11 @@ def intersect1d(
) -> NDArray[Any]: ...
@overload
def intersect1d(
ar1: _ArrayLike[_SCTNoCast],
ar2: _ArrayLike[_SCTNoCast],
ar1: _ArrayLike[_EitherSCT],
ar2: _ArrayLike[_EitherSCT],
assume_unique: bool = ...,
return_indices: L[True] = ...,
) -> tuple[NDArray[_SCTNoCast], NDArray[intp], NDArray[intp]]: ...
) -> tuple[NDArray[_EitherSCT], NDArray[intp], NDArray[intp]]: ...
@overload
def intersect1d(
ar1: ArrayLike,
Expand All @@ -368,10 +324,10 @@ def intersect1d(

@overload
def setxor1d(
ar1: _ArrayLike[_SCTNoCast],
ar2: _ArrayLike[_SCTNoCast],
ar1: _ArrayLike[_EitherSCT],
ar2: _ArrayLike[_EitherSCT],
assume_unique: bool = ...,
) -> NDArray[_SCTNoCast]: ...
) -> NDArray[_EitherSCT]: ...
@overload
def setxor1d(
ar1: ArrayLike,
Expand Down Expand Up @@ -400,9 +356,9 @@ def in1d(

@overload
def union1d(
ar1: _ArrayLike[_SCTNoCast],
ar2: _ArrayLike[_SCTNoCast],
) -> NDArray[_SCTNoCast]: ...
ar1: _ArrayLike[_EitherSCT],
ar2: _ArrayLike[_EitherSCT],
) -> NDArray[_EitherSCT]: ...
@overload
def union1d(
ar1: ArrayLike,
Expand All @@ -411,10 +367,10 @@ def union1d(

@overload
def setdiff1d(
ar1: _ArrayLike[_SCTNoCast],
ar2: _ArrayLike[_SCTNoCast],
ar1: _ArrayLike[_EitherSCT],
ar2: _ArrayLike[_EitherSCT],
assume_unique: bool = ...,
) -> NDArray[_SCTNoCast]: ...
) -> NDArray[_EitherSCT]: ...
@overload
def setdiff1d(
ar1: ArrayLike,
Expand Down
7 changes: 2 additions & 5 deletions numpy/typing/tests/data/reveal/arraysetops.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ from typing import Any

import numpy as np
import numpy.typing as npt
from numpy.lib._arraysetops_impl import (
UniqueAllResult, UniqueCountsResult, UniqueInverseResult
)
from numpy._typing import _64Bit
from numpy.lib._arraysetops_impl import UniqueAllResult, UniqueCountsResult, UniqueInverseResult

from typing_extensions import assert_type

Expand All @@ -28,7 +25,7 @@ assert_type(np.intersect1d(AR_M, AR_M, assume_unique=True), npt.NDArray[np.datet
assert_type(np.intersect1d(AR_f8, AR_i8), npt.NDArray[Any])
assert_type(
np.intersect1d(AR_f8, AR_f8, return_indices=True),
tuple[npt.NDArray[np.floating[_64Bit]], npt.NDArray[np.intp], npt.NDArray[np.intp]],
tuple[npt.NDArray[np.float64], npt.NDArray[np.intp], npt.NDArray[np.intp]],
)

assert_type(np.setxor1d(AR_i8, AR_i8), npt.NDArray[np.int64])
Expand Down
Loading
0