Skip to content

Prevent creating Instance that proxies another Instance when inferring __new__(cls) #1682

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

Merged
merged 6 commits into from
Jul 2, 2022
Merged
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
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ Release date: TBA

Closes PyCQA/pylint#7092

* Prevent creating ``Instance`` objects that proxy other ``Instance``s when there is
ambiguity (or user error) in calling ``__new__(cls)``.

Refs PyCQA/pylint#7109


What's New in astroid 2.11.6?
=============================
Expand Down
12 changes: 9 additions & 3 deletions astroid/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ class Instance(BaseInstance):
# pylint: disable=unnecessary-lambda
special_attributes = lazy_descriptor(lambda: objectmodel.InstanceModel())

def __init__(self, proxied: nodes.ClassDef) -> None:
super().__init__(proxied)

def __repr__(self):
return "<Instance of {}.{} at 0x{}>".format(
self._proxied.root().name, self._proxied.name, id(self)
Expand Down Expand Up @@ -434,9 +437,12 @@ def _infer_builtin_new(
return

node_context = context.extra_context.get(caller.args[0])
infer = caller.args[0].infer(context=node_context)

yield from (Instance(x) if x is not Uninferable else x for x in infer) # type: ignore[misc,arg-type]
for inferred in caller.args[0].infer(context=node_context):
if inferred is Uninferable:
yield inferred
if isinstance(inferred, nodes.ClassDef):
yield Instance(inferred)
raise InferenceError

def bool_value(self, context=None):
return True
Expand Down
9 changes: 9 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -3799,6 +3799,15 @@ def test_builtin_new() -> None:
with pytest.raises(InferenceError):
next(ast_node4.infer())

ast_node5 = extract_node(
"""
class A: pass
A.__new__(A()) #@
"""
)
with pytest.raises(InferenceError):
next(ast_node5.infer())

@pytest.mark.xfail(reason="Does not support function metaclasses")
def test_function_metaclasses(self):
# These are not supported right now, although
Expand Down