def new_get_ast(filepath, modname): ast = old_get_ast(filepath, modname) with open(filepath) as f: source_code = f.readlines() ending_transformer = TransformVisitor() register_transforms(source_code, ending_transformer) ending_transformer.visit(ast) return ast
def type_inference_transformer(self) -> TransformVisitor: """Instantiate a visitor to perform type inference on an AST. """ type_visitor = TransformVisitor() for klass in astroid.ALL_NODE_CLASSES: if hasattr(self, f'visit_{klass.__name__.lower()}'): type_visitor.register_transform(klass, getattr(self, f'visit_{klass.__name__.lower()}')) return type_visitor
def new_get_ast(self, filepath, modname): ast = old_get_ast(self, filepath, modname) if ast is not None: with open(filepath, encoding='utf-8') as f: source_code = f.readlines() ending_transformer = TransformVisitor() register_transforms(source_code, ending_transformer) ending_transformer.visit(ast) return ast
def type_inference_transformer(self) -> TransformVisitor: """Instantiate a visitor to perform type inference on an AST. """ type_visitor = TransformVisitor() for klass in astroid.ALL_NODE_CLASSES: if hasattr(self, f'visit_{klass.__name__.lower()}'): type_visitor.register_transform( klass, getattr(self, f'visit_{klass.__name__.lower()}')) else: type_visitor.register_transform(klass, self.visit_default) return type_visitor
def environment_transformer(self) -> TransformVisitor: """Return a TransformVisitor that sets an environment for every node.""" visitor = TransformVisitor() visitor.register_transform(astroid.FunctionDef, self._set_function_def_environment) visitor.register_transform(astroid.ClassDef, self._set_classdef_environment) visitor.register_transform(astroid.Module, self._set_module_environment) visitor.register_transform(astroid.ListComp, self._set_listcomp_environment) visitor.register_transform(astroid.DictComp, self._set_dictcomp_environment) visitor.register_transform(astroid.SetComp, self._set_setcomp_environment) return visitor
def init_register_ending_setters(source_code): """Instantiate a visitor to transform the nodes. Register the transform functions on an instance of TransformVisitor. @type source_code: list of strings @rtype: TransformVisitor """ ending_transformer = TransformVisitor() # Check consistency of astroid-provided fromlineno and col_offset attributes. for node_class in astroid.ALL_NODE_CLASSES: ending_transformer.register_transform( node_class, fix_start_attributes, lambda node: node.fromlineno is None or node.col_offset is None) # Ad hoc transformations ending_transformer.register_transform(astroid.Tuple, _set_start_from_first_child) ending_transformer.register_transform(astroid.Arguments, fix_start_attributes) ending_transformer.register_transform(astroid.Arguments, set_arguments) ending_transformer.register_transform(astroid.Slice, fix_slice(source_code)) for node_class in NODES_WITHOUT_CHILDREN: ending_transformer.register_transform(node_class, set_without_children) for node_class in NODES_WITH_CHILDREN: ending_transformer.register_transform(node_class, set_from_last_child) # Nodes where the source code must also be provided. # source_code and the predicate functions get stored in the TransformVisitor for node_class, start_pred, end_pred in NODES_REQUIRING_SOURCE: if start_pred is not None: ending_transformer.register_transform( node_class, start_setter_from_source(source_code, start_pred)) if end_pred is not None: # This is for searching for a trailing comma after a tuple's final element if node_class is astroid.Tuple: ending_transformer.register_transform( node_class, end_setter_from_source(source_code, end_pred, True)) else: ending_transformer.register_transform( node_class, end_setter_from_source(source_code, end_pred)) # Nodes where extra parentheses are included ending_transformer.register_transform(astroid.Const, add_parens_to_const(source_code)) ending_transformer.register_transform(astroid.Tuple, add_parens_to_const(source_code)) return ending_transformer
def environment_transformer(self) -> TransformVisitor: """Return a TransformVisitor that sets an environment for every node.""" visitor = TransformVisitor() visitor.register_transform(astroid.FunctionDef, self._set_function_def_environment) visitor.register_transform(astroid.AsyncFunctionDef, self._set_function_def_environment) visitor.register_transform(astroid.ClassDef, self._set_classdef_environment) visitor.register_transform(astroid.Module, self._set_module_environment) visitor.register_transform(astroid.ListComp, self._set_comprehension_environment) visitor.register_transform(astroid.DictComp, self._set_comprehension_environment) visitor.register_transform(astroid.SetComp, self._set_comprehension_environment) visitor.register_transform(astroid.GeneratorExp, self._set_comprehension_environment) visitor.register_transform(astroid.Lambda, self._set_comprehension_environment) return visitor
class AstroidManager: """Responsible to build astroid from files or modules. Use the Borg (singleton) pattern. """ name = "astroid loader" brain = {} max_inferable_values: ClassVar[int] = 100 def __init__(self): self.__dict__ = AstroidManager.brain if not self.__dict__: # NOTE: cache entries are added by the [re]builder self.astroid_cache = {} self._mod_file_cache = {} self._failed_import_hooks = [] self.always_load_extensions = False self.optimize_ast = False self.extension_package_whitelist = set() self._transform = TransformVisitor() @property def register_transform(self): # This and unregister_transform below are exported for convenience return self._transform.register_transform @property def unregister_transform(self): return self._transform.unregister_transform @property def builtins_module(self): return self.astroid_cache["builtins"] def visit_transforms(self, node): """Visit the transforms and apply them to the given *node*.""" return self._transform.visit(node) def ast_from_file(self, filepath, modname=None, fallback=True, source=False): """given a module name, return the astroid object""" try: filepath = get_source_file(filepath, include_no_ext=True) source = True except NoSourceFile: pass if modname is None: try: modname = ".".join(modpath_from_file(filepath)) except ImportError: modname = filepath if (modname in self.astroid_cache and self.astroid_cache[modname].file == filepath): return self.astroid_cache[modname] if source: # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder return AstroidBuilder(self).file_build(filepath, modname) if fallback and modname: return self.ast_from_module_name(modname) raise AstroidBuildingError("Unable to build an AST for {path}.", path=filepath) def ast_from_string(self, data, modname="", filepath=None): """Given some source code as a string, return its corresponding astroid object""" # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder return AstroidBuilder(self).string_build(data, modname, filepath) def _build_stub_module(self, modname): # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder return AstroidBuilder(self).string_build("", modname) def _build_namespace_module(self, modname, path): # pylint: disable=import-outside-toplevel; circular import from astroid.builder import build_namespace_package_module return build_namespace_package_module(modname, path) def _can_load_extension(self, modname): if self.always_load_extensions: return True if is_standard_module(modname): return True parts = modname.split(".") return any(".".join(parts[:x]) in self.extension_package_whitelist for x in range(1, len(parts) + 1)) def ast_from_module_name(self, modname, context_file=None): """given a module name, return the astroid object""" if modname in self.astroid_cache: return self.astroid_cache[modname] if modname == "__main__": return self._build_stub_module(modname) if context_file: old_cwd = os.getcwd() os.chdir(os.path.dirname(context_file)) try: found_spec = self.file_from_module_name(modname, context_file) if found_spec.type == spec.ModuleType.PY_ZIPMODULE: module = self.zip_import_data(found_spec.location) if module is not None: return module elif found_spec.type in ( spec.ModuleType.C_BUILTIN, spec.ModuleType.C_EXTENSION, ): if (found_spec.type == spec.ModuleType.C_EXTENSION and not self._can_load_extension(modname)): return self._build_stub_module(modname) try: module = load_module_from_name(modname) except Exception as e: raise AstroidImportError( "Loading {modname} failed with:\n{error}", modname=modname, path=found_spec.location, ) from e return self.ast_from_module(module, modname) elif found_spec.type == spec.ModuleType.PY_COMPILED: raise AstroidImportError( "Unable to load compiled module {modname}.", modname=modname, path=found_spec.location, ) elif found_spec.type == spec.ModuleType.PY_NAMESPACE: return self._build_namespace_module( modname, found_spec.submodule_search_locations) elif found_spec.type == spec.ModuleType.PY_FROZEN: return self._build_stub_module(modname) if found_spec.location is None: raise AstroidImportError( "Can't find a file for module {modname}.", modname=modname) return self.ast_from_file(found_spec.location, modname, fallback=False) except AstroidBuildingError as e: for hook in self._failed_import_hooks: try: return hook(modname) except AstroidBuildingError: pass raise e finally: if context_file: os.chdir(old_cwd) def zip_import_data(self, filepath): if zipimport is None: return None # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder builder = AstroidBuilder(self) for ext in ZIP_IMPORT_EXTS: try: eggpath, resource = filepath.rsplit(ext + os.path.sep, 1) except ValueError: continue try: # pylint: disable=no-member importer = zipimport.zipimporter(eggpath + ext) # pylint: enable=no-member zmodname = resource.replace(os.path.sep, ".") if importer.is_package(resource): zmodname = zmodname + ".__init__" module = builder.string_build(importer.get_source(resource), zmodname, filepath) return module except Exception: # pylint: disable=broad-except continue return None def file_from_module_name(self, modname, contextfile): try: value = self._mod_file_cache[(modname, contextfile)] except KeyError: try: value = file_info_from_modpath(modname.split("."), context_file=contextfile) except ImportError as e: value = AstroidImportError( "Failed to import module {modname} with error:\n{error}.", modname=modname, # we remove the traceback here to save on memory usage (since these exceptions are cached) error=e.with_traceback(None), ) self._mod_file_cache[(modname, contextfile)] = value if isinstance(value, AstroidBuildingError): # we remove the traceback here to save on memory usage (since these exceptions are cached) raise value.with_traceback(None) return value def ast_from_module(self, module, modname=None): """given an imported module, return the astroid object""" modname = modname or module.__name__ if modname in self.astroid_cache: return self.astroid_cache[modname] try: # some builtin modules don't have __file__ attribute filepath = module.__file__ if is_python_source(filepath): return self.ast_from_file(filepath, modname) except AttributeError: pass # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder return AstroidBuilder(self).module_build(module, modname) def ast_from_class(self, klass, modname=None): """get astroid for the given class""" if modname is None: try: modname = klass.__module__ except AttributeError as exc: raise AstroidBuildingError( "Unable to get module for class {class_name}.", cls=klass, class_repr=safe_repr(klass), modname=modname, ) from exc modastroid = self.ast_from_module_name(modname) return modastroid.getattr(klass.__name__)[0] # XXX def infer_ast_from_something(self, obj, context=None): """infer astroid for the given class""" if hasattr(obj, "__class__") and not isinstance(obj, type): klass = obj.__class__ else: klass = obj try: modname = klass.__module__ except AttributeError as exc: raise AstroidBuildingError( "Unable to get module for {class_repr}.", cls=klass, class_repr=safe_repr(klass), ) from exc except Exception as exc: raise AstroidImportError( "Unexpected error while retrieving module for {class_repr}:\n" "{error}", cls=klass, class_repr=safe_repr(klass), ) from exc try: name = klass.__name__ except AttributeError as exc: raise AstroidBuildingError( "Unable to get name for {class_repr}:\n", cls=klass, class_repr=safe_repr(klass), ) from exc except Exception as exc: raise AstroidImportError( "Unexpected error while retrieving name for {class_repr}:\n" "{error}", cls=klass, class_repr=safe_repr(klass), ) from exc # take care, on living object __module__ is regularly wrong :( modastroid = self.ast_from_module_name(modname) if klass is obj: for inferred in modastroid.igetattr(name, context): yield inferred else: for inferred in modastroid.igetattr(name, context): yield inferred.instantiate_class() def register_failed_import_hook(self, hook): """Registers a hook to resolve imports that cannot be found otherwise. `hook` must be a function that accepts a single argument `modname` which contains the name of the module or package that could not be imported. If `hook` can resolve the import, must return a node of type `astroid.Module`, otherwise, it must raise `AstroidBuildingError`. """ self._failed_import_hooks.append(hook) def cache_module(self, module): """Cache a module if no module with the same name is known yet.""" self.astroid_cache.setdefault(module.name, module) def bootstrap(self): """Bootstrap the required AST modules needed for the manager to work The bootstrap usually involves building the AST for the builtins module, which is required by the rest of astroid to work correctly. """ from astroid import raw_building # pylint: disable=import-outside-toplevel raw_building._astroid_bootstrapping() def clear_cache(self): """Clear the underlying cache. Also bootstraps the builtins module.""" self.astroid_cache.clear() self.bootstrap()
def register_type_constraints_setter(): """Instantiate a visitor to transform the nodes. Register the transform functions on an instance of TransformVisitor. """ type_visitor = TransformVisitor() type_visitor.register_transform(astroid.Const, set_const_type_constraints) type_visitor.register_transform(astroid.Tuple, set_tuple_type_constraints) type_visitor.register_transform(astroid.List, set_list_type_constraints) type_visitor.register_transform(astroid.Dict, set_dict_type_constraints) type_visitor.register_transform(astroid.Name, set_name_type_constraints) type_visitor.register_transform(astroid.BinOp, set_binop_type_constraints) type_visitor.register_transform(astroid.UnaryOp, set_unaryop_type_constraints) type_visitor.register_transform(astroid.Index, set_index_type_constraints) type_visitor.register_transform(astroid.Subscript, set_subscript_type_constraints) type_visitor.register_transform(astroid.Compare, set_compare_type_constraints) type_visitor.register_transform(astroid.BoolOp, set_boolop_type_constraints) type_visitor.register_transform(astroid.Expr, set_expr_type_constraints) type_visitor.register_transform(astroid.Assign, set_assign_type_constraints) type_visitor.register_transform(astroid.Return, set_return_type_constraints) type_visitor.register_transform(astroid.FunctionDef, set_functiondef_type_constraints) type_visitor.register_transform(astroid.Call, set_call_type_constraints) type_visitor.register_transform(astroid.Module, set_module_type_constraints) return type_visitor
class AstroidManager: """Responsible to build astroid from files or modules. Use the Borg (singleton) pattern. """ name = "astroid loader" brain: AstroidManagerBrain = { "astroid_cache": {}, "_mod_file_cache": {}, "_failed_import_hooks": [], "always_load_extensions": False, "optimize_ast": False, "extension_package_whitelist": set(), "_transform": TransformVisitor(), } max_inferable_values: ClassVar[int] = 100 def __init__(self) -> None: # NOTE: cache entries are added by the [re]builder self.astroid_cache = AstroidManager.brain["astroid_cache"] self._mod_file_cache = AstroidManager.brain["_mod_file_cache"] self._failed_import_hooks = AstroidManager.brain[ "_failed_import_hooks"] self.always_load_extensions = AstroidManager.brain[ "always_load_extensions"] self.optimize_ast = AstroidManager.brain["optimize_ast"] self.extension_package_whitelist = AstroidManager.brain[ "extension_package_whitelist"] self._transform = AstroidManager.brain["_transform"] @property def register_transform(self): # This and unregister_transform below are exported for convenience return self._transform.register_transform @property def unregister_transform(self): return self._transform.unregister_transform @property def builtins_module(self) -> nodes.Module: return self.astroid_cache["builtins"] def visit_transforms(self, node): """Visit the transforms and apply them to the given *node*.""" return self._transform.visit(node) def ast_from_file(self, filepath, modname=None, fallback=True, source=False): """given a module name, return the astroid object""" try: filepath = get_source_file(filepath, include_no_ext=True) source = True except NoSourceFile: pass if modname is None: try: modname = ".".join(modpath_from_file(filepath)) except ImportError: modname = filepath if (modname in self.astroid_cache and self.astroid_cache[modname].file == filepath): return self.astroid_cache[modname] if source: # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder return AstroidBuilder(self).file_build(filepath, modname) if fallback and modname: return self.ast_from_module_name(modname) raise AstroidBuildingError("Unable to build an AST for {path}.", path=filepath) def ast_from_string(self, data, modname="", filepath=None): """Given some source code as a string, return its corresponding astroid object""" # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder return AstroidBuilder(self).string_build(data, modname, filepath) def _build_stub_module(self, modname): # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder return AstroidBuilder(self).string_build("", modname) def _build_namespace_module(self, modname: str, path: list[str]) -> nodes.Module: # pylint: disable=import-outside-toplevel; circular import from astroid.builder import build_namespace_package_module return build_namespace_package_module(modname, path) def _can_load_extension(self, modname: str) -> bool: if self.always_load_extensions: return True if is_standard_module(modname): return True return is_module_name_part_of_extension_package_whitelist( modname, self.extension_package_whitelist) def ast_from_module_name( self, modname: str | None, context_file: str | None = None, use_cache: bool = True, ) -> nodes.Module: """Given a module name, return the astroid object.""" # Sometimes we don't want to use the cache. For example, when we're # importing a module with the same name as the file that is importing # we want to fallback on the import system to make sure we get the correct # module. if modname in self.astroid_cache and use_cache: return self.astroid_cache[modname] if modname == "__main__": return self._build_stub_module(modname) if context_file: old_cwd = os.getcwd() os.chdir(os.path.dirname(context_file)) try: found_spec = self.file_from_module_name(modname, context_file) if found_spec.type == spec.ModuleType.PY_ZIPMODULE: module = self.zip_import_data(found_spec.location) if module is not None: return module elif found_spec.type in ( spec.ModuleType.C_BUILTIN, spec.ModuleType.C_EXTENSION, ): if (found_spec.type == spec.ModuleType.C_EXTENSION and not self._can_load_extension(modname)): return self._build_stub_module(modname) try: module = load_module_from_name(modname) except Exception as e: raise AstroidImportError( "Loading {modname} failed with:\n{error}", modname=modname, path=found_spec.location, ) from e return self.ast_from_module(module, modname) elif found_spec.type == spec.ModuleType.PY_COMPILED: raise AstroidImportError( "Unable to load compiled module {modname}.", modname=modname, path=found_spec.location, ) elif found_spec.type == spec.ModuleType.PY_NAMESPACE: return self._build_namespace_module( modname, found_spec.submodule_search_locations) elif found_spec.type == spec.ModuleType.PY_FROZEN: if found_spec.location is None: return self._build_stub_module(modname) # For stdlib frozen modules we can determine the location and # can therefore create a module from the source file return self.ast_from_file(found_spec.location, modname, fallback=False) if found_spec.location is None: raise AstroidImportError( "Can't find a file for module {modname}.", modname=modname) return self.ast_from_file(found_spec.location, modname, fallback=False) except AstroidBuildingError as e: for hook in self._failed_import_hooks: try: return hook(modname) except AstroidBuildingError: pass raise e finally: if context_file: os.chdir(old_cwd) def zip_import_data(self, filepath): if zipimport is None: return None # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder builder = AstroidBuilder(self) for ext in ZIP_IMPORT_EXTS: try: eggpath, resource = filepath.rsplit(ext + os.path.sep, 1) except ValueError: continue try: # pylint: disable-next=no-member importer = zipimport.zipimporter(eggpath + ext) zmodname = resource.replace(os.path.sep, ".") if importer.is_package(resource): zmodname = zmodname + ".__init__" module = builder.string_build(importer.get_source(resource), zmodname, filepath) return module except Exception: # pylint: disable=broad-except continue return None def file_from_module_name(self, modname, contextfile): try: value = self._mod_file_cache[(modname, contextfile)] except KeyError: try: value = file_info_from_modpath(modname.split("."), context_file=contextfile) except ImportError as e: # pylint: disable-next=redefined-variable-type value = AstroidImportError( "Failed to import module {modname} with error:\n{error}.", modname=modname, # we remove the traceback here to save on memory usage (since these exceptions are cached) error=e.with_traceback(None), ) self._mod_file_cache[(modname, contextfile)] = value if isinstance(value, AstroidBuildingError): # we remove the traceback here to save on memory usage (since these exceptions are cached) raise value.with_traceback(None) # pylint: disable=no-member return value def ast_from_module(self, module: types.ModuleType, modname: str | None = None): """given an imported module, return the astroid object""" modname = modname or module.__name__ if modname in self.astroid_cache: return self.astroid_cache[modname] try: # some builtin modules don't have __file__ attribute filepath = module.__file__ if is_python_source(filepath): return self.ast_from_file(filepath, modname) except AttributeError: pass # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder return AstroidBuilder(self).module_build(module, modname) def ast_from_class(self, klass, modname=None): """get astroid for the given class""" if modname is None: try: modname = klass.__module__ except AttributeError as exc: raise AstroidBuildingError( "Unable to get module for class {class_name}.", cls=klass, class_repr=safe_repr(klass), modname=modname, ) from exc modastroid = self.ast_from_module_name(modname) return modastroid.getattr(klass.__name__)[0] # XXX def infer_ast_from_something(self, obj, context=None): """infer astroid for the given class""" if hasattr(obj, "__class__") and not isinstance(obj, type): klass = obj.__class__ else: klass = obj try: modname = klass.__module__ except AttributeError as exc: raise AstroidBuildingError( "Unable to get module for {class_repr}.", cls=klass, class_repr=safe_repr(klass), ) from exc except Exception as exc: raise AstroidImportError( "Unexpected error while retrieving module for {class_repr}:\n" "{error}", cls=klass, class_repr=safe_repr(klass), ) from exc try: name = klass.__name__ except AttributeError as exc: raise AstroidBuildingError( "Unable to get name for {class_repr}:\n", cls=klass, class_repr=safe_repr(klass), ) from exc except Exception as exc: raise AstroidImportError( "Unexpected error while retrieving name for {class_repr}:\n{error}", cls=klass, class_repr=safe_repr(klass), ) from exc # take care, on living object __module__ is regularly wrong :( modastroid = self.ast_from_module_name(modname) if klass is obj: for inferred in modastroid.igetattr(name, context): yield inferred else: for inferred in modastroid.igetattr(name, context): yield inferred.instantiate_class() def register_failed_import_hook(self, hook): """Registers a hook to resolve imports that cannot be found otherwise. `hook` must be a function that accepts a single argument `modname` which contains the name of the module or package that could not be imported. If `hook` can resolve the import, must return a node of type `astroid.Module`, otherwise, it must raise `AstroidBuildingError`. """ self._failed_import_hooks.append(hook) def cache_module(self, module): """Cache a module if no module with the same name is known yet.""" self.astroid_cache.setdefault(module.name, module) def bootstrap(self) -> None: """Bootstrap the required AST modules needed for the manager to work The bootstrap usually involves building the AST for the builtins module, which is required by the rest of astroid to work correctly. """ from astroid import raw_building # pylint: disable=import-outside-toplevel raw_building._astroid_bootstrapping() def clear_cache(self) -> None: """Clear the underlying cache, bootstrap the builtins module and re-register transforms.""" # import here because of cyclic imports # pylint: disable=import-outside-toplevel from astroid.inference_tip import clear_inference_tip_cache from astroid.interpreter.objectmodel import ObjectModel from astroid.nodes.node_classes import LookupMixIn clear_inference_tip_cache() self.astroid_cache.clear() # NB: not a new TransformVisitor() AstroidManager.brain[ "_transform"].transforms = collections.defaultdict(list) for lru_cache in ( LookupMixIn.lookup, _cache_normalize_path_, util.is_namespace, ObjectModel.attributes, ): lru_cache.cache_clear() # type: ignore[attr-defined] self.bootstrap() # Reload brain plugins. During initialisation this is done in astroid.__init__.py for module in BRAIN_MODULES_DIRECTORY.iterdir(): if module.suffix == ".py": module_spec = find_spec(f"astroid.brain.{module.stem}") assert module_spec module_object = module_from_spec(module_spec) assert module_spec.loader module_spec.loader.exec_module(module_object)
def register_rule(rule: AstroidRule) -> None: transformer: TransformVisitor = TransformVisitor() transformer.register_transform( rule.on_node, rule.transform, rule.predicate, ) yield transformer