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
70 changes: 60 additions & 10 deletions numpy/_core/defchararray.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1044,66 +1044,116 @@ def startswith(
def str_len(A: UST_co) -> NDArray[int_]: ...

# Overload 1 and 2: str- or bytes-based array-likes
# overload 3: arbitrary object with unicode=False (-> bytes_)
# overload 4: arbitrary object with unicode=True (-> str_)
# overload 3 and 4: arbitrary object with unicode=False (-> bytes_)
# overload 5 and 6: arbitrary object with unicode=True (-> str_)
# overload 7: arbitrary object with unicode=None (default) (-> str_ | bytes_)
@overload
def array(
obj: U_co,
itemsize: int | None = ...,
copy: bool = ...,
unicode: L[False] = ...,
unicode: L[True] | None = ...,
order: _OrderKACF = ...,
) -> _CharArray[str_]: ...
@overload
def array(
obj: S_co,
itemsize: int | None = ...,
copy: bool = ...,
unicode: L[False] = ...,
unicode: L[False] | None = ...,
order: _OrderKACF = ...,
) -> _CharArray[bytes_]: ...
@overload
def array(
obj: object,
itemsize: int | None,
copy: bool,
unicode: L[False],
order: _OrderKACF = ...,
) -> _CharArray[bytes_]: ...
@overload
def array(
obj: object,
itemsize: int | None = ...,
copy: bool = ...,
unicode: L[False] = ...,
*,
unicode: L[False],
order: _OrderKACF = ...,
) -> _CharArray[bytes_]: ...
@overload
def array(
obj: object,
itemsize: int | None,
copy: bool,
unicode: L[True],
order: _OrderKACF = ...,
) -> _CharArray[str_]: ...
@overload
def array(
obj: object,
itemsize: int | None = ...,
copy: bool = ...,
unicode: L[True] = ...,
*,
unicode: L[True],
order: _OrderKACF = ...,
) -> _CharArray[str_]: ...
@overload
def array(
obj: object,
itemsize: int | None = ...,
copy: bool = ...,
unicode: bool | None = ...,
order: _OrderKACF = ...,
) -> _CharArray[str_] | _CharArray[bytes_]: ...

@overload
def asarray(
obj: U_co,
itemsize: int | None = ...,
unicode: L[False] = ...,
unicode: L[True] | None = ...,
order: _OrderKACF = ...,
) -> _CharArray[str_]: ...
@overload
def asarray(
obj: S_co,
itemsize: int | None = ...,
unicode: L[False] = ...,
unicode: L[False] | None = ...,
order: _OrderKACF = ...,
) -> _CharArray[bytes_]: ...
@overload
def asarray(
obj: object,
itemsize: int | None,
unicode: L[False],
order: _OrderKACF = ...,
) -> _CharArray[bytes_]: ...
@overload
def asarray(
obj: object,
itemsize: int | None = ...,
unicode: L[False] = ...,
*,
unicode: L[False],
order: _OrderKACF = ...,
) -> _CharArray[bytes_]: ...
@overload
def asarray(
obj: object,
itemsize: int | None,
unicode: L[True],
order: _OrderKACF = ...,
) -> _CharArray[str_]: ...
@overload
def asarray(
obj: object,
itemsize: int | None = ...,
unicode: L[True] = ...,
*,
unicode: L[True],
order: _OrderKACF = ...,
) -> _CharArray[str_]: ...
@overload
def asarray(
obj: object,
itemsize: int | None = ...,
unicode: bool | None = ...,
order: _OrderKACF = ...,
) -> _CharArray[str_] | _CharArray[bytes_]: ...
6 changes: 6 additions & 0 deletions numpy/typing/tests/data/reveal/char.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,16 @@ assert_type(np.char.array("bob", copy=True), np.char.chararray[np_t._AnyShape, n
assert_type(np.char.array(b"bob", itemsize=5), np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.array(1, unicode=False), np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.array(1, unicode=True), np.char.chararray[np_t._AnyShape, np.dtype[np.str_]])
assert_type(np.char.array(1), np.char.chararray[np_t._AnyShape, np.dtype[np.str_]] | np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.array(AR_U, unicode=False), np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.array(AR_S, unicode=True), np.char.chararray[np_t._AnyShape, np.dtype[np.str_]])

assert_type(np.char.asarray(AR_U), np.char.chararray[np_t._AnyShape, np.dtype[np.str_]])
assert_type(np.char.asarray(AR_S, order="K"), np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.asarray("bob"), np.char.chararray[np_t._AnyShape, np.dtype[np.str_]])
assert_type(np.char.asarray(b"bob", itemsize=5), np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.asarray(1, unicode=False), np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.asarray(1, unicode=True), np.char.chararray[np_t._AnyShape, np.dtype[np.str_]])
assert_type(np.char.asarray(1), np.char.chararray[np_t._AnyShape, np.dtype[np.str_]] | np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.asarray(AR_U, unicode=False), np.char.chararray[np_t._AnyShape, np.dtype[np.bytes_]])
assert_type(np.char.asarray(AR_S, unicode=True), np.char.chararray[np_t._AnyShape, np.dtype[np.str_]])
Loading