Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'CPUDispatcher' object has no attribute '__globals__' for numba.njitted function and typeguard test #1271

Open
gatoniel opened this issue Nov 8, 2022 · 0 comments

Comments

@gatoniel
Copy link

gatoniel commented Nov 8, 2022

Hi,
I am using the numba.njit decorator to decorate a function. When I execute the typeguard nox session I get an error when this decorated function is called:

    def test_numba_sum_njit() -> None:
        x = np.arange(4)
>       s = numba_sum(x)

tests/test_numba_typeguard.py:8:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.nox/typeguard-3-9/lib/python3.9/site-packages/typeguard/__init__.py:1031: in wrapper
    memo = _CallMemo(python_func, _localns, args=args, kwargs=kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <typeguard._CallMemo object at 0x7fefc16be880>, func = CPUDispatcher(<function numba_sum at 0x7fefc17815e0>)
frame_locals = {'T': ~T, 'TypeVar': <class 'typing.TypeVar'>, '__builtins__': {'ArithmeticError': <class 'ArithmeticError'>, 'Asserti...s/.nox/typeguard-3-9/lib/python3.9/site-packages/my_module/__pycache__/numba_typeguard.cpython-39.pyc', ...}
args = (array([0, 1, 2, 3]),), kwargs = {}, forward_refs_policy = <ForwardRefPolicy.ERROR: 1>

    def __init__(self, func: Callable, frame_locals: Optional[Dict[str, Any]] = None,
                 args: tuple = None, kwargs: Dict[str, Any] = None,
                 forward_refs_policy=ForwardRefPolicy.ERROR):
>       super().__init__(func.__globals__, frame_locals)
E       AttributeError: 'CPUDispatcher' object has no attribute '__globals__'

.nox/typeguard-3-9/lib/python3.9/site-packages/typeguard/__init__.py:191: AttributeError

In order to narrow down the error I realized that this error is only raised when I import the function via from my_module.numba_typeguard import numba_sum. When I import that exactly same function via from .numba_typeguard import numba_sum_local the error is not there. Here are the three files I am using:

src/my_module/numba_typeguard.py

from typing import TypeVar

import numpy as np
import numpy.typing as npt
from numba import njit

T = TypeVar("T", bound=np.generic)


@njit
def numba_sum(
    array: npt.NDArray[T],
) -> T:
    return np.sum(array)

tests/numba_typeguard.py same function as above but in tests directory.

from typing import TypeVar

import numpy as np
import numpy.typing as npt
from numba import njit

T = TypeVar("T", bound=np.generic)


@njit
def numba_sum_local(
    array: npt.NDArray[T],
) -> T:
    return np.sum(array)

tests/test_numba_typeguard.py

import numpy as np
from my_module.numba_typeguard import numba_sum
from .numba_typeguard import numba_sum_local


def test_numba_sum_njit() -> None:
    x = np.arange(4)
    s = numba_sum(x)
    assert s == np.sum(x)


def test_numba_sum() -> None:
    x = np.arange(4)
    s = numba_sum.py_func(x)
    assert s == np.sum(x)


def test_numba_sum_local_njit() -> None:
    x = np.arange(4)
    s = numba_sum_local(x)
    assert s == np.sum(x)


def test_numba_sum_local() -> None:
    x = np.arange(4)
    s = numba_sum_local.py_func(x)
    assert s == np.sum(x)

Only the test function test_numba_sum_njit fails with the above error. Is there any way to avoid that error? Somehow it is related to the fact that the one function is imported via an absolute import while the other one is imported relatively.

Is there a way to disbale / ignore errors in typeguard with a comment as is possible for mypy / flake8?

I raised also an issue in the typeguard repo: agronholm/typeguard#277

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant