code: def test_heapset_sorted_flag_left():
      heap = HeapSet(key=operator.attrgetter("i"))
      assert heap._sorted
      c1 = C("1", 1)
      c2 = C("2", 2)
      c3 = C("3", 3)
      c4 = C("4", 4)
  
      heap.add(c4)
      assert not heap._sorted
      heap.add(c3)
      heap.add(c2)
      heap.add(c1)
  
      list(heap.sorted())
      assert_heap_sorted(heap)
  
      # `peek` maintains sort if first element is not discarded
      assert heap.peek() is c1
      assert_heap_sorted(heap)
  
      # `pop` always de-sorts
      assert heap.pop() is c1
      assert not heap._sorted
  
      list(heap.sorted())
  
      # discard first element
      heap.discard(c2)
      assert heap.peek() is c3
      assert not heap._sorted
  
      # popping the last element resets the sorted flag
      assert heap.pop() is c3
      assert heap.pop() is c4
      assert not heap
      assert_heap_sorted(heap)
  
      # discarding`` the last element resets the sorted flag
      heap.add(c1)
      heap.add(c2)
      assert not heap._sorted
      heap.discard(c1)
      assert not heap._sorted
      heap.discard(c2)
      assert not heap
      assert_heap_sorted(heap)
  
imports: from __future__ import annotations
  import heapq
  import operator
  import pickle
  import random
  from collections.abc import Mapping
  import pytest
  from distributed.collections import LRU, HeapSet, sum_mappings
label_desc: Use image node_id 4 for calling a global function with example usage: test_heapset_sorted_flag_left() without return types