question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Overload not properly producing omitted types

See original GitHub issue

Reporting a bug

  • I have tried using the latest released version of Numba (most recent is visible in the change log (https://github.com/numba/numba/blob/main/CHANGE_LOG).
  • I have included a self contained code sample to reproduce the problem. i.e. it’s possible to run as ‘python bug.py’.

It seems like the compilation path for overloads doesn’t properly do argument folding and therefore doesn’t produce omitted types. Here is a reproducer:

from numba.extending import overload
from numba import njit

def foo(a, b=1):
	pass

@overload(foo)
def foo_overload(a, b=1):
	print(a, b)
	def foo_impl(a, b=1):
		return a + b
        return foo_impl


@njit
def g(a):
	return foo(a)

g(100)

On master this outputs:

int64 1

So b is a python literal 1, not an omitted type.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
njriasancommented, May 12, 2022

This seems to be possible to convert to a literal type if you reuse the argument folding used by dispatchers. Here is a diff that outputs int64 omitted(default=1)

diff --git a/numba/core/typing/templates.py b/numba/core/typing/templates.py
index 222be0c61..6ddd61a0d 100644
--- a/numba/core/typing/templates.py
+++ b/numba/core/typing/templates.py
@@ -790,7 +790,21 @@ class _OverloadFunctionTemplate(AbstractTemplate):
             # problems
             raise TypingError(str(e)) from e
         else:
-            ovf_result = self._overload_func(*args, **kws)
+            def normal_handler(index, param, value):
+                return value
+
+            def default_handler(index, param, default):
+                return types.Omitted(default)
+
+            def stararg_handler(index, param, values):
+                return types.StarArgTuple(values)
+
+            pysig = utils.pysignature(self._overload_func)
+            args = fold_arguments(pysig, args, kws,
+                                normal_handler,
+                                default_handler,
+                                stararg_handler)
+            ovf_result = self._overload_func(*args)
 
         if ovf_result is None:
             # No implementation => fail typing

Of course the python signature should probably be computed only once. Is there any reason this isn’t an omitted type right now? It seems like if there was ever a default value that was unhashable then it wouldn’t be possible to cache the overload.

0reactions
stuartarchibaldcommented, May 17, 2022

@njriasan as noted in the Numba meeting, the follow doesn’t actually solve any of this but might help some use cases:

from numba.extending import overload, types
from numba import njit

def foo(a, b=1):
        pass

@overload(foo, strict=False)
def foo_overload(a, b=types.Omitted(types.literal(1))):
    print(a, b)
    def foo_impl(a, b=1):
            return a + b
    return foo_impl


@njit
def g(a):
        return foo(a)

g(100)
Read more comments on GitHub >

github_iconTop Results From Across the Web

8.11 — Function overload resolution and ambiguous matches
If an overloaded function is not properly differentiated from the other overloads of the same name, then the compiler will issue a compile ......
Read more >
Wrong generic overload function called - Stack Overflow
The type of the method to be called is selected during compile time. What does the compiler know about your types?
Read more >
select overload not match when enable strict mode in tsconfig
When I turn off strictFunctionTypes everything work well. ... to select the state with selector have created it appear overload not match.
Read more >
Overload resolution - cppreference.com
If at least one of the arguments to an operator in an expression has a class type or an enumeration type, both builtin...
Read more >
Operator Overloading, C++ FAQ - Standard C++
What operators can/cannot be overloaded? Why can't I overload . (dot), :: , sizeof , etc.? Can I define my ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found