Describe the issue:
import numpy as np
from typing import reveal_type
reveal_type(np.char.array('foo', unicode=False))
outputs
t.py:4: note: Revealed type is "numpy._core.defchararray.chararray[builtins.tuple[Any, ...], numpy.dtype[numpy.str_]]"
I'd have expected
t.py:4: note: Revealed type is "numpy._core.defchararray.chararray[builtins.tuple[Any, ...], numpy.dtype[numpy.bytes_]]"
I think the issue is
|
@overload |
|
def array( |
|
obj: U_co, |
|
itemsize: int | None = ..., |
|
copy: bool = ..., |
|
unicode: L[False] = ..., |
|
order: _OrderKACF = ..., |
|
) -> _CharArray[str_]: ... |
The default for unicode is None, not Literal[False]. It seems the idea is:
unicode=True: return np.str_ type
unicode=False: return np.bytes_ type
- unicode=None` (default): return either of the above, depending on the input
Spotted this while trying out https://github.com/MarcoGorelli/fix-overload-defaults
Reproduce the code example:
Error message:
Python and NumPy Versions:
2.4.0.dev0+git20250714.cc92651
3.12.11 | packaged by conda-forge | (main, Jun 4 2025, 14:45:31) [GCC 13.3.0]
Type-checker version and settings:
mypy 1.16
Additional typing packages.
No response
Describe the issue:
outputs
I'd have expected
I think the issue is
numpy/numpy/_core/defchararray.pyi
Lines 1049 to 1056 in 46c0bff
The default for
unicodeisNone, notLiteral[False]. It seems the idea is:unicode=True: returnnp.str_typeunicode=False: returnnp.bytes_typeSpotted this while trying out https://github.com/MarcoGorelli/fix-overload-defaults
Reproduce the code example:
Error message:
Python and NumPy Versions:
2.4.0.dev0+git20250714.cc92651
3.12.11 | packaged by conda-forge | (main, Jun 4 2025, 14:45:31) [GCC 13.3.0]
Type-checker version and settings:
mypy 1.16
Additional typing packages.
No response