Skip to content
This repository was 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
Changes from 1 commit
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
41 changes: 36 additions & 5 deletions sdc/utilities/sdc_typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TypeChecker:
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 +59,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):
"""
Raise exception with unified message
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 message about invalid type of parameter
Parameters
----------
data: :obj:`any`
Expand All @@ -70,9 +84,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(self, exc_cls, self.default_tmpl, self.func_name,
name, data, expected_types)

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

Choose a reason for hiding this comment

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

Maybe use default exception as SDCLimitation, as it seems to be more appropriate 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(self, exc_cls, tmpl, self.func_name, name, data)

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