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

Expand TypeChecker to raise SDCLimitation('...Unsupported...') #771

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 0 additions & 5 deletions sdc/datatypes/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@
TypeChecker)


class SDCLimitation(Exception):
"""Exception to be raised in case of SDC limitation"""
pass


def hpat_arrays_append(A, B):
pass

Expand Down
2 changes: 1 addition & 1 deletion sdc/datatypes/hpat_pandas_dataframe_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
from sdc.hiframes.pd_dataframe_type import DataFrameType
from sdc.datatypes.hpat_pandas_dataframe_getitem_types import (DataFrameGetitemAccessorType,
dataframe_getitem_accessor_init)
from sdc.datatypes.common_functions import SDCLimitation
from sdc.utilities.sdc_typing_utils import SDCLimitation
from sdc.datatypes.hpat_pandas_dataframe_rolling_types import _hpat_pandas_df_rolling_init
from sdc.datatypes.hpat_pandas_rolling_types import (
gen_sdc_pandas_rolling_overload_body, sdc_pandas_rolling_docstring_tmpl)
Expand Down
2 changes: 1 addition & 1 deletion sdc/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from numba.errors import TypingError

import sdc
from sdc.datatypes.common_functions import SDCLimitation
from sdc.utilities.sdc_typing_utils import SDCLimitation
from sdc.tests.gen_test_data import ParquetGenerator
from sdc.tests.test_base import TestCase
from sdc.tests.test_utils import (check_numba_version,
Expand Down
46 changes: 41 additions & 5 deletions sdc/utilities/sdc_typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@
from sdc.str_arr_type import string_array_type


class SDCLimitation(Exception):
"""Exception to be raised in case of SDC limitation"""
pass


class TypeChecker:
"""
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: {}'
default_tmpl = '{} The object {}\n given: {}\n expected: {}'

def __init__(self, func_name):
"""
Expand All @@ -59,9 +64,23 @@ def __init__(self, func_name):
"""
self.func_name = func_name

def raise_exc(self, data, expected_types, name=''):
def _raise_exc(self, exc_cls, tmpl, *args):
"""
Generic for raising exception
Parameters
----------
tmpl: :obj:`any`
template for exception message
exc_cls: :obj:`Exception`
class of the exception to be raised
args: :obj:`list`
arguments to render template
"""
raise exc_cls(tmpl.format(*args))

def raise_exc(self, data, expected_types, name='', exc_cls=TypingError):
"""
Raise exception with unified message
Raise exception with message about invalid type of parameter
Parameters
----------
data: :obj:`any`
Expand All @@ -70,9 +89,26 @@ def raise_exc(self, data, expected_types, name=''):
expected types inserting directly to the exception
name: :obj:`str`
name of the parameter
exc_cls: :obj:`Exception`
class of the exception to be raised
"""
self._raise_exc(exc_cls, self.default_tmpl, self.func_name,
name, data, expected_types)

def raise_unsupported_exc(self, data, name='', exc_cls=SDCLimitation):
Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking of replacing all raises related to SDC limitations with this, but it seems this only covers case of unsupported parameters, so for instance in series.fillna we have:

            raise TypingError('{} Not implemented when Series dtype is {} and\
                 inplace={}'.format(_func_name, self.dtype, inplace))

Maybe add in raise_exc and internal func additional parameter e.g. msg, which if exists will be used instead of default msg format?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean to replace call

msg = '{} Not implemented when Series dtype is {} and inplace={}'.format(_func_name, self.dtype, inplace)
raise SDCLimitation(msg)

with something like this

msg = '{} Not implemented when Series dtype is {} and inplace={}'.format(_func_name, self.dtype, inplace)
ty_checker.raise_exc(None, None, exc_cls=SDCLimitation, msg=msg)

?

I think TypeChecker shouldn't be used for such complicated cases, TypeChecker doesn't make sense, when it increases the amount of code and makes it harder.
Maybe I didn't catch you correctly, please correct me in this case.

"""
Raise exception with message about unsupported parameter
Parameters
----------
data: :obj:`any`
real type of the data
name: :obj:`str`
name of the parameter
exc_cls: :obj:`Exception`
class of the exception to be raised
"""
msg = self.msg_template.format(self.func_name, name, data, expected_types)
raise TypingError(msg)
tmpl = '{} Unsupported object {}\n given: {}'
self._raise_exc(exc_cls, tmpl, self.func_name, name, data)

def check(self, data, accepted_type, name=''):
"""
Expand Down