@@ -5488,6 +5488,7 @@ def _create_basic_elements(
5488
5488
"""Create a list of nodes to function as the elements of a new node."""
5489
5489
elements : list [NodeNG ] = []
5490
5490
for element in value :
5491
+ # NOTE: avoid accessing any attributes of element in the loop.
5491
5492
element_node = const_factory (element )
5492
5493
element_node .parent = node
5493
5494
elements .append (element_node )
@@ -5500,6 +5501,7 @@ def _create_dict_items(
5500
5501
"""Create a list of node pairs to function as the items of a new dict node."""
5501
5502
elements : list [tuple [SuccessfulInferenceResult , SuccessfulInferenceResult ]] = []
5502
5503
for key , value in values .items ():
5504
+ # NOTE: avoid accessing any attributes of both key and value in the loop.
5503
5505
key_node = const_factory (key )
5504
5506
key_node .parent = node
5505
5507
value_node = const_factory (value )
@@ -5510,18 +5512,23 @@ def _create_dict_items(
5510
5512
5511
5513
def const_factory (value : Any ) -> ConstFactoryResult :
5512
5514
"""Return an astroid node for a python value."""
5513
- assert not isinstance (value , NodeNG )
5515
+ # NOTE: avoid accessing any attributes of value until it is known that value
5516
+ # is of a const type, to avoid possibly triggering code for a live object.
5517
+ # Accesses include value.__class__ and isinstance(value, ...), but not type(value).
5518
+ # See: https://github.com/pylint-dev/astroid/issues/2686
5519
+ value_type = type (value )
5520
+ assert not issubclass (value_type , NodeNG )
5514
5521
5515
5522
# This only handles instances of the CONST types. Any
5516
5523
# subclasses get inferred as EmptyNode.
5517
5524
# TODO: See if we should revisit these with the normal builder.
5518
- if value . __class__ not in CONST_CLS :
5525
+ if value_type not in CONST_CLS :
5519
5526
node = EmptyNode ()
5520
5527
node .object = value
5521
5528
return node
5522
5529
5523
5530
instance : List | Set | Tuple | Dict
5524
- initializer_cls = CONST_CLS [value . __class__ ]
5531
+ initializer_cls = CONST_CLS [value_type ]
5525
5532
if issubclass (initializer_cls , (List , Set , Tuple )):
5526
5533
instance = initializer_cls (
5527
5534
lineno = None ,
0 commit comments