File tree Expand file tree Collapse file tree 3 files changed +23
-3
lines changed Expand file tree Collapse file tree 3 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -109,6 +109,11 @@ Release date: TBA
109
109
110
110
Closes PyCQA/pylint#7092
111
111
112
+ * Prevent creating ``Instance`` objects that proxy other ``Instance``s when there is
113
+ ambiguity (or user error) in calling ``__new__(cls)``.
114
+
115
+ Refs PyCQA/pylint#7109
116
+
112
117
113
118
What's New in astroid 2.11.6?
114
119
=============================
Original file line number Diff line number Diff line change @@ -293,6 +293,9 @@ class Instance(BaseInstance):
293
293
# pylint: disable=unnecessary-lambda
294
294
special_attributes = lazy_descriptor (lambda : objectmodel .InstanceModel ())
295
295
296
+ def __init__ (self , proxied : nodes .ClassDef ) -> None :
297
+ super ().__init__ (proxied )
298
+
296
299
def __repr__ (self ):
297
300
return "<Instance of {}.{} at 0x{}>" .format (
298
301
self ._proxied .root ().name , self ._proxied .name , id (self )
@@ -434,9 +437,12 @@ def _infer_builtin_new(
434
437
return
435
438
436
439
node_context = context .extra_context .get (caller .args [0 ])
437
- infer = caller .args [0 ].infer (context = node_context )
438
-
439
- yield from (Instance (x ) if x is not Uninferable else x for x in infer ) # type: ignore[misc,arg-type]
440
+ for inferred in caller .args [0 ].infer (context = node_context ):
441
+ if inferred is Uninferable :
442
+ yield inferred
443
+ if isinstance (inferred , nodes .ClassDef ):
444
+ yield Instance (inferred )
445
+ raise InferenceError
440
446
441
447
def bool_value (self , context = None ):
442
448
return True
Original file line number Diff line number Diff line change @@ -3799,6 +3799,15 @@ def test_builtin_new() -> None:
3799
3799
with pytest .raises (InferenceError ):
3800
3800
next (ast_node4 .infer ())
3801
3801
3802
+ ast_node5 = extract_node (
3803
+ """
3804
+ class A: pass
3805
+ A.__new__(A()) #@
3806
+ """
3807
+ )
3808
+ with pytest .raises (InferenceError ):
3809
+ next (ast_node5 .infer ())
3810
+
3802
3811
@pytest .mark .xfail (reason = "Does not support function metaclasses" )
3803
3812
def test_function_metaclasses (self ):
3804
3813
# These are not supported right now, although
You can’t perform that action at this time.
0 commit comments