@@ -5488,6 +5488,7 @@ def _create_basic_elements(
54885488 """Create a list of nodes to function as the elements of a new node."""
54895489 elements : list [NodeNG ] = []
54905490 for element in value :
5491+ # NOTE: avoid accessing any attributes of element in the loop.
54915492 element_node = const_factory (element )
54925493 element_node .parent = node
54935494 elements .append (element_node )
@@ -5500,6 +5501,7 @@ def _create_dict_items(
55005501 """Create a list of node pairs to function as the items of a new dict node."""
55015502 elements : list [tuple [SuccessfulInferenceResult , SuccessfulInferenceResult ]] = []
55025503 for key , value in values .items ():
5504+ # NOTE: avoid accessing any attributes of both key and value in the loop.
55035505 key_node = const_factory (key )
55045506 key_node .parent = node
55055507 value_node = const_factory (value )
@@ -5510,18 +5512,23 @@ def _create_dict_items(
55105512
55115513def const_factory (value : Any ) -> ConstFactoryResult :
55125514 """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 )
55145521
55155522 # This only handles instances of the CONST types. Any
55165523 # subclasses get inferred as EmptyNode.
55175524 # 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 :
55195526 node = EmptyNode ()
55205527 node .object = value
55215528 return node
55225529
55235530 instance : List | Set | Tuple | Dict
5524- initializer_cls = CONST_CLS [value . __class__ ]
5531+ initializer_cls = CONST_CLS [value_type ]
55255532 if issubclass (initializer_cls , (List , Set , Tuple )):
55265533 instance = initializer_cls (
55275534 lineno = None ,
0 commit comments