Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Add more specific integer constructors #76

Merged
merged 3 commits into from
Apr 30, 2020
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
33 changes: 25 additions & 8 deletions numpy-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,18 @@ class datetime64:

class integer(number, _real_generic): ...
class signedinteger(integer): ...
class int8(signedinteger): ...
class int16(signedinteger): ...
class int32(signedinteger): ...
class int64(signedinteger): ...

class int8(signedinteger):
def __init__(self, value: SupportsInt = ...) -> None: ...

class int16(signedinteger):
def __init__(self, value: SupportsInt = ...) -> None: ...

class int32(signedinteger):
def __init__(self, value: SupportsInt = ...) -> None: ...

class int64(signedinteger):
def __init__(self, value: SupportsInt = ...) -> None: ...

class timedelta64(signedinteger):
def __init__(self, _data: Any = ..., _format: str = ...) -> None: ...
Expand All @@ -441,10 +449,19 @@ class timedelta64(signedinteger):
def __mod__(self, other: timedelta64) -> timedelta64: ...

class unsignedinteger(integer): ...
class uint8(unsignedinteger): ...
class uint16(unsignedinteger): ...
class uint32(unsignedinteger): ...
class uint64(unsignedinteger): ...

class uint8(unsignedinteger):
def __init__(self, value: SupportsInt = ...) -> None: ...

class uint16(unsignedinteger):
def __init__(self, value: SupportsInt = ...) -> None: ...

class uint32(unsignedinteger):
def __init__(self, value: SupportsInt = ...) -> None: ...

class uint64(unsignedinteger):
def __init__(self, value: SupportsInt = ...) -> None: ...

class inexact(number): ...
class floating(inexact, _real_generic): ...
class float16(floating): ...
Expand Down
15 changes: 15 additions & 0 deletions tests/fail/scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@
td_64 / dt_64 # E: No overload
td_64 % 1 # E: Unsupported operand types
td_64 % dt_64 # E: Unsupported operand types


class A:
def __float__(self):
return 1.0


np.int8(A()) # E: incompatible type
np.int16(A()) # E: incompatible type
np.int32(A()) # E: incompatible type
np.int64(A()) # E: incompatible type
np.uint8(A()) # E: incompatible type
np.uint16(A()) # E: incompatible type
np.uint32(A()) # E: incompatible type
np.uint64(A()) # E: incompatible type
9 changes: 7 additions & 2 deletions tests/pass/scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ def __complex__(self):
return 3j


class B:
def __int__(self):
return 4


class A:
def __float__(self):
return 4
return 4.0


np.complex32(3)
Expand All @@ -20,7 +25,7 @@ def __float__(self):
np.int16(3.4)
np.int32(4)
np.int64(-1)
np.uint8(A())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of removing, I would follow what is done here:

https://github.com/numpy/numpy-stubs/pull/70/files#diff-dd377d4c09025262e6f7008bcfe583dfR10

i.e.

class B:
    def __int__(self):
        return 4

...

np.uint8(B())

(That way we keep a case checking that a class with __int__ passes.)

np.uint8(B())
np.uint32()

np.float16(A())
Expand Down