8000 automatic generation of type checks for overload by vlad-perevezentsev · Pull Request #911 · IntelPython/sdc · GitHub
[go: up one dir, main page]

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

automatic generation of type checks for overload #911

Open
wants to merge 12 commits into
base: numba_typing
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
remove full_match and add a signature matching check
  • Loading branch information
vlad-perevezentsev committed Sep 15, 2020
commit 1cb60da757ef2fccd52f3944aa5d2b238a5228fd
49 changes: 30 additions & 19 deletions numba_typing/overload_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def wrapper(*args):
if args_orig_func.defaults:
defaults_dict = {name: value for name, value in zip(
args_orig_func.args[::-1], args_orig_func.defaults[::-1])}
result = choose_func_by_sig(sig_list, values_dict, defaults_dict)
if valid_signature(sig_list, values_dict, defaults_dict):
result = choose_func_by_sig(sig_list, values_dict)

if result is None:
raise TypeError(f'Unsupported types a={a}, b={b}')
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks both a and b are undefined.

Expand All @@ -33,6 +34,22 @@ def wrapper(*args):
return overload_inner


def valid_signature(list_signature, values_dict, defaults_dict):
def check_defaults(sig_def):
for name, val in defaults_dict.items():
if sig_def.get(name) is None:
raise AttributeError(f'{name} does not match the signature of the function passed to overload_list')
if not sig_def[name] == val:
Copy link
Contributor

Choose a reason for hiding this comment

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

This condition looks pretty strange. Maybe if sig_def[name] != val?

raise ValueError(f'The default arguments are not equal: {name}: {val} != {sig_def[name]}')

for sig, _ in list_signature:
for param in sig.parameters:
if len(param) != len(values_dict.items()):
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to call method items() here.

check_defaults(sig.defaults)

return True


def check_int_type(n_type):
return isinstance(n_type, types.Integer)

Expand Down Expand Up @@ -155,26 +172,20 @@ def match_generic(self, p_type, n_type):
TypeChecker.add_type_check(dict, check_dict_type)


def choose_func_by_sig(sig_list, values_dict, defaults_dict):
checker = TypeChecker()
for sig, func in sig_list: # sig = (Signature,func)
for param in sig.parameters: # param = {'a':int,'b':int}
for name, typ in values_dict.items(): # name,type = 'a',int64
if isinstance(typ, types.Literal):
typ = typ.literal_type

full_match = checker.match(param[name], typ)
def choose_func_by_sig(sig_list, values_dict):
def check_signature(sig_params, types_dict):
checker = TypeChecker()
for name, typ in types_dict.items(): # name,type = 'a',int64
if isinstance(typ, types.Literal):
typ = typ.literal_type
if not checker.match(sig_params[name], typ):
return False

if not full_match:
break

if len(param) != len(values_dict.items()):
for name, val in defaults_dict.items():
if not sig.defaults.get(name) is None:
full_match = full_match and sig.defaults[name] == val
return True

checker.clear_typevars_dict()
if full_match:
for sig, func in sig_list: # sig = (Signature,func)
for param in sig.parameters: # param = {'a':int,'b':int}
if check_signature(param, values_dict):
return func

return None
0