def assertCollected(self, ref: ReferenceType[T], msg=None):
        """Check that the object referenced by `ref` has been garbage
        collected.

        Optionally prints out the reference path to the object if it was not
        garbage collected. This is enabled with the `WITH_REF_PATH` environment
        variable.
        """

        caller_frame = python_utils.caller_frame(offset=0)
        alls = [
            caller_frame.f_builtins,
            caller_frame.f_globals,
            caller_frame.f_locals,
        ]

        gc.collect()

        if msg is None:
            msg = f"Object {ref} was not garbage collected."

        # obj = ref()

        with self.subTest(part="garbage collection"):
            self.assertTrue(ref() is None, msg)

        # Enable WITH_REF_PATH to see printout of why the given ref was not
        # GC-ed.
        if (
            ref() is not None
            and os.environ.get(WITH_REF_PATH_VAR, None) is not None
        ):
            with self.subTest(part="reference path"):
                print("Trying to find path to non-collected object.")
                path = test_utils.find_path(id(alls), id(ref()))

                if path is None:
                    print(
                        "Couldn't find reference path. Looking at immediate referrers instead:"
                    )
                    for referent in gc.get_referrers(ref()):
                        print(
                            "  ",
                            python_utils.class_name(type(referent)),
                            str(referent)[0:128],
                        )

                else:
                    print(f"Reference path from test frame to {ref}:")
                    test_utils.print_referent_lens(origin=alls, lens=path)
Check that the object referenced by `ref` has been garbage
        collected.

        Optionally prints out the reference path to the object if it was not
        garbage collected. This is enabled with the `WITH_REF_PATH` environment
        variable.
        
tests/test.py