This repository was archived by the owner on Feb 2, 2024. It is now read-only.
Repository navigation
automatic generation of type checks for overload #911
Open
vlad-perevezentsev
wants to merge
12
commits into
IntelPython:numba_typing
Choose a base branch
from
vlad-perevezentsev:numba_typing
base: numba_typing
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8bd7bc0
add automatic generation of type checks for overload_list
vlad-perevezentsev d3f4a5d
add exception handling to the match function and correct handling of …
vlad-perevezentsev b54da17
add unittests
vlad-perevezentsev 05c745b
add default value processing and tests
vlad-perevezentsev b7446ca
fix comments
vlad-perevezentsev 09289bf
fix comments and add new tests
vlad-perevezentsev 1cb60da
remove full_match and add a signature matching check
vlad-perevezentsev 967ae29
add a test cases generator function
vlad-perevezentsev 716402c
fix comments and change the old tests
vlad-perevezentsev 2096e94
fix PEP
vlad-perevezentsev 5ec33ac
fix PEP
vlad-perevezentsev 7da564b
add additional tests
vlad-perevezentsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix comments and add new tests
- Loading branch information
commit 09289bfb1acd0d5d25d525596c1b4ab866f1707d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,6 @@ | |
| from numba.extending import overload | ||
| from type_annotations import product_annotations, get_func_annotations | ||
| import typing | ||
| from numba import NumbaDeprecationWarning, NumbaPendingDeprecationWarning | ||
| import warnings | ||
| from numba.typed import List, Dict | ||
| from inspect import getfullargspec | ||
|
|
||
|
|
@@ -26,7 +24,7 @@ def wrapper(*args): | |
| result = choose_func_by_sig(sig_list, values_dict, defaults_dict) | ||
|
|
||
| if result is None: | ||
| raise numba.TypingError(f'Unsupported types a={a}, b={b}') | ||
| raise TypeError(f'Unsupported types a={a}, b={b}') | ||
|
|
||
| return result | ||
|
|
||
|
|
@@ -52,28 +50,27 @@ def check_str_type(n_type): | |
|
|
||
|
|
||
| def check_list_type(self, p_type, n_type): | ||
| res = isinstance(n_type, types.List) or isinstance(n_type, types.ListType) | ||
| if isinstance(p_type, type): | ||
| res = isinstance(n_type, (types.List, types.ListType)) | ||
| if p_type == list: | ||
| return res | ||
| else: | ||
| return res and self.match(p_type.__args__[0], n_type.dtype) | ||
|
|
||
|
|
||
| def check_tuple_type(self, p_type, n_type): | ||
| res = False | ||
| if isinstance(n_type, types.Tuple): | ||
| res = True | ||
| if isinstance(p_type, type): | ||
| return res | ||
| for p_val, n_val in zip(p_type.__args__, n_type.key): | ||
| res = res and self.match(p_val, n_val) | ||
| if isinstance(n_type, types.UniTuple): | ||
| res = True | ||
| if isinstance(p_type, type): | ||
| return res | ||
| for p_val in p_type.__args__: | ||
| res = res and self.match(p_val, n_type.key[0]) | ||
| return res | ||
| if not isinstance(n_type, (types.Tuple, types.UniTuple)): | ||
| return False | ||
| try: | ||
| if len(p_type.__args__) != len(n_type.types): | ||
| return False | ||
| except AttributeError: # if p_type == tuple | ||
| return True | ||
|
|
||
| for p_val, n_val in zip(p_type.__args__, n_type.types): | ||
| if not self.match(p_val, n_val): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def check_dict_type(self, p_type, n_type): | ||
|
|
@@ -89,9 +86,7 @@ def check_dict_type(self, p_type, n_type): | |
|
|
||
| class TypeChecker: | ||
|
|
||
| _types_dict = {int: check_int_type, float: check_float_type, bool: check_bool_type, | ||
| str: check_str_type, list: check_list_type, | ||
| tuple: check_tuple_type, dict: check_dict_type} | ||
| _types_dict: dict = {} | ||
|
|
||
| def __init__(self): | ||
| self._typevars_dict = {} | ||
|
|
@@ -118,60 +113,65 @@ def _get_origin(p_obj): | |
| return p_obj.__origin__ | ||
|
|
||
| def match(self, p_type, n_type): | ||
| if p_type == typing.Any: | ||
| return True | ||
| try: | ||
| if p_type == typing.Any: | ||
| return True | ||
| elif self._is_generic(p_type): | ||
| if self._is_generic(p_type): | ||
| origin_type = self._get_origin(p_type) | ||
| if origin_type == typing.Generic: | ||
| return self.match_generic(p_type, n_type) | ||
| else: | ||
| return self._types_dict[origin_type](self, p_type, n_type) | ||
| elif isinstance(p_type, typing.TypeVar): | ||
|
|
||
| return self._types_dict[origin_type](self, p_type, n_type) | ||
|
|
||
| if isinstance(p_type, typing.TypeVar): | ||
| return self.match_typevar(p_type, n_type) | ||
| else: | ||
| if p_type in (list, tuple): | ||
| return self._types_dict[p_type](self, p_type, n_type) | ||
| return self._types_dict[p_type](n_type) | ||
|
|
||
| if p_type in (list, tuple, dict): | ||
| return self._types_dict[p_type](self, p_type, n_type) | ||
|
|
||
| return self._types_dict[p_type](n_type) | ||
|
|
||
| except KeyError: | ||
| print((f'A check for the {p_type} was not found.')) | ||
| return None | ||
| raise TypeError(f'A check for the {p_type} was not found.') | ||
|
|
||
| def match_typevar(self, p_type, n_type): | ||
| if not self._typevars_dict.get(p_type) and n_type not in self._typevars_dict.values(): | ||
| if isinstance(n_type, types.List): | ||
| n_type = types.ListType(n_type.dtype) | ||
| if not self._typevars_dict.get(p_type): | ||
| self._typevars_dict[p_type] = n_type | ||
| return True | ||
| return self._typevars_dict.get(p_type) == n_type | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it should be |
||
|
|
||
| def match_generic(self, p_type, n_type): | ||
| res = True | ||
| for arg in p_type.__args__: | ||
| res = res and self.match(arg, n_type) | ||
| return res | ||
| raise SystemError | ||
|
|
||
|
|
||
| TypeChecker.add_type_check(int, check_int_type) | ||
| TypeChecker.add_type_check(float, check_float_type) | ||
| TypeChecker.add_type_check(str, check_str_type) | ||
| TypeChecker.add_type_check(bool, check_bool_type) | ||
| TypeChecker.add_type_check(list, check_list_type) | ||
| TypeChecker.add_type_check(tuple, check_tuple_type) | ||
| TypeChecker.add_type_check(dict, check_dict_type) | ||
|
|
||
|
|
||
| def choose_func_by_sig(sig_list, values_dict, defaults_dict): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's do it this way: def choose_func_by_sig(sig_list, values_dict, defaults_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
return True
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 |
||
| checker = TypeChecker() | ||
| for sig, func in sig_list: # sig = (Signature,func) | ||
| for param in sig.parameters: # param = {'a':int,'b':int} | ||
| full_match = True | ||
| for name, typ in values_dict.items(): # name,type = 'a',int64 | ||
| if isinstance(typ, types.Literal): | ||
| typ = typ.literal_type | ||
|
|
||
| full_match = full_match and checker.match( | ||
| param[name], typ.literal_type) | ||
|
|
||
| if sig.defaults.get(name, False): | ||
| full_match = full_match and sig.defaults[name] == typ.literal_value | ||
| else: | ||
| full_match = full_match and checker.match(param[name], typ) | ||
| full_match = checker.match(param[name], typ) | ||
|
|
||
| if not full_match: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really need this |
||
| break | ||
|
|
||
| for name, val in defaults_dict.items(): | ||
| if not sig.defaults.get(name) is None: | ||
| full_match = full_match and sig.defaults[name] == val | ||
| 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 | ||
|
|
||
| checker.clear_typevars_dict() | ||
| if full_match: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks both
aandbare undefined.