def visit_ImportFrom(self, node: ast.ImportFrom): warnings.warn(f'Detected local import in {obj.__module__}.{obj.__name__}') if node.level == 0: mod = import_module(node.module) else: mod = import_module('.' + node.module, get_object_module(obj).__package__) pickler._add_requirement(mod)
def test_extension_loader__lazy(ext_loader, two_temp_modules): module1, module2 = two_temp_modules ext_loader.builtin_extensions[module1] = Extension(module1, [module2], force=False) ext_loader.load_all() assert not module_imported(module1) assert not module_imported(module2) import_module(module2) # noqa assert module_imported(module1) assert module_imported(module2)
def test_extension_loader__lazy_defered(ext_loader, two_temp_modules, temp_module_factory): module1, module2 = two_temp_modules ext_loader.builtin_extensions[module1] = Extension(module1, [module2], force=False) ext_loader.load_all() assert not module_imported(module1) assert not module_imported(module2) module3 = temp_module_factory(f'import {module2}') import_module(module3) # noqa assert module_imported(module1) assert module_imported(module2)
def test_module_representation(): from setup import setup_args for module in setup_args['install_requires']: mod_name = module.split('==')[0] try: mod = import_module(mod_name) assert module == get_module_repr(mod) except (ImportError, NameError): continue
def load(cls, extension: Union[str, Extension]): """ Load single extension :param extension: str of :class:`Extension` instance to load """ if isinstance(extension, str): extension = Extension(extension, [], force=True) if extension not in cls._loaded_extensions and not module_imported(extension.module): logger.debug('Importing extension module %s', extension.module) cls._loaded_extensions[extension] = import_module(extension.module)
def analyze_module_imports(module_path): module = importing.import_module(module_path) requirements = set() for name, obj in module.__dict__.items(): if isinstance(obj, ModuleType): mod = obj else: mod = get_object_base_module(obj) requirements.add(get_module_repr(mod)) return requirements
def get_local_module_reqs(mod): tree = ast.parse(inspect.getsource(mod)) imports = [] for statement in tree.body: if isinstance(statement, ast.Import): imports += [(n.name, None) for n in statement.names] elif isinstance(statement, ast.ImportFrom): if statement.level == 0: imp = (statement.module, None) else: imp = ('.' + statement.module, mod.__package__) imports.append(imp) result = [import_module(i, p) for i, p in imports] if mod.__file__.endswith('__init__.py'): # add loaded subpackages prefix = mod.__name__ + '.' result += [mod for name, mod in sys.modules.items() if name.startswith(prefix)] return result
def test_module_version(): # we do not check for concrete version as they could differ assert get_module_version(import_module('numpy')) is not None assert get_module_version(import_module('dill')) is not None # responses doesn't have __version__ attr, thus heuristics should be applied here assert get_module_version(import_module('responses')) is not None