Skip to content

Commit

Permalink
Add type annotations for @intrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
aneeshdurg committed Jan 23, 2024
1 parent fed996d commit 8465419
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
warn_unused_configs = True
follow_imports = silent
show_error_context = True
files = **/numba/core/types/*.py, **/numba/core/datamodel/*.py, **/numba/core/rewrites/*.py, **/numba/core/unsafe/*.py, **/numba/core/rvsdg_frontend/*.py, **/numba/core/rvsdg_frontend/rvsdg/*.py
files = **/numba/core/types/*.py, **/numba/core/extending.py, **/numba/core/datamodel/*.py, **/numba/core/rewrites/*.py, **/numba/core/unsafe/*.py, **/numba/core/rvsdg_frontend/*.py, **/numba/core/rvsdg_frontend/rvsdg/*.py

# Per-module options:
# To classify a given module as Level 1, 2 or 3 it must be added both in files (variable above) and in the lists below.
Expand Down
2 changes: 2 additions & 0 deletions numba/_helperlib.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def _import_cython_function(module_name: str, function_name: str):
...
24 changes: 16 additions & 8 deletions numba/core/extending.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import collections
import functools

from typing import Callable, Concatenate, Generic, ParamSpec, TypeVar

import numba
from numba.core import types, errors, utils, config

Expand Down Expand Up @@ -311,18 +313,24 @@ def struct_getattr_impl(context, builder, typ, val):
return impl_ret_borrowed(context, builder, attrty, attrval)


class _Intrinsic(ReduceMixin):
# Type of the parameters of the function being converted to an intrinsic,
# excluding the typing context parameter.
P = ParamSpec("P")
# Return type of the function being converted to an intrinsic
R = TypeVar("R")
class _Intrinsic(ReduceMixin, Generic[P, R]):
"""
Dummy callable for intrinsic
"""
_memo = weakref.WeakValueDictionary()
_memo: weakref.WeakValueDictionary = weakref.WeakValueDictionary()
__cache_size = config.FUNCTION_CACHE_SIZE # type: ignore
# hold refs to last N functions deserialized, retaining them in _memo
# regardless of whether there is another reference
_recent = collections.deque(maxlen=config.FUNCTION_CACHE_SIZE)
_recent: collections.deque = collections.deque(maxlen=__cache_size)

__uuid = None

def __init__(self, name, defn, prefer_literal=False, **kwargs):
def __init__(self, name, defn: Callable[Concatenate[object, P], R], prefer_literal=False, **kwargs):
self._ctor_kwargs = kwargs
self._name = name
self._defn = defn
Expand Down Expand Up @@ -360,7 +368,7 @@ def _register(self):
infer(template)
infer_global(self, types.Function(template))

def __call__(self, *args, **kwargs):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
"""
This is only defined to pretend to be a callable from CPython.
"""
Expand Down Expand Up @@ -395,7 +403,7 @@ def _rebuild(cls, uuid, name, defn):
return llc


def intrinsic(*args, **kwargs):
def intrinsic(*args, **kwargs) -> Callable[[Callable[Concatenate[object, P], R]], Callable[P, R]]:
"""
A decorator marking the decorated function as typing and implementing
*func* in nopython mode using the llvmlite IRBuilder API. This is an escape
Expand Down Expand Up @@ -432,7 +440,7 @@ def codegen(context, builder, signature, args):
return sig, codegen
"""
# Make inner function for the actual work
def _intrinsic(func):
def _intrinsic(func: Callable[Concatenate[object, P], R]) -> Callable[P, R]:
name = getattr(func, '__name__', str(func))
llc = _Intrinsic(name, func, **kwargs)
llc._register()
Expand All @@ -444,7 +452,7 @@ def _intrinsic(func):
else:
# options are given, create a new callable to recv the
# definition function
def wrapper(func):
def wrapper(func: Callable[Concatenate[object, P], R]) -> Callable[P, R]:
return _intrinsic(func)
return wrapper

Expand Down

0 comments on commit 8465419

Please sign in to comment.