@@ -1346,6 +1346,7 @@ def safe_infer(
1346
1346
context : InferenceContext | None = None ,
1347
1347
* ,
1348
1348
compare_constants : bool = False ,
1349
+ compare_constructors : bool = False ,
1349
1350
) -> InferenceResult | None :
1350
1351
"""Return the inferred value for the given node.
1351
1352
@@ -1354,6 +1355,9 @@ def safe_infer(
1354
1355
1355
1356
If compare_constants is True and if multiple constants are inferred,
1356
1357
unequal inferred values are also considered ambiguous and return None.
1358
+
1359
+ If compare_constructors is True and if multiple classes are inferred,
1360
+ constructors with different signatures are held ambiguous and return None.
1357
1361
"""
1358
1362
inferred_types : set [str | None ] = set ()
1359
1363
try :
@@ -1386,6 +1390,13 @@ def safe_infer(
1386
1390
and function_arguments_are_ambiguous (inferred , value )
1387
1391
):
1388
1392
return None
1393
+ if (
1394
+ compare_constructors
1395
+ and isinstance (inferred , nodes .ClassDef )
1396
+ and isinstance (value , nodes .ClassDef )
1397
+ and class_constructors_are_ambiguous (inferred , value )
1398
+ ):
1399
+ return None
1389
1400
except astroid .InferenceError :
1390
1401
return None # There is some kind of ambiguity
1391
1402
except StopIteration :
@@ -1434,6 +1445,21 @@ def function_arguments_are_ambiguous(
1434
1445
return False
1435
1446
1436
1447
1448
+ def class_constructors_are_ambiguous (
1449
+ class1 : nodes .ClassDef , class2 : nodes .ClassDef
1450
+ ) -> bool :
1451
+ try :
1452
+ constructor1 = class1 .local_attr ("__init__" )[0 ]
1453
+ constructor2 = class2 .local_attr ("__init__" )[0 ]
1454
+ except astroid .NotFoundError :
1455
+ return False
1456
+ if not isinstance (constructor1 , nodes .FunctionDef ):
1457
+ return False
1458
+ if not isinstance (constructor2 , nodes .FunctionDef ):
1459
+ return False
1460
+ return function_arguments_are_ambiguous (constructor1 , constructor2 )
1461
+
1462
+
1437
1463
def has_known_bases (
1438
1464
klass : nodes .ClassDef , context : InferenceContext | None = None
1439
1465
) -> bool :
0 commit comments