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

[4.0.x] Make manager required in AstroidBuilder/InspectBuilder #2313

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions astroid/brain/brain_nose.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _pep8(name, caps=CAPITALS):

def _nose_tools_functions():
"""Get an iterator of names and bound methods."""
module = AstroidBuilder().string_build(
module = AstroidBuilder(AstroidManager()).string_build(
textwrap.dedent(
"""
import unittest
Expand Down Expand Up @@ -54,7 +54,7 @@ def _nose_tools_transform(node):

def _nose_tools_trivial_transform():
"""Custom transform for the nose.tools module."""
stub = AstroidBuilder().string_build("""__all__ = []""")
stub = AstroidBuilder(AstroidManager()).string_build("""__all__ = []""")
all_entries = ["ok_", "eq_"]

for pep8_name, method in _nose_tools_functions():
Expand Down
18 changes: 10 additions & 8 deletions astroid/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
from collections.abc import Iterator, Sequence
from io import TextIOWrapper
from tokenize import detect_encoding
from typing import TYPE_CHECKING

from astroid import bases, modutils, nodes, raw_building, rebuilder, util
from astroid._ast import ParserModule, get_parser_module
from astroid.exceptions import AstroidBuildingError, AstroidSyntaxError, InferenceError
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid.manager import AstroidManager

# The name of the transient function that is used to
# wrap expressions to be extracted when calling
Expand Down Expand Up @@ -64,13 +67,11 @@ class AstroidBuilder(raw_building.InspectBuilder):
by default being True.
"""

def __init__(
self, manager: AstroidManager | None = None, apply_transforms: bool = True
) -> None:
def __init__(self, manager: AstroidManager, apply_transforms: bool = True) -> None:
super().__init__(manager)
self._apply_transforms = apply_transforms
if not raw_building.InspectBuilder.bootstrapped:
raw_building._astroid_bootstrapping()
manager.bootstrap()

def module_build(
self, module: types.ModuleType, modname: str | None = None
Expand Down Expand Up @@ -289,10 +290,11 @@ def parse(
Apply the transforms for the give code. Use it if you
don't want the default transforms to be applied.
"""
# pylint: disable-next=import-outside-toplevel
from astroid.manager import AstroidManager

code = textwrap.dedent(code)
builder = AstroidBuilder(
manager=AstroidManager(), apply_transforms=apply_transforms
)
builder = AstroidBuilder(AstroidManager(), apply_transforms=apply_transforms)
return builder.string_build(code, modname=module_name, path=path)


Expand Down
3 changes: 2 additions & 1 deletion astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from typing import Any, Literal, NamedTuple, Protocol

from astroid.const import PY310_PLUS
from astroid.modutils import EXT_LIB_DIRS

from . import util

Expand Down Expand Up @@ -185,6 +184,8 @@ def contribute_to_path(
if spec.location is None:
# Builtin.
return None
# pylint: disable-next=import-outside-toplevel
from astroid.modutils import EXT_LIB_DIRS

if _is_setuptools_namespace(Path(spec.location)):
# extend_path is called, search sys.path for module/packages
Expand Down
19 changes: 1 addition & 18 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Any, ClassVar

from astroid import nodes
from astroid.builder import AstroidBuilder, build_namespace_package_module
from astroid.context import InferenceContext, _invalidate_cache
from astroid.exceptions import AstroidBuildingError, AstroidImportError
from astroid.interpreter._import import spec, util
Expand Down Expand Up @@ -145,9 +146,6 @@ def ast_from_file(
):
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)
Expand All @@ -159,23 +157,14 @@ def ast_from_string(
"""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: str) -> nodes.Module:
# 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: Sequence[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:
Expand Down Expand Up @@ -272,9 +261,6 @@ def zip_import_data(self, filepath: str) -> nodes.Module | None:
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:
Expand Down Expand Up @@ -334,9 +320,6 @@ def ast_from_module(
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: type, modname: str | None = None) -> nodes.ClassDef:
Expand Down
14 changes: 7 additions & 7 deletions astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from astroid import bases, nodes
from astroid.const import _EMPTY_OBJECT_MARKER, IS_PYPY
from astroid.manager import AstroidManager
from astroid.nodes import node_classes

logger = logging.getLogger(__name__)
Expand All @@ -37,8 +36,6 @@
types.ClassMethodDescriptorType,
]

# the keys of CONST_CLS eg python builtin types
_CONSTANTS = tuple(node_classes.CONST_CLS)
TYPE_NONE = type(None)
TYPE_NOTIMPLEMENTED = type(NotImplemented)
TYPE_ELLIPSIS = type(...)
Expand Down Expand Up @@ -428,8 +425,8 @@ class InspectBuilder:

bootstrapped: bool = False

def __init__(self, manager_instance: AstroidManager | None = None) -> None:
self._manager = manager_instance or AstroidManager()
def __init__(self, manager_instance) -> None:
self._manager = manager_instance
self._done: dict[types.ModuleType | type, nodes.Module | nodes.ClassDef] = {}
self._module: types.ModuleType

Expand Down Expand Up @@ -510,7 +507,7 @@ def object_build(
object_build_methoddescriptor(node, member, name)
elif inspect.isdatadescriptor(member):
object_build_datadescriptor(node, member, name)
elif isinstance(member, _CONSTANTS):
elif isinstance(member, tuple(node_classes.CONST_CLS)):
attach_const_node(node, name, member)
elif inspect.isroutine(member):
# This should be called for Jython, where some builtin
Expand Down Expand Up @@ -599,7 +596,10 @@ def _astroid_bootstrapping() -> None:
"""astroid bootstrapping the builtins module"""
# this boot strapping is necessary since we need the Const nodes to
# inspect_build builtins, and then we can proxy Const
builder = InspectBuilder()
# pylint: disable-next=import-outside-toplevel
from astroid.manager import AstroidManager

builder = InspectBuilder(AstroidManager())
astroid_builtin = builder.inspect_build(builtins)

for cls, node_cls in node_classes.CONST_CLS.items():
Expand Down
Loading