# -*- coding: utf-8 -*- import logging import pytest from test.utils import assert_bool, make_safety_fixture, skipif_known_failing logging.basicConfig(level=logging.ERROR) # Reset dependency graph before each test _safety_fixture, _safety_state, run_cell_ = make_safety_fixture() def run_cell(cell): # print() # print('*******************************************') # print('running', cell) # print('*******************************************') # print() run_cell_(cell) def stale_detected(): return _safety_state[0].test_and_clear_detected_flag() def assert_detected(msg=''): assert_bool(stale_detected(), msg=msg) def assert_detected_if_full_propagation(msg=''):
# -*- coding: utf-8 -*- import logging from typing import Optional, Set from nbsafety.data_model.data_symbol import DataSymbol from nbsafety.singletons import nbs from test.utils import assert_bool, make_safety_fixture, skipif_known_failing logging.basicConfig(level=logging.ERROR) # Reset dependency graph before each test _safety_fixture, run_cell_ = make_safety_fixture( setup_cells=["from test.test_nested_symbols import DotDict"]) run_cell = run_cell_ def stale_detected(): return nbs().test_and_clear_detected_flag() def assert_detected(msg=""): assert_bool(stale_detected(), msg=msg) def assert_not_detected(msg=""): assert_bool(not stale_detected(), msg=msg) def lookup_symbols(val) -> Optional[Set[DataSymbol]]: safety = nbs() alias_set = {
# -*- coding: utf-8 -*- import logging from nbsafety.data_model.data_symbol import DataSymbol from nbsafety.singletons import nbs from test.utils import make_safety_fixture, skipif_known_failing logging.basicConfig(level=logging.ERROR) # Reset dependency graph before each test _safety_fixture, run_cell = make_safety_fixture() def lookup_symbol(name: str) -> DataSymbol: ret = nbs().global_scope.lookup_data_symbol_by_name_this_indentation(name) assert ret is not None, "got None for %s" % name return ret def test_basic(): run_cell("x = object()") assert lookup_symbol("x").get_ref_count() == 1 run_cell("y = x") assert lookup_symbol("x").get_ref_count() == 2 assert lookup_symbol("y").get_ref_count() == 2 run_cell("del x") assert lookup_symbol("y").get_ref_count() == 1 run_cell("y = None") # None has special semantics as it can mean that the symbol was gc'd # Right now (28/04/2021, hash 9099347) this isn't used anywhere but # that may change.
# -*- coding: utf-8 -*- import logging from typing import Set from nbsafety.data_model.code_cell import cells from nbsafety.singletons import nbs from nbsafety.types import CellId from test.utils import make_safety_fixture, skipif_known_failing logging.basicConfig(level=logging.ERROR) # Reset dependency graph before each test # _safety_fixture, run_cell_ = make_safety_fixture(trace_messages_enabled=True) _safety_fixture, run_cell_ = make_safety_fixture(mark_typecheck_failures_unsafe=True) def run_cell(cell, cell_id=None, **kwargs): """Mocks the `change active cell` portion of the comm protocol""" if cell_id is not None: nbs().handle({"type": "change_active_cell", "active_cell_id": cell_id}) run_cell_(cell, **kwargs) def get_cell_ids_needing_typecheck() -> Set[CellId]: return { cell.cell_id for cell in cells().all_cells_most_recently_run_for_each_id() if cell.needs_typecheck }