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

Move typing imports and some definitions behind guard #2585

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion astroid/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid import nodes
from astroid.bases import Instance
from astroid.context import CallContext, InferenceContext
from astroid.exceptions import InferenceError, NoDefault
from astroid.typing import InferenceResult
from astroid.util import Uninferable, UninferableBase, safe_infer

if TYPE_CHECKING:
from astroid.typing import InferenceResult


class CallSite:
"""Class for understanding arguments passed into a call site.
Expand Down
16 changes: 9 additions & 7 deletions astroid/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

import collections
import collections.abc
from collections.abc import Iterable, Iterator
from typing import TYPE_CHECKING, Any, Literal
from typing import TYPE_CHECKING

from astroid import decorators, nodes
from astroid.const import PY310_PLUS
Expand All @@ -27,15 +26,18 @@
NameInferenceError,
)
from astroid.interpreter import objectmodel
from astroid.typing import (
InferenceErrorInfo,
InferenceResult,
SuccessfulInferenceResult,
)
from astroid.util import Uninferable, UninferableBase, safe_infer

if TYPE_CHECKING:
from collections.abc import Iterable, Iterator
from typing import Any, Literal

from astroid.constraint import Constraint
from astroid.typing import (
InferenceErrorInfo,
InferenceResult,
SuccessfulInferenceResult,
)


PROPERTIES = {"builtins.property", "abc.abstractproperty"}
Expand Down
6 changes: 5 additions & 1 deletion astroid/brain/brain_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid import arguments, nodes
from astroid.context import InferenceContext
from astroid.exceptions import UseInferenceDefault
from astroid.inference_tip import inference_tip
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid.context import InferenceContext


def infer_namespace(node, context: InferenceContext | None = None):
callsite = arguments.CallSite.from_call(node, context=context)
Expand Down
9 changes: 8 additions & 1 deletion astroid/brain/brain_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
Without this hook pylint reports unsupported-assignment-operation
for attrs classes
"""
from astroid.manager import AstroidManager

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid.nodes.node_classes import AnnAssign, Assign, AssignName, Call, Unknown
from astroid.nodes.scoped_nodes import ClassDef
from astroid.util import safe_infer

if TYPE_CHECKING:
from astroid.manager import AstroidManager

