Skip to content
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
108 changes: 75 additions & 33 deletions src/numpy-stubs/@test/generated/scalar_ops_comparison.pyi

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/numpy-stubs/@test/generated/scalar_ops_modular.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions src/numpy-stubs/@test/runtime/legacy/arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Self
from typing import Any, Self

import pytest

Expand Down Expand Up @@ -34,28 +34,28 @@ def __array__(
ret[()] = self
return ret

def __sub__(self, value: object) -> Self:
def __sub__(self, value: Any) -> Self:
return self

def __rsub__(self, value: object) -> Self:
def __rsub__(self, value: Any) -> Self:
return self

def __floordiv__(self, value: object) -> Self:
def __floordiv__(self, value: Any) -> Self:
return self

def __rfloordiv__(self, value: object) -> Self:
def __rfloordiv__(self, value: Any) -> Self:
return self

def __mul__(self, value: object) -> Self:
def __mul__(self, value: Any) -> Self:
return self

def __rmul__(self, value: object) -> Self:
def __rmul__(self, value: Any) -> Self:
return self

def __pow__(self, value: object) -> Self:
def __pow__(self, value: Any) -> Self:
return self

def __rpow__(self, value: object) -> Self:
def __rpow__(self, value: Any) -> Self:
return self


Expand Down Expand Up @@ -84,15 +84,13 @@ def __rpow__(self, value: object) -> Self:
AR_b - AR_LIKE_f
AR_b - AR_LIKE_c
AR_b - AR_LIKE_m
AR_b - AR_LIKE_O

AR_LIKE_u - AR_b
AR_LIKE_i - AR_b
AR_LIKE_f - AR_b
AR_LIKE_c - AR_b
AR_LIKE_m - AR_b
AR_LIKE_M - AR_b
AR_LIKE_O - AR_b

AR_u - AR_LIKE_b
AR_u - AR_LIKE_u
Expand Down
23 changes: 14 additions & 9 deletions src/numpy-stubs/@test/static/accept/arithmetic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,33 @@ AR_Any: _nt.Array

assert_type(m8 // m8, np.int64)
assert_type(m8 % m8, np.timedelta64)
assert_type(divmod(m8, m8), tuple[np.int64, np.timedelta64]) # pyright: ignore[reportArgumentType, reportAssertTypeFailure, reportCallIssue]
# mypy incorrectly infers this as "tuple[Any, ...]" but pyright behaves correctly
assert_type(divmod(m8, m8), tuple[np.int64, np.timedelta64]) # type: ignore[assert-type]

assert_type(M8_none + m8, np.datetime64[None])
# mypy incorrectly infers this as "timedelta64[Any]" but pyright behaves correctly
assert_type(M8_none - M8, np.timedelta64[None]) # type: ignore[assert-type]
# mypy incorrectly infers this as "datetime64[Any]" but pyright behaves correctly
assert_type(M8_none + m8, np.datetime64[None]) # type: ignore[assert-type]
assert_type(M8_none - m8, np.datetime64[None]) # type: ignore[assert-type]
assert_type(M8_none + i, np.datetime64[None])
assert_type(M8_none + i8, np.datetime64[None])
assert_type(M8_none - M8, np.timedelta64[None])
assert_type(M8_none - m8, np.datetime64[None])
assert_type(M8_none - i, np.datetime64[None])
assert_type(M8_none + i8, np.datetime64[None])
assert_type(M8_none - i8, np.datetime64[None])

assert_type(m8_none + m8, np.timedelta64[None])
# mypy incorrectly infers this as "timedelta64[Any]" but pyright behaves correctly
assert_type(m8_none + m8, np.timedelta64[None]) # type: ignore[assert-type]
assert_type(m8_none + i, np.timedelta64[None])
assert_type(m8_none + i8, np.timedelta64[None])
assert_type(m8_none - i, np.timedelta64[None])
assert_type(m8_none - i8, np.timedelta64[None])

assert_type(m8_int + i, np.timedelta64[int])
assert_type(m8_int + m8_delta, np.timedelta64[int])
assert_type(m8_int + m8, np.timedelta64[int | None])
assert_type(m8_int - i, np.timedelta64[int])
assert_type(m8_int + m8_delta, np.timedelta64[int])
assert_type(m8_int - m8_delta, np.timedelta64[int])
assert_type(m8_int - m8, np.timedelta64[int | None])
# mypy incorrectly infers this as "timedelta64[Any]" but pyright behaves correctly
assert_type(m8_int + m8, np.timedelta64[int]) # type: ignore[assert-type]
assert_type(m8_int - m8, np.timedelta64[int]) # type: ignore[assert-type]

assert_type(m8_delta + date, dt.date)
assert_type(m8_delta + time, dt.datetime)
Expand Down
3 changes: 2 additions & 1 deletion src/numpy-stubs/@test/static/accept/array_constructors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ assert_type(np.arange(10.0), _nt.Array1D[np.float64 | Any])
assert_type(np.arange(0, stop=10.0), _nt.Array1D[np.float64 | Any])
assert_type(np.arange(np.timedelta64(0)), _nt.Array1D[np.timedelta64[Any]])
assert_type(np.arange(0, np.timedelta64(10)), _nt.Array1D[np.timedelta64[Any]])
assert_type(np.arange(np.datetime64("0"), np.datetime64("10")), _nt.Array1D[np.datetime64[Any]])
# mypy incorrectly infers this as "ndarray[Any, Any]" but pyright behaves correctly
assert_type(np.arange(np.datetime64("0"), np.datetime64("10")), _nt.Array1D[np.datetime64[Any]]) # type: ignore[assert-type]
assert_type(np.arange(10, dtype=np.float64), _nt.Array1D[np.float64 | Any])
assert_type(np.arange(0, 10, step=2, dtype=np.int16), _nt.Array1D[np.int16])
assert_type(np.arange(10, dtype=int), _nt.Array1D[np.int_])
Expand Down
12 changes: 8 additions & 4 deletions src/numpy-stubs/@test/static/accept/arraysetops.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ AR_LIKE_i_: list[int]

assert_type(np.intersect1d(AR_i_, AR_i_), _nt.Array1D[np.intp])
assert_type(np.intersect1d(AR_f8, AR_i_), _nt.Array1D[np.float64])
assert_type(np.intersect1d(AR_M, AR_M, assume_unique=True), _nt.Array1D[np.datetime64])
# mypy incorrectly infers this as "ndarray[Any, Any]" but pyright behaves correctly
assert_type(np.intersect1d(AR_M, AR_M, assume_unique=True), _nt.Array1D[np.datetime64]) # type: ignore[assert-type]
assert_type(
np.intersect1d(AR_f8, AR_f8, return_indices=True),
tuple[_nt.Array1D[np.float64], _nt.Array1D[np.intp], _nt.Array1D[np.intp]],
)

assert_type(np.union1d(AR_i_, AR_i_), _nt.Array1D[np.intp])
assert_type(np.union1d(AR_f8, AR_i_), _nt.Array1D[np.float64])
assert_type(np.union1d(AR_M, AR_M), _nt.Array1D[np.datetime64])
# mypy incorrectly infers this as "ndarray[Any, Any]" but pyright behaves correctly
assert_type(np.union1d(AR_M, AR_M), _nt.Array1D[np.datetime64]) # type: ignore[assert-type]

assert_type(np.ediff1d(AR_b), _nt.Array1D[np.int8])
assert_type(np.ediff1d(AR_M), _nt.Array1D[np.timedelta64])
Expand All @@ -33,11 +35,13 @@ assert_type(np.ediff1d(AR_LIKE_i_, to_begin=[0, 1]), _nt.Array1D[np.intp])

assert_type(np.setxor1d(AR_i_, AR_i_), _nt.Array1D[np.intp])
assert_type(np.setxor1d(AR_f8, AR_i_), _nt.Array1D[np.float64])
assert_type(np.setxor1d(AR_M, AR_M, assume_unique=True), _nt.Array1D[np.datetime64])
# mypy incorrectly infers this as "ndarray[Any, Any]" but pyright behaves correctly
assert_type(np.setxor1d(AR_M, AR_M, assume_unique=True), _nt.Array1D[np.datetime64]) # type: ignore[assert-type]

assert_type(np.setdiff1d(AR_i_, AR_i_), _nt.Array1D[np.intp])
assert_type(np.setdiff1d(AR_f8, AR_i_), _nt.Array1D[np.float64])
assert_type(np.setdiff1d(AR_M, AR_M, assume_unique=True), _nt.Array1D[np.datetime64])
# mypy incorrectly infers this as "ndarray[Any, Any]" but pyright behaves correctly
assert_type(np.setdiff1d(AR_M, AR_M, assume_unique=True), _nt.Array1D[np.datetime64]) # type: ignore[assert-type]

assert_type(np.isin(AR_i_, AR_i_), _nt.Array[np.bool])
assert_type(np.isin(AR_f8, AR_i_), _nt.Array[np.bool])
Expand Down
3 changes: 2 additions & 1 deletion src/numpy-stubs/@test/static/accept/comparisons.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ assert_type(i8 > [decimal.Decimal("1.5")], BoolND)
###
# Time structures

assert_type(dt > dt, np.bool)
# mypy incorrectly infers this as "Any" but pyright behaves correctly
assert_type(dt > dt, np.bool) # type: ignore[assert-type]

assert_type(td > td, np.bool)
assert_type(td > i, np.bool)
Expand Down
3 changes: 2 additions & 1 deletion src/numpy-stubs/@test/static/accept/fromnumeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ assert_type(np.around(AR_f4, out=AR_subclass), NDArraySubclass)
assert_type(np.mean(AR_b1), np.floating)
assert_type(np.mean(AR_i8), np.floating)
assert_type(np.mean(AR_f4), np.floating)
assert_type(np.mean(AR_m), np.timedelta64)
# mypy incorrectly infers this as "Any" but pyright behaves correctly
assert_type(np.mean(AR_m), np.timedelta64) # type: ignore[assert-type]
assert_type(np.mean(AR_c16), np.complexfloating)
assert_type(np.mean(AR_O), Any)
assert_type(np.mean(AR_f4, axis=0), Any)
Expand Down
7 changes: 5 additions & 2 deletions src/numpy-stubs/@test/static/accept/lib_function_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ assert_type(np.sinc(AR_c16), _nt.Array[np.complexfloating])

assert_type(np.median(AR_f8, keepdims=False), np.floating)
assert_type(np.median(AR_c16, overwrite_input=True), np.complexfloating)
assert_type(np.median(AR_m), np.timedelta64)

# mypy incorrectly infers this as "Any" but pyright behaves correctly
assert_type(np.median(AR_m), np.timedelta64) # type: ignore[assert-type]
assert_type(np.median(AR_O), Any)
assert_type(np.median(AR_f8, keepdims=True), Any)
assert_type(np.median(AR_c16, axis=0), Any)
Expand Down Expand Up @@ -204,7 +206,8 @@ assert_type(np.trapezoid(AR_i8, AR_f8), np.float64 | _nt.Array[np.float64])
assert_type(np.trapezoid(AR_f8, AR_i8), np.float64 | _nt.Array[np.float64])
assert_type(np.trapezoid(AR_c16), np.complex128 | _nt.Array[np.complex128])
assert_type(np.trapezoid(AR_c16, AR_c16), np.complex128 | _nt.Array[np.complex128])
assert_type(np.trapezoid(AR_m), np.timedelta64 | _nt.Array[np.timedelta64])
# mypy incorrectly infers this as "Any" but pyright behaves correctly
assert_type(np.trapezoid(AR_m), np.timedelta64 | _nt.Array[np.timedelta64]) # type: ignore[assert-type]
assert_type(np.trapezoid(AR_O), Any)
assert_type(np.trapezoid(AR_O, AR_LIKE_f), Any)

Expand Down
22 changes: 13 additions & 9 deletions src/numpy-stubs/@test/static/accept/mod.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ AR_m: _nt.Array[np.timedelta64]
assert_type(m % m, np.timedelta64)
assert_type(m % m_nat, np.timedelta64[None])
assert_type(m % m_int0, np.timedelta64[None])
assert_type(m % m_int, np.timedelta64[int | None])
assert_type(m_nat % m, np.timedelta64[None])
assert_type(m % m_int, np.timedelta64)
# mypy incorrectly infers this as "timedelta64[Any]", but pyright behaves correctly
assert_type(m_nat % m, np.timedelta64[None]) # type: ignore[assert-type]
assert_type(m_int % m_nat, np.timedelta64[None])
assert_type(m_int % m_int0, np.timedelta64[None])
assert_type(m_int % m_int, np.timedelta64[int | None])
Expand All @@ -46,19 +47,22 @@ assert_type(m_td % m_td, np.timedelta64[dt.timedelta | None])
assert_type(AR_m % m, _nt.Array[np.timedelta64])
assert_type(m % AR_m, _nt.Array[np.timedelta64])

# mypy incorrectly infers this as "tuple[Any, ...]", but pyright behaves correctly (surprisingly)
assert_type(divmod(m, m), tuple[np.int64, np.timedelta64]) # type: ignore[assert-type]
assert_type(divmod(m, m_nat), tuple[np.int64, np.timedelta64]) # type: ignore[assert-type]
assert_type(divmod(m, m_int0), tuple[np.int64, np.timedelta64]) # type: ignore[assert-type]
assert_type(divmod(m, m_int), tuple[np.int64, np.timedelta64])
# mypy incorrectly infers this as "tuple[Any, ...]" but pyright behaves correctly
assert_type(divmod(m_nat, m), tuple[np.int64, np.timedelta64[None]]) # type: ignore[assert-type]
assert_type(divmod(m_int, m_nat), tuple[np.int64, np.timedelta64[None]])
assert_type(divmod(m_int, m_int0), tuple[np.int64, np.timedelta64[None]])

# NOTE: The pyright ignores are the consequence of a pernicious bug in pyright
# (microsoft/pyright#9896, microsoft/pyright#10849, microsoft/pyright#10899) that
# causes incorrect behavior in certain functions that accept generic protocols with
# overloaded methods. Even though mypy also isn't fully correct here, it will at least
# not falsely reject valid calls, and has no problems with any of the following tests.

assert_type(divmod(m, m), tuple[np.int64, np.timedelta64]) # pyright: ignore[reportArgumentType, reportAssertTypeFailure, reportCallIssue]
assert_type(divmod(m, m_nat), tuple[np.int64, np.timedelta64[None]])
assert_type(divmod(m, m_int0), tuple[np.int64, np.timedelta64[None]])
assert_type(divmod(m, m_int), tuple[np.int64, np.timedelta64[int | None]]) # pyright: ignore[reportArgumentType, reportAssertTypeFailure, reportCallIssue]
assert_type(divmod(m_nat, m), tuple[np.int64, np.timedelta64[None]])
assert_type(divmod(m_int, m_nat), tuple[np.int64, np.timedelta64[None]])
assert_type(divmod(m_int, m_int0), tuple[np.int64, np.timedelta64[None]])
assert_type(divmod(m_int, m_int), tuple[np.int64, np.timedelta64[int | None]]) # pyright: ignore[reportArgumentType, reportAssertTypeFailure, reportCallIssue]
assert_type(divmod(m_int, m_td), tuple[np.int64, np.timedelta64[int | None]]) # pyright: ignore[reportArgumentType, reportAssertTypeFailure, reportCallIssue]
assert_type(divmod(m_td, m_nat), tuple[np.int64, np.timedelta64[None]])
Expand Down
7 changes: 4 additions & 3 deletions src/numpy-stubs/@test/static/accept/multiarray.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ assert_type(np.busday_count("2011-01", "2011-02"), np.int_)
assert_type(np.busday_count(["2011-01"], "2011-02"), _nt.Array[np.int_])
assert_type(np.busday_count(["2011-01"], date_scalar), _nt.Array[np.int_])

assert_type(np.busday_offset(date_scalar, m), np.datetime64[dt.datetime])
assert_type(np.busday_offset(M, m), np.datetime64[dt.datetime])
assert_type(np.busday_offset(M, 5), np.datetime64[dt.datetime])
# mypy incorrectly infers this as "Any" but pyright behaves correctly
assert_type(np.busday_offset(date_scalar, m), np.datetime64[dt.datetime]) # type: ignore[assert-type]
assert_type(np.busday_offset(M, m), np.datetime64[dt.datetime]) # type: ignore[assert-type]
assert_type(np.busday_offset(M, 5), np.datetime64[dt.datetime]) # type: ignore[assert-type]
assert_type(np.busday_offset(AR_M, m), _nt.Array[np.datetime64[dt.datetime]])
assert_type(np.busday_offset(M, timedelta_seq), _nt.Array[np.datetime64[dt.datetime]])
assert_type(np.busday_offset("2011-01", 1, roll="forward"), np.datetime64[dt.datetime])
Expand Down
9 changes: 6 additions & 3 deletions src/numpy-stubs/@test/static/accept/numeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ assert_type(np.correlate(AR_b, AR_u8), _nt.Array1D[np.unsignedinteger])
assert_type(np.correlate(AR_i8, AR_b), _nt.Array1D[np.signedinteger])
assert_type(np.correlate(AR_i8, AR_f8), _nt.Array1D[np.floating])
assert_type(np.correlate(AR_i8, AR_c16), _nt.Array1D[np.complexfloating])
assert_type(np.correlate(AR_i8, AR_m), _nt.Array1D[np.timedelta64])
# mypy incorrectly infers this as "ndarray[Any, Any]", but pyright behaves correctly
assert_type(np.correlate(AR_i8, AR_m), _nt.Array1D[np.timedelta64]) # type: ignore[assert-type]
assert_type(np.correlate(AR_O, AR_O), _nt.Array1D[np.object_])

assert_type(np.convolve(ints, AR_i8, mode="valid"), _nt.Array1D[np.signedinteger])
Expand All @@ -54,7 +55,8 @@ assert_type(np.convolve(AR_b, AR_u8), _nt.Array1D[np.unsignedinteger])
assert_type(np.convolve(AR_i8, AR_b), _nt.Array1D[np.signedinteger])
assert_type(np.convolve(AR_i8, AR_f8), _nt.Array1D[np.floating])
assert_type(np.convolve(AR_i8, AR_c16), _nt.Array1D[np.complexfloating])
assert_type(np.convolve(AR_i8, AR_m), _nt.Array1D[np.timedelta64])
# mypy incorrectly infers this as "ndarray[Any, Any]", but pyright behaves correctly
assert_type(np.convolve(AR_i8, AR_m), _nt.Array1D[np.timedelta64]) # type: ignore[assert-type]
assert_type(np.convolve(AR_O, AR_O), _nt.Array1D[np.object_])

assert_type(np.outer(i8, AR_i8), _nt.Array2D[np.signedinteger])
Expand All @@ -78,7 +80,8 @@ assert_type(np.tensordot(AR_b, AR_u8), _nt.Array[np.unsignedinteger])
assert_type(np.tensordot(AR_i8, AR_b), _nt.Array[np.signedinteger])
assert_type(np.tensordot(AR_i8, AR_f8), _nt.Array[np.floating])
assert_type(np.tensordot(AR_i8, AR_c16), _nt.Array[np.complexfloating])
assert_type(np.tensordot(AR_i8, AR_m), _nt.Array[np.timedelta64])
# mypy incorrectly infers this as "ndarray[Any, Any]", but pyright behaves correctly
assert_type(np.tensordot(AR_i8, AR_m), _nt.Array[np.timedelta64]) # type: ignore[assert-type]
assert_type(np.tensordot(AR_O, AR_O), _nt.Array[np.object_])

assert_type(np.isscalar(i8), bool)
Expand Down
9 changes: 5 additions & 4 deletions src/numpy-stubs/@test/static/accept/ufuncs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ assert_type(np.isnat(dt64), np.bool_)
assert_type(np.isnat(td64), np.bool_)
assert_type(np.isnat([td64, td64]), _nt.Array[np.bool_])
assert_type(np.isnat([td64, td64], out=AR_bool), _nt.Array[np.bool_])
assert_type(np.isnat(AR_dt64), _nt.Array[np.bool_])
assert_type(np.isnat(AR_td64), _nt.Array[np.bool_])
assert_type(np.isnat(AR_dt64, out=AR_bool), _nt.Array[np.bool_])
assert_type(np.isnat(AR_td64, out=AR_bool), _nt.Array[np.bool_])
# mypy incorrectly infers the return type as "Any" here, but pyright is correct
assert_type(np.isnat(AR_dt64), _nt.Array[np.bool_]) # type: ignore[assert-type]
assert_type(np.isnat(AR_td64), _nt.Array[np.bool_]) # type: ignore[assert-type]
assert_type(np.isnat(AR_dt64, out=AR_bool), _nt.Array[np.bool_]) # type: ignore[assert-type]
assert_type(np.isnat(AR_td64, out=AR_bool), _nt.Array[np.bool_]) # type: ignore[assert-type]

assert_type(np.isinf(f8), np.bool_)
assert_type(np.isinf(AR_f8), _nt.Array[np.bool_])
Expand Down
6 changes: 0 additions & 6 deletions src/numpy-stubs/@test/static/reject/arithmetic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ AR_LIKE_m - AR_M # type: ignore[operator] # pyright: ignore[reportOperatorIssu

# array floor division

AR_M // AR_LIKE_b # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
AR_M // AR_LIKE_u # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
AR_M // AR_LIKE_i # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
AR_M // AR_LIKE_f # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
AR_M // AR_LIKE_c # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
AR_M // AR_LIKE_m # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
AR_M // AR_LIKE_M # type: ignore[operator] # pyright: ignore[reportOperatorIssue]

AR_b // AR_LIKE_M # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
Expand All @@ -59,7 +54,6 @@ AR_c // AR_LIKE_M # type: ignore[operator] # pyright: ignore[reportOperatorIss
AR_m // AR_LIKE_M # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
AR_M // AR_LIKE_M # type: ignore[operator] # pyright: ignore[reportOperatorIssue]

AR_m // AR_LIKE_b # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
AR_m // AR_LIKE_c # type: ignore[operator] # pyright: ignore[reportOperatorIssue]

AR_b // AR_LIKE_m # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
Expand Down
Loading