From ce0defe4b63cef53687f840fbcdfabf4d12fb478 Mon Sep 17 00:00:00 2001 From: KsanaKozlova Date: Wed, 2 Sep 2020 19:13:43 +0300 Subject: [PATCH 1/3] add new python typing --- numba_typing/new_typing.py | 127 +++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 numba_typing/new_typing.py diff --git a/numba_typing/new_typing.py b/numba_typing/new_typing.py new file mode 100644 index 000000000..9f39aab27 --- /dev/null +++ b/numba_typing/new_typing.py @@ -0,0 +1,127 @@ +import typing +import numpy as np +import pandas as pd +from abc import abstractmethod +import typing_extensions + +T = typing.TypeVar('T') +S = typing.TypeVar('S') + + +class Array(typing.Generic[T, S]): + '''Annotation for np.ndarray + Use square brackets to indicate type and dimension + For example: Array[int, typing_extensions.Literal[4]]''' + __slots__ = () + + @abstractmethod + def __array__(self) -> np.ndarray: + pass + + +class Series(typing.Generic[T, S]): + '''Annotation for pandas.Series + For expample: Series[int, str] + int - type of index + str - type of value''' + __slots__ = () + + @abstractmethod + def __array__(self) -> pd.core.series.Series: + pass + + +class int8(typing_extensions.Protocol): + """Annotation for int8""" + @abstractmethod + def __int__(self) -> int: + pass + + +class int16(typing_extensions.Protocol): + """Annotation for int16""" + @abstractmethod + def __int__(self) -> int: + pass + + +class int32(typing_extensions.Protocol): + """Annotation for int32""" + @abstractmethod + def __int__(self) -> int: + pass + + +class int64(typing_extensions.Protocol): + """Annotation for int64""" + @abstractmethod + def __int__(self) -> int: + pass + + +class unint8(typing_extensions.Protocol): + """Annotation for unsigned int8""" + @abstractmethod + def __int__(self) -> int: + pass + + +class unint16(typing_extensions.Protocol): + """Annotation for unsigned int16""" + @abstractmethod + def __int__(self) -> int: + pass + + +class unint32(typing_extensions.Protocol): + """Annotation for unsigned int32""" + @abstractmethod + def __int__(self) -> int: + pass + + +class unint64(typing_extensions.Protocol): + """Annotation for unsigned int64""" + @abstractmethod + def __int__(self) -> int: + pass + + +class float32(typing_extensions.Protocol): + """Annotation for float32""" + @abstractmethod + def __float__(self) -> float: + pass + + +class float64(typing_extensions.Protocol): + """Annotation for float32""" + @abstractmethod + def __float__(self) -> float: + pass + + +number = typing.Union[int, float] # numba's number type + + +T_int = typing.TypeVar('T_int', int8, int16, int32, int64) +T_float = typing.TypeVar('T_float', float32, float64) + + +class LiteralType(typing.Generic[T]): + pass + + +class PreferLiteralType(typing.Generic[T]): + pass + + +class PreferNonLiteralType(typing.Generic[T]): + pass + + +L_int = LiteralType[int] +L_float = LiteralType[float] + +TL_int = LiteralType[T_int] +TL_float = LiteralType[T_float] From 90e436e13224eefd791c60e3b835e0ab168bd561 Mon Sep 17 00:00:00 2001 From: KsanaKozlova Date: Thu, 3 Sep 2020 18:08:18 +0300 Subject: [PATCH 2/3] split into several files --- numba_typing/new_types/__init__.py | 2 + numba_typing/new_types/array.py | 17 ++++++ numba_typing/new_types/number_types.py | 80 ++++++++++++++++++++++++++ numba_typing/new_types/special.py | 23 ++++++++ 4 files changed, 122 insertions(+) create mode 100644 numba_typing/new_types/__init__.py create mode 100644 numba_typing/new_types/array.py create mode 100644 numba_typing/new_types/number_types.py create mode 100644 numba_typing/new_types/special.py diff --git a/numba_typing/new_types/__init__.py b/numba_typing/new_types/__init__.py new file mode 100644 index 000000000..a207bb59f --- /dev/null +++ b/numba_typing/new_types/__init__.py @@ -0,0 +1,2 @@ + +__all__ = ['array', 'number_types', 'pandas', 'special'] diff --git a/numba_typing/new_types/array.py b/numba_typing/new_types/array.py new file mode 100644 index 000000000..ea1595dc8 --- /dev/null +++ b/numba_typing/new_types/array.py @@ -0,0 +1,17 @@ +import typing +import numpy as np +from abc import abstractmethod + +T = typing.TypeVar('T') +S = typing.TypeVar('S') + + +class Array(typing.Generic[T, S]): + '''Annotation for np.ndarray + Use square brackets to indicate type and dimension + For example: Array[int, typing_extensions.Literal[4]]''' + __slots__ = () + + @abstractmethod + def __array__(self) -> np.ndarray: + pass diff --git a/numba_typing/new_types/number_types.py b/numba_typing/new_types/number_types.py new file mode 100644 index 000000000..e279dca2c --- /dev/null +++ b/numba_typing/new_types/number_types.py @@ -0,0 +1,80 @@ +import typing +from abc import abstractmethod +import typing_extensions + + +class int8(typing_extensions.Protocol): + """Annotation for int8""" + @abstractmethod + def __int__(self) -> int: + pass + + +class int16(typing_extensions.Protocol): + """Annotation for int16""" + @abstractmethod + def __int__(self) -> int: + pass + + +class int32(typing_extensions.Protocol): + """Annotation for int32""" + @abstractmethod + def __int__(self) -> int: + pass + + +class int64(typing_extensions.Protocol): + """Annotation for int64""" + @abstractmethod + def __int__(self) -> int: + pass + + +class unint8(typing_extensions.Protocol): + """Annotation for unsigned int8""" + @abstractmethod + def __int__(self) -> int: + pass + + +class unint16(typing_extensions.Protocol): + """Annotation for unsigned int16""" + @abstractmethod + def __int__(self) -> int: + pass + + +class unint32(typing_extensions.Protocol): + """Annotation for unsigned int32""" + @abstractmethod + def __int__(self) -> int: + pass + + +class unint64(typing_extensions.Protocol): + """Annotation for unsigned int64""" + @abstractmethod + def __int__(self) -> int: + pass + + +class float32(typing_extensions.Protocol): + """Annotation for float32""" + @abstractmethod + def __float__(self) -> float: + pass + + +class float64(typing_extensions.Protocol): + """Annotation for float32""" + @abstractmethod + def __float__(self) -> float: + pass + + +number = typing.Union[int, float] # numba's number type + + +T_int = typing.TypeVar('T_int', int8, int16, int32, int64) +T_float = typing.TypeVar('T_float', float32, float64) diff --git a/numba_typing/new_types/special.py b/numba_typing/new_types/special.py new file mode 100644 index 000000000..e2cc044f6 --- /dev/null +++ b/numba_typing/new_types/special.py @@ -0,0 +1,23 @@ +import typing +import number_types + +T = typing.TypeVar('T') + + +class LiteralType(typing.Generic[T]): + pass + + +class PreferLiteralType(typing.Generic[T]): + pass + + +class PreferNonLiteralType(typing.Generic[T]): + pass + + +L_int = LiteralType[int] +L_float = LiteralType[float] + +TL_int = LiteralType[number_types.T_int] +TL_float = LiteralType[number_types.T_float] From ac86694003c12b030f48f94056d2cb0c5b26ee13 Mon Sep 17 00:00:00 2001 From: KsanaKozlova Date: Fri, 4 Sep 2020 16:38:21 +0300 Subject: [PATCH 3/3] rename files --- numba_typing/new_types/__init__.py | 2 +- numba_typing/new_types/pandas_Series.py | 18 +++++++++++++++ numba_typing/new_types/special_LiteralType.py | 23 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 numba_typing/new_types/pandas_Series.py create mode 100644 numba_typing/new_types/special_LiteralType.py diff --git a/numba_typing/new_types/__init__.py b/numba_typing/new_types/__init__.py index a207bb59f..240ba8240 100644 --- a/numba_typing/new_types/__init__.py +++ b/numba_typing/new_types/__init__.py @@ -1,2 +1,2 @@ -__all__ = ['array', 'number_types', 'pandas', 'special'] +__all__ = ['array', 'number_types', 'pandas_series', 'special_LiteralType'] diff --git a/numba_typing/new_types/pandas_Series.py b/numba_typing/new_types/pandas_Series.py new file mode 100644 index 000000000..f98eea48f --- /dev/null +++ b/numba_typing/new_types/pandas_Series.py @@ -0,0 +1,18 @@ +import typing +import pandas as pd +from abc import abstractmethod + +T = typing.TypeVar('T') +S = typing.TypeVar('S') + + +class Series(typing.Generic[T, S]): + '''Annotation for pandas.Series + For expample: Series[int, str] + int - type of index + str - type of value''' + __slots__ = () + + @abstractmethod + def __array__(self) -> pd.core.series.Series: + pass diff --git a/numba_typing/new_types/special_LiteralType.py b/numba_typing/new_types/special_LiteralType.py new file mode 100644 index 000000000..e2cc044f6 --- /dev/null +++ b/numba_typing/new_types/special_LiteralType.py @@ -0,0 +1,23 @@ +import typing +import number_types + +T = typing.TypeVar('T') + + +class LiteralType(typing.Generic[T]): + pass + + +class PreferLiteralType(typing.Generic[T]): + pass + + +class PreferNonLiteralType(typing.Generic[T]): + pass + + +L_int = LiteralType[int] +L_float = LiteralType[float] + +TL_int = LiteralType[number_types.T_int] +TL_float = LiteralType[number_types.T_float]