Skip to content

Commit cd97e2e

Browse files
authored
Merge pull request #28283 from charris/backport-28279
TYP: Fix scalar constructors
2 parents ccf97a7 + 3cfe1fe commit cd97e2e

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

numpy/__init__.pyi

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,6 +4134,9 @@ float32: TypeAlias = floating[_32Bit]
41344134

41354135
# either a C `double`, `float`, or `longdouble`
41364136
class float64(floating[_64Bit], float): # type: ignore[misc]
4137+
def __new__(cls, x: _ConvertibleToFloat | None = ..., /) -> Self: ...
4138+
4139+
#
41374140
@property
41384141
def itemsize(self) -> L[8]: ...
41394142
@property
@@ -4268,7 +4271,15 @@ longdouble: TypeAlias = floating[_NBitLongDouble]
42684271
# describing the two 64 bit floats representing its real and imaginary component
42694272

42704273
class complexfloating(inexact[_NBit1, complex], Generic[_NBit1, _NBit2]):
4271-
def __init__(self, value: _ConvertibleToComplex | None = ..., /) -> None: ...
4274+
@overload
4275+
def __init__(
4276+
self,
4277+
real: complex | SupportsComplex | SupportsFloat | SupportsIndex = ...,
4278+
imag: complex | SupportsFloat | SupportsIndex = ...,
4279+
/,
4280+
) -> None: ...
4281+
@overload
4282+
def __init__(self, real: _ConvertibleToComplex | None = ..., /) -> None: ...
42724283

42734284
@property
42744285
def real(self) -> floating[_NBit1]: ... # type: ignore[override]
@@ -4352,6 +4363,17 @@ class complexfloating(inexact[_NBit1, complex], Generic[_NBit1, _NBit2]):
43524363
complex64: TypeAlias = complexfloating[_32Bit, _32Bit]
43534364

43544365
class complex128(complexfloating[_64Bit, _64Bit], complex): # type: ignore[misc]
4366+
@overload
4367+
def __new__(
4368+
cls,
4369+
real: complex | SupportsComplex | SupportsFloat | SupportsIndex = ...,
4370+
imag: complex | SupportsFloat | SupportsIndex = ...,
4371+
/,
4372+
) -> Self: ...
4373+
@overload
4374+
def __new__(cls, real: _ConvertibleToComplex | None = ..., /) -> Self: ...
4375+
4376+
#
43554377
@property
43564378
def itemsize(self) -> L[16]: ...
43574379
@property
@@ -4706,12 +4728,26 @@ class character(flexible[_CharacterItemT_co], Generic[_CharacterItemT_co]):
47064728

47074729
class bytes_(character[bytes], bytes):
47084730
@overload
4709-
def __init__(self, value: object = ..., /) -> None: ...
4731+
def __new__(cls, o: object = ..., /) -> Self: ...
4732+
@overload
4733+
def __new__(cls, s: str, /, encoding: str, errors: str = ...) -> Self: ...
4734+
4735+
#
4736+
@overload
4737+
def __init__(self, o: object = ..., /) -> None: ...
47104738
@overload
4711-
def __init__(self, value: str, /, encoding: str = ..., errors: str = ...) -> None: ...
4739+
def __init__(self, s: str, /, encoding: str, errors: str = ...) -> None: ...
4740+
4741+
#
47124742
def __bytes__(self, /) -> bytes: ...
47134743

47144744
class str_(character[str], str):
4745+
@overload
4746+
def __new__(cls, value: object = ..., /) -> Self: ...
4747+
@overload
4748+
def __new__(cls, value: bytes, /, encoding: str = ..., errors: str = ...) -> Self: ...
4749+
4750+
#
47154751
@overload
47164752
def __init__(self, value: object = ..., /) -> None: ...
47174753
@overload

numpy/typing/tests/data/fail/scalars.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ np.float32(3j) # E: incompatible type
2828
np.float32([1.0, 0.0, 0.0]) # E: incompatible type
2929
np.complex64([]) # E: incompatible type
3030

31-
np.complex64(1, 2) # E: Too many arguments
3231
# TODO: protocols (can't check for non-existent protocols w/ __getattr__)
3332

3433
np.datetime64(0) # E: No overload variant
@@ -60,7 +59,7 @@ np.flexible(b"test") # E: Cannot instantiate abstract class
6059
np.float64(value=0.0) # E: Unexpected keyword argument
6160
np.int64(value=0) # E: Unexpected keyword argument
6261
np.uint64(value=0) # E: Unexpected keyword argument
63-
np.complex128(value=0.0j) # E: Unexpected keyword argument
62+
np.complex128(value=0.0j) # E: No overload variant
6463
np.str_(value='bob') # E: No overload variant
6564
np.bytes_(value=b'test') # E: No overload variant
6665
np.void(value=b'test') # E: No overload variant

0 commit comments

Comments
 (0)