ATTRIB_NAMES = frozenset(
(
"attr.Factory",
Expand Down
13 changes: 11 additions & 2 deletions astroid/brain/brain_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

"""Astroid hooks for understanding ``boto3.ServiceRequest()``."""

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid.builder import extract_node
from astroid.manager import AstroidManager
from astroid.nodes.scoped_nodes import ClassDef

if TYPE_CHECKING:
from astroid.manager import AstroidManager


BOTO_SERVICE_FACTORY_QUALIFIED_NAME = "boto3.resources.base.ServiceResource"


Expand All @@ -28,5 +35,7 @@ def _looks_like_boto3_service_request(node: ClassDef) -> bool:

def register(manager: AstroidManager) -> None:
manager.register_transform(
ClassDef, service_request_transform, _looks_like_boto3_service_request
ClassDef,
service_request_transform,
_looks_like_boto3_service_request,
)
19 changes: 11 additions & 8 deletions astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
from __future__ import annotations

import itertools
from collections.abc import Callable, Iterable, Iterator
from collections.abc import Iterator
from functools import partial
from typing import TYPE_CHECKING, Any, NoReturn, Union, cast
from typing import TYPE_CHECKING, Union, cast

from astroid import arguments, helpers, nodes, objects, util
from astroid.builder import AstroidBuilder
from astroid.context import InferenceContext
from astroid.exceptions import (
AstroidTypeError,
AttributeInferenceError,
Expand All @@ -25,14 +24,18 @@
from astroid.manager import AstroidManager
from astroid.nodes import scoped_nodes
from astroid.raw_building import build_module
from astroid.typing import (
ConstFactoryResult,
InferenceResult,
SuccessfulInferenceResult,
)

if TYPE_CHECKING:
from collections.abc import Callable, Iterable
from typing import Any, NoReturn

from astroid.bases import Instance
from astroid.context import InferenceContext
from astroid.typing import (
ConstFactoryResult,
InferenceResult,
SuccessfulInferenceResult,
)

ContainerObjects = Union[
objects.FrozenSet,
Expand Down
8 changes: 6 additions & 2 deletions astroid/brain/brain_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid.brain.helpers import register_module_extender
from astroid.builder import extract_node, parse
from astroid.context import InferenceContext
from astroid.exceptions import AttributeInferenceError
from astroid.manager import AstroidManager
from astroid.nodes.scoped_nodes import ClassDef

if TYPE_CHECKING:
from astroid.context import InferenceContext
from astroid.manager import AstroidManager


def _collections_transform():
return parse(
Expand Down
10 changes: 8 additions & 2 deletions astroid/brain/brain_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

from astroid import nodes
from __future__ import annotations

from typing import TYPE_CHECKING

from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid import nodes
from astroid.manager import AstroidManager


def _re_transform() -> nodes.Module:
Expand Down
9 changes: 7 additions & 2 deletions astroid/brain/brain_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
Thus astroid doesn't know that the value member is a builtin type
among float, int, bytes or str.
"""
from __future__ import annotations

import sys
from typing import TYPE_CHECKING

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid import nodes
from astroid.manager import AstroidManager


def enrich_ctypes_redefined_types() -> nodes.Module:
Expand Down
10 changes: 8 additions & 2 deletions astroid/brain/brain_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

from astroid import nodes
from __future__ import annotations

from typing import TYPE_CHECKING

from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid import nodes
from astroid.manager import AstroidManager


def _curses_transform() -> nodes.Module:
Expand Down
24 changes: 15 additions & 9 deletions astroid/brain/brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,29 @@

from __future__ import annotations

from collections.abc import Iterator
from typing import Literal, Union
from typing import TYPE_CHECKING

from astroid import bases, context, nodes
from astroid import bases, nodes
from astroid.builder import parse
from astroid.const import PY310_PLUS, PY313_PLUS
from astroid.exceptions import AstroidSyntaxError, InferenceError, UseInferenceDefault
from astroid.inference_tip import inference_tip
from astroid.manager import AstroidManager
from astroid.typing import InferenceResult
from astroid.util import Uninferable, UninferableBase, safe_infer

_FieldDefaultReturn = Union[
None,
tuple[Literal["default"], nodes.NodeNG],
tuple[Literal["default_factory"], nodes.Call],
]
if TYPE_CHECKING:
from collections.abc import Iterator
from typing import Literal, Union

from astroid import context
from astroid.typing import InferenceResult

_FieldDefaultReturn = Union[
None,
tuple[Literal["default"], nodes.NodeNG],
tuple[Literal["default_factory"], nodes.Call],
]


DATACLASSES_DECORATORS = frozenset(("dataclass",))
FIELD_NAME = "field"
Expand Down
8 changes: 7 additions & 1 deletion astroid/brain/brain_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

from astroid import nodes
from __future__ import annotations

from typing import TYPE_CHECKING

from astroid.brain.helpers import register_module_extender
from astroid.builder import AstroidBuilder
from astroid.const import PY312_PLUS
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid import nodes


def datetime_transform() -> nodes.Module:
"""The datetime module was C-accelerated in Python 3.12, so use the
Expand Down
7 changes: 6 additions & 1 deletion astroid/brain/brain_dateutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

"""Astroid hooks for dateutil."""

from __future__ import annotations

import textwrap
from typing import TYPE_CHECKING

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import AstroidBuilder
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid import nodes


def dateutil_transform() -> nodes.Module:
return AstroidBuilder(AstroidManager()).string_build(
Expand Down
16 changes: 11 additions & 5 deletions astroid/brain/brain_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@

from __future__ import annotations

from collections.abc import Iterator
from functools import partial
from itertools import chain
from typing import TYPE_CHECKING

from astroid import BoundMethod, arguments, nodes, objects
from astroid import BoundMethod, arguments, objects
from astroid.builder import extract_node
from astroid.context import InferenceContext
from astroid.exceptions import InferenceError, UseInferenceDefault
from astroid.inference_tip import inference_tip
from astroid.interpreter import objectmodel
from astroid.manager import AstroidManager
from astroid.nodes.node_classes import AssignName, Attribute, Call, Name
from astroid.nodes.scoped_nodes import FunctionDef
from astroid.typing import InferenceResult, SuccessfulInferenceResult
from astroid.util import UninferableBase, safe_infer

if TYPE_CHECKING:
from collections.abc import Iterator

from astroid import nodes
from astroid.context import InferenceContext
from astroid.manager import AstroidManager
from astroid.typing import InferenceResult, SuccessfulInferenceResult


LRU_CACHE = "functools.lru_cache"


Expand Down
10 changes: 8 additions & 2 deletions astroid/brain/brain_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

from astroid import nodes
from __future__ import annotations

from typing import TYPE_CHECKING

from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid import nodes
from astroid.manager import AstroidManager


def _hashlib_transform() -> nodes.Module:
Expand Down
7 changes: 6 additions & 1 deletion astroid/brain/brain_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt

"""Astroid brain hints for some of the `http` module."""
from __future__ import annotations

import textwrap
from typing import TYPE_CHECKING

from astroid import nodes
from astroid.brain.helpers import register_module_extender
from astroid.builder import AstroidBuilder
from astroid.manager import AstroidManager

if TYPE_CHECKING:
from astroid import nodes


def _http_transform() -> nodes.Module:
code = textwrap.dedent(
Expand Down
Loading
Loading