Skip to content

Commit 61aa567

Browse files
authored
Deprecate nodes import from astroid (#2837)
1 parent 166f444 commit 61aa567

File tree

3 files changed

+175
-85
lines changed

3 files changed

+175
-85
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ Release date: TBA
9696
Closes pylint-dev/pylint#8985
9797
Closes pylint-dev/pylint#10558
9898

99+
* Deprecate importing node classes from ``astroid`` directly. This will be removed in v5.
100+
It's recommended to import them from ``astroid.nodes`` instead.
101+
102+
Refs #2837
103+
99104

100105
What's New in astroid 3.3.11?
101106
=============================

astroid/__init__.py

Lines changed: 151 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -80,91 +80,91 @@
8080
from astroid.astroid_manager import MANAGER
8181
from astroid.nodes import (
8282
CONST_CLS,
83-
AnnAssign,
84-
Arguments,
85-
Assert,
86-
Assign,
87-
AssignAttr,
88-
AssignName,
89-
AsyncFor,
90-
AsyncFunctionDef,
91-
AsyncWith,
92-
Attribute,
93-
AugAssign,
94-
Await,
95-
BinOp,
96-
BoolOp,
97-
Break,
98-
Call,
99-
ClassDef,
100-
Compare,
101-
Comprehension,
102-
ComprehensionScope,
103-
Const,
104-
Continue,
105-
Decorators,
106-
DelAttr,
107-
Delete,
108-
DelName,
109-
Dict,
110-
DictComp,
111-
DictUnpack,
112-
EmptyNode,
113-
EvaluatedObject,
114-
ExceptHandler,
115-
Expr,
116-
For,
117-
FormattedValue,
118-
FunctionDef,
119-
GeneratorExp,
120-
Global,
121-
If,
122-
IfExp,
123-
Import,
124-
ImportFrom,
125-
Interpolation,
126-
JoinedStr,
127-
Keyword,
128-
Lambda,
129-
List,
130-
ListComp,
131-
Match,
132-
MatchAs,
133-
MatchCase,
134-
MatchClass,
135-
MatchMapping,
136-
MatchOr,
137-
MatchSequence,
138-
MatchSingleton,
139-
MatchStar,
140-
MatchValue,
141-
Module,
142-
Name,
143-
NamedExpr,
144-
NodeNG,
145-
Nonlocal,
146-
ParamSpec,
147-
Pass,
148-
Raise,
149-
Return,
150-
Set,
151-
SetComp,
152-
Slice,
153-
Starred,
154-
Subscript,
155-
TemplateStr,
156-
Try,
157-
TryStar,
158-
Tuple,
159-
TypeAlias,
160-
TypeVar,
161-
TypeVarTuple,
162-
UnaryOp,
163-
Unknown,
164-
While,
165-
With,
166-
Yield,
167-
YieldFrom,
83+
AnnAssign as _DEPRECATED_AnnAssign,
84+
Arguments as _DEPRECATED_Arguments,
85+
Assert as _DEPRECATED_Assert,
86+
Assign as _DEPRECATED_Assign,
87+
AssignAttr as _DEPRECATED_AssignAttr,
88+
AssignName as _DEPRECATED_AssignName,
89+
AsyncFor as _DEPRECATED_AsyncFor,
90+
AsyncFunctionDef as _DEPRECATED_AsyncFunctionDef,
91+
AsyncWith as _DEPRECATED_AsyncWith,
92+
Attribute as _DEPRECATED_Attribute,
93+
AugAssign as _DEPRECATED_AugAssign,
94+
Await as _DEPRECATED_Await,
95+
BinOp as _DEPRECATED_BinOp,
96+
BoolOp as _DEPRECATED_BoolOp,
97+
Break as _DEPRECATED_Break,
98+
Call as _DEPRECATED_Call,
99+
ClassDef as _DEPRECATED_ClassDef,
100+
Compare as _DEPRECATED_Compare,
101+
Comprehension as _DEPRECATED_Comprehension,
102+
ComprehensionScope as _DEPRECATED_ComprehensionScope,
103+
Const as _DEPRECATED_Const,
104+
Continue as _DEPRECATED_Continue,
105+
Decorators as _DEPRECATED_Decorators,
106+
DelAttr as _DEPRECATED_DelAttr,
107+
Delete as _DEPRECATED_Delete,
108+
DelName as _DEPRECATED_DelName,
109+
Dict as _DEPRECATED_Dict,
110+
DictComp as _DEPRECATED_DictComp,
111+
DictUnpack as _DEPRECATED_DictUnpack,
112+
EmptyNode as _DEPRECATED_EmptyNode,
113+
EvaluatedObject as _DEPRECATED_EvaluatedObject,
114+
ExceptHandler as _DEPRECATED_ExceptHandler,
115+
Expr as _DEPRECATED_Expr,
116+
For as _DEPRECATED_For,
117+
FormattedValue as _DEPRECATED_FormattedValue,
118+
FunctionDef as _DEPRECATED_FunctionDef,
119+
GeneratorExp as _DEPRECATED_GeneratorExp,
120+
Global as _DEPRECATED_Global,
121+
If as _DEPRECATED_If,
122+
IfExp as _DEPRECATED_IfExp,
123+
Import as _DEPRECATED_Import,
124+
ImportFrom as _DEPRECATED_ImportFrom,
125+
Interpolation as _DEPRECATED_Interpolation,
126+
JoinedStr as _DEPRECATED_JoinedStr,
127+
Keyword as _DEPRECATED_Keyword,
128+
Lambda as _DEPRECATED_Lambda,
129+
List as _DEPRECATED_List,
130+
ListComp as _DEPRECATED_ListComp,
131+
Match as _DEPRECATED_Match,
132+
MatchAs as _DEPRECATED_MatchAs,
133+
MatchCase as _DEPRECATED_MatchCase,
134+
MatchClass as _DEPRECATED_MatchClass,
135+
MatchMapping as _DEPRECATED_MatchMapping,
136+
MatchOr as _DEPRECATED_MatchOr,
137+
MatchSequence as _DEPRECATED_MatchSequence,
138+
MatchSingleton as _DEPRECATED_MatchSingleton,
139+
MatchStar as _DEPRECATED_MatchStar,
140+
MatchValue as _DEPRECATED_MatchValue,
141+
Module as _DEPRECATED_Module,
142+
Name as _DEPRECATED_Name,
143+
NamedExpr as _DEPRECATED_NamedExpr,
144+
NodeNG as _DEPRECATED_NodeNG,
145+
Nonlocal as _DEPRECATED_Nonlocal,
146+
ParamSpec as _DEPRECATED_ParamSpec,
147+
Pass as _DEPRECATED_Pass,
148+
Raise as _DEPRECATED_Raise,
149+
Return as _DEPRECATED_Return,
150+
Set as _DEPRECATED_Set,
151+
SetComp as _DEPRECATED_SetComp,
152+
Slice as _DEPRECATED_Slice,
153+
Starred as _DEPRECATED_Starred,
154+
Subscript as _DEPRECATED_Subscript,
155+
TemplateStr as _DEPRECATED_TemplateStr,
156+
Try as _DEPRECATED_Try,
157+
TryStar as _DEPRECATED_TryStar,
158+
Tuple as _DEPRECATED_Tuple,
159+
TypeAlias as _DEPRECATED_TypeAlias,
160+
TypeVar as _DEPRECATED_TypeVar,
161+
TypeVarTuple as _DEPRECATED_TypeVarTuple,
162+
UnaryOp as _DEPRECATED_UnaryOp,
163+
Unknown as _DEPRECATED_Unknown,
164+
While as _DEPRECATED_While,
165+
With as _DEPRECATED_With,
166+
Yield as _DEPRECATED_Yield,
167+
YieldFrom as _DEPRECATED_YieldFrom,
168168
are_exclusive,
169169
builtin_lookup,
170170
unpack_infer,
@@ -174,3 +174,69 @@
174174
# isort: on
175175

176176
from astroid.util import Uninferable
177+
178+
__all__ = [
179+
"CONST_CLS",
180+
"MANAGER",
181+
"AstroidBuildingError",
182+
"AstroidError",
183+
"AstroidImportError",
184+
"AstroidIndexError",
185+
"AstroidSyntaxError",
186+
"AstroidTypeError",
187+
"AstroidValueError",
188+
"AttributeInferenceError",
189+
"BaseInstance",
190+
"BoundMethod",
191+
"Context",
192+
"DuplicateBasesError",
193+
"ExceptionInstance",
194+
"InconsistentMroError",
195+
"InferenceError",
196+
"InferenceOverwriteError",
197+
"Instance",
198+
"MroError",
199+
"NameInferenceError",
200+
"NoDefault",
201+
"NotFoundError",
202+
"ParentMissingError",
203+
"ResolveError",
204+
"StatementMissing",
205+
"SuperArgumentTypeError",
206+
"SuperError",
207+
"TooManyLevelsError",
208+
"UnboundMethod",
209+
"Uninferable",
210+
"UnresolvableName",
211+
"UseInferenceDefault",
212+
"__version__",
213+
"_inference_tip_cached",
214+
"are_exclusive",
215+
"builtin_lookup",
216+
"extract_node",
217+
"function_to_method",
218+
"inference_tip",
219+
"node_classes",
220+
"parse",
221+
"raw_building",
222+
"register_module_extender",
223+
"scoped_nodes",
224+
"unpack_infer",
225+
"version",
226+
]
227+
228+
229+
def __getattr__(name: str):
230+
if (val := globals().get(f"_DEPRECATED_{name}")) is None:
231+
msg = f"module '{__name__}' has no attribute '{name}"
232+
raise AttributeError(msg)
233+
234+
# pylint: disable-next=import-outside-toplevel
235+
import warnings
236+
237+
msg = (
238+
f"importing '{name}' from 'astroid' is deprecated and will be removed in v5, "
239+
"import it from 'astroid.nodes' instead"
240+
)
241+
warnings.warn(msg, DeprecationWarning, stacklevel=2)
242+
return val

tests/test_nodes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sys
1414
import textwrap
1515
import unittest
16+
import warnings
1617
from typing import Any
1718

1819
import pytest
@@ -2311,3 +2312,21 @@ def test_arguments_default_value():
23112312

23122313
node = extract_node("def fruit(seeds, flavor='good', *, peel='maybe'): ...")
23132314
assert node.args.default_value("flavor").value == "good"
2315+
2316+
2317+
def test_deprecated_nodes_import_from_toplevel():
2318+
# pylint: disable=import-outside-toplevel,no-name-in-module
2319+
with pytest.raises(
2320+
DeprecationWarning, match="importing 'For' from 'astroid' is deprecated"
2321+
):
2322+
from astroid import For
2323+
2324+
with warnings.catch_warnings():
2325+
warnings.simplefilter("ignore", DeprecationWarning)
2326+
from astroid import For
2327+
2328+
assert For is nodes.For
2329+
2330+
# This should not raise a DeprecationWarning
2331+
# pylint: disable-next=unused-import
2332+
from astroid import builtin_lookup

0 commit comments

Comments
 (0)