Skip to content

Commit bf4d420

Browse files
Deprecate astroid.helpers.safe_infer()
safe_infer() was moved to astroid.util
1 parent f0c77e1 commit bf4d420

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

astroid/helpers.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from __future__ import annotations
88

9+
import warnings
910
from collections.abc import Generator
1011

1112
from astroid import bases, manager, nodes, objects, raw_building, util
@@ -19,10 +20,20 @@
1920
)
2021
from astroid.nodes import scoped_nodes
2122
from astroid.typing import InferenceResult
23+
from astroid.util import safe_infer as real_safe_infer
2224

23-
# Don't remove the following safe_infer import without a deprecation notice.
24-
# safe_infer was moved to util in #2232, but can be accessed here via this.
25-
from astroid.util import safe_infer
25+
26+
def safe_infer(
27+
node: nodes.NodeNG | bases.Proxy | util.UninferableBase,
28+
context: InferenceContext | None = None,
29+
) -> InferenceResult | None:
30+
# When removing, also remove the real_safe_infer alias
31+
warnings.warn(
32+
"Import safe_infer from astroid.util; this shim in astroid.helpers will be removed.",
33+
DeprecationWarning,
34+
stacklevel=2,
35+
)
36+
return real_safe_infer(node, context=context)
2637

2738

2839
def _build_proxy_class(cls_name: str, builtins: nodes.Module) -> nodes.ClassDef:
@@ -166,7 +177,7 @@ def has_known_bases(klass, context: InferenceContext | None = None) -> bool:
166177
except AttributeError:
167178
pass
168179
for base in klass.bases:
169-
result = safe_infer(base, context=context)
180+
result = real_safe_infer(base, context=context)
170181
# TODO: check for A->B->A->B pattern in class structure too?
171182
if (
172183
not isinstance(result, scoped_nodes.ClassDef)
@@ -241,7 +252,7 @@ def object_len(node, context: InferenceContext | None = None):
241252
# pylint: disable=import-outside-toplevel; circular import
242253
from astroid.objects import FrozenSet
243254

244-
inferred_node = safe_infer(node, context=context)
255+
inferred_node = real_safe_infer(node, context=context)
245256

246257
# prevent self referential length calls from causing a recursion error
247258
# see https://github.com/pylint-dev/astroid/issues/777

tests/test_helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,13 @@ class A(type): #@
264264
def test_uninferable_for_safe_infer() -> None:
265265
uninfer = util.Uninferable
266266
assert util.safe_infer(util.Uninferable) == uninfer
267+
268+
269+
def test_safe_infer_shim() -> None:
270+
with pytest.warns(DeprecationWarning) as records:
271+
helpers.safe_infer(nodes.Unknown())
272+
273+
assert (
274+
"Import safe_infer from astroid.util; this shim in astroid.helpers will be removed."
275+
in records[0].message.args[0]
276+
)

0 commit comments

Comments
 (0)