summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2023-04-23 11:46:02 +0200
committerDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2023-04-23 12:01:50 +0200
commitf4a1168c0fda96b6bb7d01acb83b39405e7fe07a (patch)
tree049d6c3c47b8bfdea9cda8e3c2db7698e3aa8dd2
parent30df5a84bf8d9ed14e90defb8dc3359b9e25f7e5 (diff)
downloadastroid-git-f4a1168c0fda96b6bb7d01acb83b39405e7fe07a.tar.gz
Remove all ``lazy_imports``
-rw-r--r--astroid/builder.py9
-rw-r--r--astroid/inference.py13
-rw-r--r--astroid/interpreter/objectmodel.py30
-rw-r--r--astroid/nodes/scoped_nodes/scoped_nodes.py7
-rw-r--r--astroid/protocols.py6
-rw-r--r--astroid/util.py10
6 files changed, 41 insertions, 34 deletions
diff --git a/astroid/builder.py b/astroid/builder.py
index dc1738ef..90f211be 100644
--- a/astroid/builder.py
+++ b/astroid/builder.py
@@ -17,19 +17,12 @@ import types
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 import objects
-else:
- objects = util.lazy_import("objects")
-
-
# The name of the transient function that is used to
# wrap expressions to be extracted when calling
# extract_node.
@@ -235,6 +228,8 @@ class AstroidBuilder(raw_building.InspectBuilder):
This adds name to locals and handle members definition.
"""
+ from astroid import objects # pylint: disable=import-outside-toplevel
+
try:
frame = node.frame(future=True)
for inferred in node.expr.infer():
diff --git a/astroid/inference.py b/astroid/inference.py
index 39bd94d7..e18ac69f 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -14,7 +14,16 @@ import typing
from collections.abc import Callable, Generator, Iterable, Iterator
from typing import TYPE_CHECKING, Any, Optional, TypeVar, Union
-from astroid import bases, constraint, decorators, helpers, nodes, protocols, util
+from astroid import (
+ bases,
+ constraint,
+ decorators,
+ helpers,
+ nodes,
+ objects,
+ protocols,
+ util,
+)
from astroid.const import PY310_PLUS
from astroid.context import (
CallContext,
@@ -44,8 +53,6 @@ from astroid.typing import (
if TYPE_CHECKING:
from astroid.objects import Property
-# Prevents circular imports
-objects = util.lazy_import("objects")
_T = TypeVar("_T")
_BaseContainerT = TypeVar("_BaseContainerT", bound=nodes.BaseContainer)
diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py
index 5090c536..bd9e9f5b 100644
--- a/astroid/interpreter/objectmodel.py
+++ b/astroid/interpreter/objectmodel.py
@@ -38,16 +38,12 @@ from astroid.exceptions import AttributeInferenceError, InferenceError, NoDefaul
from astroid.manager import AstroidManager
from astroid.nodes import node_classes
-objects = util.lazy_import("objects")
-builder = util.lazy_import("builder")
-
if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal
if TYPE_CHECKING:
- from astroid import builder
from astroid.objects import Property
IMPL_PREFIX = "attr_"
@@ -137,6 +133,8 @@ class ObjectModel:
@property
def attr___new__(self) -> bases.BoundMethod:
"""Calling cls.__new__(type) on an object returns an instance of 'type'."""
+ from astroid import builder # pylint: disable=import-outside-toplevel
+
node: nodes.FunctionDef = builder.extract_node(
"""def __new__(self, cls): return cls()"""
)
@@ -149,6 +147,8 @@ class ObjectModel:
@property
def attr___init__(self) -> bases.BoundMethod:
"""Calling cls.__init__() normally returns None."""
+ from astroid import builder # pylint: disable=import-outside-toplevel
+
# The *args and **kwargs are necessary not to trigger warnings about missing
# or extra parameters for '__init__' methods we don't infer correctly.
# This BoundMethod is the fallback value for those.
@@ -628,6 +628,8 @@ class ContextManagerModel(ObjectModel):
will bind this method's return value to the target(s) specified in the
as clause of the statement, if any.
"""
+ from astroid import builder # pylint: disable=import-outside-toplevel
+
node: nodes.FunctionDef = builder.extract_node("""def __enter__(self): ...""")
# We set the parent as being the ClassDef of 'object' as that
# is where this method originally comes from
@@ -644,6 +646,8 @@ class ContextManagerModel(ObjectModel):
exception that caused the context to be exited. If the context was exited
without an exception, all three arguments will be None.
"""
+ from astroid import builder # pylint: disable=import-outside-toplevel
+
node: nodes.FunctionDef = builder.extract_node(
"""def __exit__(self, exc_type, exc_value, traceback): ..."""
)
@@ -828,6 +832,8 @@ class DictModel(ObjectModel):
@property
def attr_items(self):
+ from astroid import objects # pylint: disable=import-outside-toplevel
+
elems = []
obj = node_classes.List(parent=self._instance)
for key, value in self._instance.items:
@@ -836,26 +842,30 @@ class DictModel(ObjectModel):
elems.append(elem)
obj.postinit(elts=elems)
- obj = objects.DictItems(obj)
- return self._generic_dict_attribute(obj, "items")
+ items_obj = objects.DictItems(obj)
+ return self._generic_dict_attribute(items_obj, "items")
@property
def attr_keys(self):
+ from astroid import objects # pylint: disable=import-outside-toplevel
+
keys = [key for (key, _) in self._instance.items]
obj = node_classes.List(parent=self._instance)
obj.postinit(elts=keys)
- obj = objects.DictKeys(obj)
- return self._generic_dict_attribute(obj, "keys")
+ keys_obj = objects.DictKeys(obj)
+ return self._generic_dict_attribute(keys_obj, "keys")
@property
def attr_values(self):
+ from astroid import objects # pylint: disable=import-outside-toplevel
+
values = [value for (_, value) in self._instance.items]
obj = node_classes.List(parent=self._instance)
obj.postinit(values)
- obj = objects.DictValues(obj)
- return self._generic_dict_attribute(obj, "values")
+ values_obj = objects.DictValues(obj)
+ return self._generic_dict_attribute(values_obj, "values")
class PropertyModel(ObjectModel):
diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py
index e9fa04cf..0dfd1658 100644
--- a/astroid/nodes/scoped_nodes/scoped_nodes.py
+++ b/astroid/nodes/scoped_nodes/scoped_nodes.py
@@ -61,7 +61,6 @@ if TYPE_CHECKING:
ITER_METHODS = ("__iter__", "__getitem__")
EXCEPTION_BASE_CLASSES = frozenset({"Exception", "BaseException"})
-objects = util.lazy_import("objects")
BUILTIN_DESCRIPTORS = frozenset(
{"classmethod", "staticmethod", "builtins.classmethod", "builtins.staticmethod"}
)
@@ -2365,6 +2364,8 @@ class ClassDef(
:returns: An :class:`Instance` of the :class:`ClassDef` node
"""
+ from astroid import objects # pylint: disable=import-outside-toplevel
+
try:
if any(cls.name in EXCEPTION_BASE_CLASSES for cls in self.mro()):
# Subclasses of exceptions can be exception instances
@@ -2446,6 +2447,8 @@ class ClassDef(
return attrs
def _get_attribute_from_metaclass(self, cls, name, context):
+ from astroid import objects # pylint: disable=import-outside-toplevel
+
try:
attrs = cls.getattr(name, context=context, class_context=True)
except AttributeInferenceError:
@@ -2484,6 +2487,8 @@ class ClassDef(
:returns: The inferred possible values.
"""
+ from astroid import objects # pylint: disable=import-outside-toplevel
+
# set lookup name since this is necessary to infer on import nodes for
# instance
context = copy_context(context)
diff --git a/astroid/protocols.py b/astroid/protocols.py
index e9cc5a6d..07d11092 100644
--- a/astroid/protocols.py
+++ b/astroid/protocols.py
@@ -14,7 +14,7 @@ import operator as operator_mod
from collections.abc import Callable, Generator, Iterator, Sequence
from typing import Any, TypeVar
-from astroid import arguments, bases, decorators, helpers, nodes, util
+from astroid import arguments, bases, decorators, helpers, nodes, objects, util
from astroid.const import Context
from astroid.context import InferenceContext, copy_context
from astroid.exceptions import (
@@ -31,10 +31,6 @@ from astroid.typing import (
SuccessfulInferenceResult,
)
-raw_building = util.lazy_import("raw_building")
-objects = util.lazy_import("objects")
-
-
_TupleListNodeT = TypeVar("_TupleListNodeT", nodes.Tuple, nodes.List)
diff --git a/astroid/util.py b/astroid/util.py
index 43e04df3..50bde0b1 100644
--- a/astroid/util.py
+++ b/astroid/util.py
@@ -5,7 +5,6 @@
from __future__ import annotations
-import importlib
import sys
import warnings
from typing import Any
@@ -26,12 +25,6 @@ def lazy_descriptor(obj):
return DescriptorProxy(obj)
-def lazy_import(module_name: str) -> lazy_object_proxy.Proxy:
- return lazy_object_proxy.Proxy(
- lambda: importlib.import_module("." + module_name, "astroid")
- )
-
-
class UninferableBase:
"""Special inference object, which is returned when inference fails.
@@ -85,7 +78,8 @@ class BadUnaryOperationMessage(BadOperationMessage):
@property
def _object_type_helper(self):
- helpers = lazy_import("helpers")
+ from astroid import helpers # pylint: disable=import-outside-toplevel
+
return helpers.object_type
def _object_type(self, obj):