Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making typed.List a typing Generic #5958

Merged
merged 26 commits into from
Feb 1, 2021
Merged
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3628b53
setting List as typing Generic
luk-f-a Jul 3, 2020
0857207
flake8 fix
luk-f-a Jul 3, 2020
c0015eb
adding annotation to len and contains
luk-f-a Jul 3, 2020
ece5174
fixes from review and new signatures
luk-f-a Nov 27, 2020
2f96450
Merge branch 'master' of https://github.com/numba/numba into typed-li…
luk-f-a Nov 27, 2020
4d0db93
additional fixes
luk-f-a Nov 27, 2020
aebe1e9
fixing wrong overload
luk-f-a Nov 27, 2020
fc3d5a0
flake8 fixes
luk-f-a Nov 27, 2020
c66c6b2
one more flake8 fix
luk-f-a Nov 27, 2020
a7b88f9
flake8 again
luk-f-a Nov 27, 2020
a6fcb13
several improvements following review and to make mypy pass
luk-f-a Nov 28, 2020
6529714
flake8
luk-f-a Nov 28, 2020
87fb3c7
adding py.typed file
luk-f-a Dec 5, 2020
4199351
Merge branch 'master' of https://github.com/numba/numba into typed-li…
luk-f-a Dec 5, 2020
2fd3e59
explicit optional inputs
luk-f-a Jan 6, 2021
c4f0139
Update numba/typed/typedlist.py
luk-f-a Jan 28, 2021
bb375b9
Update numba/typed/typedlist.py
luk-f-a Jan 28, 2021
4c725e3
Update numba/typed/typedlist.py
luk-f-a Jan 28, 2021
02fe12a
added _Sequence to deal with Sized sequences with getitem
luk-f-a Jan 28, 2021
9838739
importing Protocol from typing_extensions
luk-f-a Jan 28, 2021
e8294ca
fallback in case Protocol is not found in typing (pre Py 3.8)
luk-f-a Jan 29, 2021
a8c8edc
remove need for custom Protocol by removing getitem in extend
luk-f-a Jan 29, 2021
f4635de
Revert "remove need for custom Protocol by removing getitem in extend"
luk-f-a Jan 29, 2021
f511819
adds a python version guard
luk-f-a Jan 29, 2021
d9c80c2
flake8 fix
luk-f-a Jan 29, 2021
1dfa46f
less verbose check for python version
luk-f-a Jan 31, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 26 additions & 14 deletions numba/typed/typedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from numba.typed import listobject
from numba.core.errors import TypingError, LoweringError
from numba.core.typing.templates import Signature
import typing as pt

DEFAULT_ALLOCATED = listobject.DEFAULT_ALLOCATED

Expand Down Expand Up @@ -170,7 +171,10 @@ def _from_meminfo_ptr(ptr, listtype):
return List(meminfo=ptr, lsttype=listtype)


class List(MutableSequence):
T = pt.TypeVar('T')


class List(MutableSequence, pt.Generic[T]):
"""A typed-list usable in Numba compiled functions.

Implements the MutableSequence interface.
Expand Down Expand Up @@ -278,7 +282,7 @@ def _initialise_list(self, item):
lsttype = types.ListType(typeof(item))
self._list_type, self._opaque = self._parse_arg(lsttype)

def __len__(self):
def __len__(self) -> int:
if not self._typed:
return 0
else:
Expand Down Expand Up @@ -317,44 +321,52 @@ def __gt__(self, other):
def __ge__(self, other):
return _ge(self, other)

def append(self, item):
def append(self, item: T) -> None:
if not self._typed:
self._initialise_list(item)
_append(self, item)

def __setitem__(self, i, item):
def __setitem__(self, i, item: T) -> None:
if not self._typed:
self._initialise_list(item)
_setitem(self, i, item)

def __getitem__(self, i):
# noqa F811 comments required due to github.com/PyCQA/pyflakes/issues/592
# noqa E704 required to follow overload style of using ... in the same line
@pt.overload
def __getitem__(self, i: int) -> T: ... # noqa: F811, E704
@pt.overload
def __getitem__(self, i: slice) -> 'List': ... # noqa: F811, E704
luk-f-a marked this conversation as resolved.
Show resolved Hide resolved

def __getitem__(self, i: pt.Union[int, slice] # noqa: F811
) -> pt.Union[T, 'List']:
if not self._typed:
raise IndexError
else:
return _getitem(self, i)

def __iter__(self):
def __iter__(self) -> pt.Iterator[T]:
for i in range(len(self)):
yield self[i]

def __contains__(self, item):
def __contains__(self, item: T) -> bool:
return _contains(self, item)

def __delitem__(self, i):
def __delitem__(self, i: pt.Union[int, slice]) -> None:
luk-f-a marked this conversation as resolved.
Show resolved Hide resolved
_delitem(self, i)

def insert(self, i, item):
def insert(self, i: int, item: T) -> None:
if not self._typed:
self._initialise_list(item)
_insert(self, i, item)

def count(self, item):
def count(self, item: T) -> int:
return _count(self, item)

def pop(self, i=-1):
def pop(self, i: int = -1) -> T:
luk-f-a marked this conversation as resolved.
Show resolved Hide resolved
return _pop(self, i)

def extend(self, iterable):
def extend(self, iterable: pt.Iterable[T]) -> None:
# Empty iterable, do nothing
if len(iterable) == 0:
return self
Expand All @@ -365,7 +377,7 @@ def extend(self, iterable):
self._initialise_list(iterable[0])
return _extend(self, iterable)

def remove(self, item):
def remove(self, item: T) -> None:
return _remove(self, item)

def clear(self):
Expand All @@ -377,7 +389,7 @@ def reverse(self):
def copy(self):
return _copy(self)

def index(self, item, start=None, stop=None):
def index(self, item: T, start: int = None, stop: int = None) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

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

Under strict mode inferred optionals are disabled, so these should be Optional[int]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, I didn't realize that strict optionals was the default, I thought that inferred optionals were the default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if we shouldn't consider switching to --no-strict-optional to avoid the added verbosity. For example, index could fit in one line (easier to read) without losing much in terms of typing (I think :int = None is clear enough). what do you think?

    def index(self, item: T, start: pt.Optional[int] = None,
              stop: pt.Optional[int] = None) -> int:

vs

def index(self, item: T, start: int = None, stop: int = None) -> int:

Copy link
Contributor

Choose a reason for hiding this comment

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

For internal annotations that's something we can decide on. I think for external facing annotations we should be as precise as possible to avoid incompatibilities for users who don't use --no-strict-optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you're right, I changed it to the explicit Optional.

return _index(self, item, start, stop)

def sort(self, key=None, reverse=False):
Expand Down