Repository: numba/numba

Issue:
named parameters not recognized with first class functions
- [x ] I am using the latest ~~released~~ **master** version of Numba (most recent is visible in
 the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x ] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).


There is a problem recognizing named parameters. The following fails

```python
from numba import njit, types

@njit(types.int64(types.int64))
def foo(x):
    return x+1

@njit
def loop(foos):
    f = foos[0]
    return f(x=1)

loop((foo,))

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.core.typeinfer.CallConstraint object at 0x7f67f39a1a30>.
mismatch of arguments number: 0 vs 1
[1] During: resolving callee type: UndefinedFunctionType[undefined(undefined)20]
```

while this works

```python
@njit
def loop(foos):
    f = foos[0]
    return f(1)

loop((foo,))
```

Discussion:

Patch:
diff --git a/numba/core/types/function_type.py b/numba/core/types/function_type.py
index 576a8cbd9..34a14e41d 100644
--- a/numba/core/types/function_type.py
+++ b/numba/core/types/function_type.py
@@ -4,7 +4,7 @@ __all__ = ['FunctionType', 'UndefinedFunctionType', 'FunctionPrototype',
 
 from abc import ABC, abstractmethod
 from .abstract import Type
-from .. import types
+from .. import types, errors
 
 
 class FunctionType(Type):
@@ -43,6 +43,17 @@ class FunctionType(Type):
     def get_call_type(self, context, args, kws):
         from numba.core import typing
 
+        if kws:
+            # First-class functions carry only the type signature
+            # information and function address value. So, it is not
+            # possible to determine the positional arguments
+            # corresponding to the keyword arguments in the call
+            # expression. For instance, the definition of the
+            # first-class function may not use the same argument names
+            # that the caller assumes. [numba/issues/5540].
+            raise errors.UnsupportedError(
+                f'first-class function call cannot use keyword arguments')
+
         if len(args) != self.nargs:
             raise ValueError(
                 f'mismatch of arguments number: {len(args)} vs {self.nargs}')


Test patch:
diff --git a/numba/tests/test_function_type.py b/numba/tests/test_function_type.py
index a9739100a..e48590b18 100644
--- a/numba/tests/test_function_type.py
+++ b/numba/tests/test_function_type.py
@@ -1028,3 +1028,28 @@ class TestMiscIssues(TestCase):
             return bar_scalar(*formulae_foo)
 
         self.assertEqual(bar(), 30)
+
+    def test_issue_5540(self):
+
+        @njit(types.int64(types.int64))
+        def foo(x):
+            return x + 1
+
+        @njit
+        def bar_bad(foos):
+            f = foos[0]
+            return f(x=1)
+
+        @njit
+        def bar_good(foos):
+            f = foos[0]
+            return f(1)
+
+        self.assertEqual(bar_good((foo, )), 2)
+
+        with self.assertRaises(errors.TypingError) as cm:
+            bar_bad((foo, ))
+
+        self.assertRegex(
+            str(cm.exception),
+            r'.*first-class function call cannot use keyword arguments')