Skip to content

Commit

Permalink
fixup! make constants have synthetic root as their parent
Browse files Browse the repository at this point in the history
  • Loading branch information
temyurchenko committed Oct 11, 2024
1 parent 3969f5d commit 5c0631a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
40 changes: 19 additions & 21 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ def function_to_method(n, klass):
return n


def _infer_first_not_none(
arg: SuccessfulInferenceResult, context: InferenceContext
) -> InferenceResult | None:
for b in arg.infer(context=context.clone()):
if not (isinstance(b, node_classes.Const) and b.value is None):
return b
return None

Check warning on line 180 in astroid/nodes/scoped_nodes/scoped_nodes.py

View check run for this annotation

Codecov / codecov/patch

astroid/nodes/scoped_nodes/scoped_nodes.py#L180

Added line #L180 was not covered by tests


class Module(LocalsDictNodeNG):
"""Class representing an :class:`ast.Module` node.
Expand Down Expand Up @@ -1540,10 +1549,7 @@ def infer_yield_result(self, context: InferenceContext | None = None):
"""
for yield_ in self.nodes_of_class(node_classes.Yield):
if yield_.value is None:
const = node_classes.Const(None)
const.parent = yield_
const.lineno = yield_.lineno
yield const
yield node_classes.Const(None, parent=yield_, lineno=yield_.lineno)
elif yield_.scope() == self:
yield from yield_.value.infer(context=context)

Expand All @@ -1553,6 +1559,8 @@ def infer_call_result(
context: InferenceContext | None = None,
) -> Iterator[InferenceResult]:
"""Infer what the function returns when called."""
if context is None:
context = InferenceContext()
if self.is_generator():
if isinstance(self, AsyncFunctionDef):
generator_cls: type[bases.Generator] = bases.AsyncGenerator
Expand Down Expand Up @@ -1584,16 +1592,11 @@ def infer_call_result(
f"caller.args was neither Arguments nor list; got {type(caller.args)}"
)
if isinstance(metaclass, ClassDef):
try:
# Find the first non-None inferred base value
get_base = lambda arg: next(
b
for b in arg.infer(context=context.clone() if context else None)
if not (isinstance(b, node_classes.Const) and b.value is None)
)
class_bases = [get_base(arg) for arg in caller.args[1:]]
except StopIteration as e:
raise InferenceError(node=caller.args[1:], context=context) from e
class_bases = []
for arg in caller.args[1:]:
if (base := _infer_first_not_none(arg, context)) is None:
raise InferenceError(node=arg, context=context)

Check warning on line 1598 in astroid/nodes/scoped_nodes/scoped_nodes.py

View check run for this annotation

Codecov / codecov/patch

astroid/nodes/scoped_nodes/scoped_nodes.py#L1598

Added line #L1598 was not covered by tests
class_bases.append(base)
new_class = ClassDef(
name="temporary_class",
lineno=0,
Expand Down Expand Up @@ -2825,13 +2828,8 @@ def _inferred_bases(self, context: InferenceContext | None = None):

for stmt in self.bases:
try:
# Find the first non-None inferred base value
baseobj = next(
b
for b in stmt.infer(context=context.clone())
if not (isinstance(b, node_classes.Const) and b.value is None)
)
except (InferenceError, StopIteration):
baseobj = _infer_first_not_none(stmt, context)
except InferenceError:
continue
if isinstance(baseobj, bases.Instance):
baseobj = baseobj._proxied
Expand Down
2 changes: 1 addition & 1 deletion tests/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def with_metaclass(meta, *bases):
class metaclass(meta):
def __new__(cls, name, this_bases, d):
return meta(name, bases, d)
return type.__new__(metaclass, 'temporary_class', (), {})
return type.__new__(metaclass, 'temporary_class', (), {})
import lala
Expand Down

0 comments on commit 5c0631a

Please sign in to comment.