-
Notifications
You must be signed in to change notification settings - Fork 62
automatic generation of type checks for overload #911
automatic generation of type checks for overload #911
Conversation
Hello @vlad-perevezentsev! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻
Comment last updated at 2020年09月29日 14:36:35 UTC
numba_typing/overload_list.py
Outdated
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.
So we will (implicitly) return None from match in this case, is this intended behavior? Please add explicit return or throw
numba_typing/overload_list.py
Outdated
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.
Instead of accessing sig[0] and sig[1] each time you can write for sig, func in sig_list:
numba_typing/overload_list.py
Outdated
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.
There are some unused imports here, like numpy, typeof and njit, please clean them up.
numba_typing/overload_list.py
Outdated
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.
Why is this needed?
numba_typing/overload_list.py
Outdated
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.
Instead of using n_type.key you could use n_type.types which is defined for both Tuple and UniTuple, so you can unite both branches:
Something like:
if not isinstance(n_type, types.Tuple, types.UniTuple): return False 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
And btw you need to check that size of p_type.__args__ and n_type.types are the same.
And I believe you don't have tests for the case when they are different.
numba_typing/overload_list.py
Outdated
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.
We should rise an exception
numba_typing/overload_list.py
Outdated
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.
Let's do it like this:
if p_type == typing.Any: return True 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) 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) 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)
numba_typing/overload_list.py
Outdated
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.
Why do you need condition n_type not in self._typevars_dict.values() ?
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.
I believe it should be self.match. E.g. list and 'types.List' are synonyms but will fail equality check ('list != types.List').
And I'm assuming you don't have such tests?
numba_typing/overload_list.py
Outdated
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.
What's doesn't feel right. Do we have any test for this case?
numba_typing/overload_list.py
Outdated
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.
Do we need this else?
numba_typing/overload_list.py
Outdated
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.
res = isinstance(n_type, (types.List, types.ListType))
numba_typing/overload_list.py
Outdated
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.
isinstance(p_type, (list, typing.List))
?
numba_typing/overload_list.py
Outdated
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.
Shouldn't dict be here too?
numba_typing/overload_list.py
Outdated
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.
I'd prefer this checks to be added using add_type_check function
AlexanderKalistratov
commented
Sep 13, 2020
This code actually modifies original function globals. This shouldn't happen, please fix.
numba_typing/overload_list.py
Outdated
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.
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
numba_typing/test_overload_list.py
Outdated
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.
please use f'{key}' instead. Here and everywhere
numba_typing/test_overload_list.py
Outdated
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.
{'a': int, 'b': int} why do you need two parameters of the same type?
What are you actually testing?
numba_typing/test_overload_list.py
Outdated
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.
Why do you need second list parameter?
numba_typing/test_overload_list.py
Outdated
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.
And why you are not testing second Union parameter?
numba_typing/test_overload_list.py
Outdated
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.
And what about both - annotation and default value?
numba_typing/test_overload_list.py
Outdated
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.
Are where any tests for TypeVar with specified types restriction?
numba_typing/test_overload_list.py
Outdated
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.
Any negative tests for such case?
numba_typing/test_overload_list.py
Outdated
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.
I'd prefer you to group all cases into 4-5 tests with subtests. Something like this:
def test_common_types(): test_cases = [({'a': 0}, {'a': int}), ({'a': 0.}, {'a': float}), ...] for case in test_cases: with self.Subtest(case=case): run_test(case)
numba_typing/overload_list.py
Outdated
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 a and b are undefined.
numba_typing/overload_list.py
Outdated
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.
You don't need to call method items() here.
numba_typing/overload_list.py
Outdated
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.
This condition looks pretty strange. Maybe if sig_def[name] != val?
No description provided.