Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
Moved TypeChecker from this PR to other one
Browse files Browse the repository at this point in the history
  • Loading branch information
densmirn committed Oct 31, 2019
1 parent e7da101 commit 60cdd9a
Showing 1 changed file with 20 additions and 81 deletions.
101 changes: 20 additions & 81 deletions hpat/datatypes/hpat_pandas_series_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import numpy
import operator
import pandas
from collections.abc import Iterable
from enum import Enum

from numba.errors import TypingError
from numba.extending import overload, overload_method, overload_attribute
Expand All @@ -45,77 +43,6 @@
from hpat.utils import to_array


class AcceptedType(Enum):
series = (SeriesType,),
omitted = (types.Omitted,),
int_ = (int, types.Integer),
float_ = (float, types.Float),
number = (int, float, types.Number),
bool_ = (bool, types.Boolean),
str_ = (str, types.UnicodeType, types.StringLiteral)


class TypingChecker:
"""
Validate object type and raise TypingError if the type is invalid, e.g.:
Method nsmallest(). The object n
given: bool
expected: int
"""
msg_template = '{} The object {}\n given: {}\n expected: {}'

def __init__(self, func_name):
"""
Parameters
----------
func_name: :obj:`str`
name of the function where types checking
"""
self.func_name = func_name

def msg(self, val, ty, name=''):
"""
Message of the exception in the special format
Parameters
----------
val: :obj:`any`
real type of the data
ty: :obj:`str`
expected type of the data
name: :obj:`str`
name of the parameter
Returns
-------
:class:`str`
message of the exception in the special format
"""
return self.msg_template.format(self.func_name, name, val, ty)

def check(self, data, accepted_types, name=''):
"""
Check data type belongs to specified type or list of types
Parameters
----------
data: :obj:`any`
real type of the data
accepted_types: :obj:`tuple` or :obj:`AcceptedType`
accepted types
name: :obj:`str`
name of the parameter
"""
if not isinstance(accepted_types, Iterable):
accepted_types = (accepted_types,)

for ty in accepted_types:
if isinstance(data, ty.value):
return True

raise TypingError(self.msg(data, ty.name, name=name))


@overload(operator.getitem)
def hpat_pandas_series_getitem(self, idx):
"""
Expand Down Expand Up @@ -234,10 +161,16 @@ def hpat_pandas_series_nsmallest(self, n=5, keep='first'):
returns :obj:`series`
"""

ty_checker = TypingChecker('Method nsmallest().')
ty_checker.check(self, AcceptedType.series)
ty_checker.check(n, (AcceptedType.omitted, AcceptedType.int_), name='n')
ty_checker.check(keep, (AcceptedType.omitted, AcceptedType.str_), name='keep')
_func_name = 'Method nsmallest().'

if not isinstance(self, SeriesType):
raise TypingError('{} The object\n given: {}\n expected: {}'.format(_func_name, self, 'series'))

if not isinstance(n, (types.Omitted, int, types.Integer)):
raise TypingError('{} The object n\n given: {}\n expected: {}'.format(_func_name, n, 'int'))

if not isinstance(keep, (types.Omitted, str, types.UnicodeType, types.StringLiteral)):
raise TypingError('{} The object keep\n given: {}\n expected: {}'.format(_func_name, keep, 'str'))

def hpat_pandas_series_nsmallest_impl(self, n=5, keep='first'):
if keep != 'first':
Expand Down Expand Up @@ -278,10 +211,16 @@ def hpat_pandas_series_nlargest(self, n=5, keep='first'):
returns :obj:`series`
"""

ty_checker = TypingChecker('Method nlargest().')
ty_checker.check(self, AcceptedType.series)
ty_checker.check(n, (AcceptedType.omitted, AcceptedType.int_), name='n')
ty_checker.check(keep, (AcceptedType.omitted, AcceptedType.str_), name='keep')
_func_name = 'Method nlargest().'

if not isinstance(self, SeriesType):
raise TypingError('{} The object\n given: {}\n expected: {}'.format(_func_name, self, 'series'))

if not isinstance(n, (types.Omitted, int, types.Integer)):
raise TypingError('{} The object n\n given: {}\n expected: {}'.format(_func_name, n, 'int'))

if not isinstance(keep, (types.Omitted, str, types.UnicodeType, types.StringLiteral)):
raise TypingError('{} The object keep\n given: {}\n expected: {}'.format(_func_name, keep, 'str'))

def hpat_pandas_series_nlargest_impl(self, n=5, keep='first'):
if keep != 'first':
Expand Down

0 comments on commit 60cdd9a

Please sign in to comment.