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

Restore numba.types as public API #5676

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions numba/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import weakref
import warnings
from types import ModuleType
from importlib import import_module
from collections.abc import Mapping
import numpy as np

Expand Down Expand Up @@ -555,3 +556,38 @@ def unified_function_type(numba_types, require_precise=True):
function = types.UndefinedFunctionType(mnargs, dispatchers)

return function


class _RedirectSubpackage(ModuleType):
"""Redirect a subpackage to a subpackage.

This allows all references like:

>>> from numba.old_subpackage import module
>>> module.item

>>> import numba.old_subpackage.module
>>> numba.old_subpackage.module.item

>>> from numba.old_subpackage.module import item
"""
def __init__(self, old_module_locals, new_module):
old_module = old_module_locals['__name__']
super().__init__(old_module)

new_mod_obj = import_module(new_module)

# Map all sub-modules over
for k, v in new_mod_obj.__dict__.items():
# Get attributes so that `subpackage.xyz` and
# `from subpackage import xyz` work
setattr(self, k, v)
if isinstance(v, ModuleType):
# Map modules into the interpreter so that
# `import subpackage.xyz` works
sys.modules[f"{old_module}.{k}"] = sys.modules[v.__name__]

# copy across dunders so that package imports work too
for attr, value in old_module_locals.items():
if attr.startswith('__') and attr.endswith('__'):
setattr(self, attr, value)
30 changes: 30 additions & 0 deletions numba/tests/test_moved_modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Tests for moved modules and their redirection from old path
"""
from numba.tests.support import TestCase


class TestMovedModule(TestCase):
"""Testing moved modules in Q1 2020 but were decided to kept as public API
"""
def tests_numba_types(self):
import numba.types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should e.g. from numba.types import containers be explicitly tested too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can add a test. In general, I am only testing a few things because I am already checking for object identity. I don't think that's any reason for it to not work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test added in 2998661

import numba.core.types as types
# The old module IS NOT the new module
self.assertIsNot(numba.types, types)
# Attribute access are there
self.assertIs(numba.types.intp, types.intp)
self.assertIs(numba.types.float64, types.float64)
self.assertIs(numba.types.Array, types.Array)
# Submodule access through old import path is possible
import numba.types.misc
self.assertIs(types.misc, numba.types.misc)
self.assertIs(types.misc.Optional, numba.types.misc.Optional)
# Import time code could be executed twice and causes the following to
# fail.
self.assertIs(types.StringLiteral, numba.types.misc.StringLiteral)
# Check numba.types.container
from numba.types import containers
self.assertIs(types.containers, containers)
self.assertIs(types.containers.Sequence, containers.Sequence)
from numba.types.containers import Sequence
self.assertIs(Sequence, containers.Sequence)
4 changes: 4 additions & 0 deletions numba/types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys
from numba.core.utils import _RedirectSubpackage
sys.modules[__name__] = _RedirectSubpackage(locals(), "numba.core.types")