Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_nested_callee_throws_recovers(self, collector):
with trace_calls(collector):
nested_throw(should_recover=True)
expected = [
CallTrace(throw, {'should_recover': bool}, NoneType),
CallTrace(nested_throw, {'should_recover': bool}, str),
]
assert collector.traces == expected
def test_get_diff2(store, db_file, stdout, stderr):
traces = [
CallTrace(super_long_function_with_long_params, {
'long_param1': str,
'long_param2': str,
'long_param3': int,
'long_param4': str,
'long_param5': int,
}, None),
CallTrace(func_anno, {'a': int, 'b': int}, int),
]
store.add(traces)
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
ret = cli.main(['stub', func.__module__, '--diff'], stdout, stderr)
expected = """- def func_anno(a: int, b: str) -> None: ...
? ^ - ^^ ^
+ def func_anno(a: int, b: int) -> int: ...
? ^^ ^ ^
def super_long_function_with_long_params(
long_param1: str,
long_param2: str,
- long_param3: str,
? ^ -
+ long_param3: int,
def test_get_diff(store, db_file, stdout, stderr):
traces = [
CallTrace(func_anno, {'a': int, 'b': int}, int),
CallTrace(func_anno2, {'a': str, 'b': str}, None),
]
store.add(traces)
with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
ret = cli.main(['stub', func.__module__, '--diff'], stdout, stderr)
expected = """- def func_anno(a: int, b: str) -> None: ...
? ^ - ^^ ^
+ def func_anno(a: int, b: int) -> int: ...
? ^^ ^ ^
"""
assert stdout.getvalue() == expected
assert stderr.getvalue() == ''
assert ret == 0
def test_log_failure_and_continue(self, caplog):
traces = [
CallTrace(dummy_func, {'a': int, 'b': int}, int),
CallTrace(object(), {}), # object() will fail to serialize
CallTrace(dummy_func, {'a': str, 'b': str}, str),
]
rows = list(serialize_traces(traces))
expected = [
CallTraceRow.from_trace(traces[0]),
CallTraceRow.from_trace(traces[2]),
]
assert rows == expected
assert [r.msg for r in caplog.records] == ["Failed to serialize trace"]
def test_trace_round_trip(self):
trace = CallTrace(dummy_func, {'a': int, 'b': int}, int)
assert CallTraceRow.from_trace(trace).to_trace() == trace
def test_ignore_non_matching_functions(self):
b = StubIndexBuilder('foo.bar')
b.log(CallTrace(untyped_helper, {'x': int, 'y': str}))
assert len(b.index) == 0
def to_trace(self) -> CallTrace:
function = get_func_in_module(self.module, self.qualname)
arg_types = arg_types_from_json(self.arg_types)
return_type = maybe_decode_type(type_from_json, self.return_type)
yield_type = maybe_decode_type(type_from_json, self.yield_type)
return CallTrace(function, arg_types, return_type, yield_type)
def __init__(
self,
logger: CallTraceLogger,
code_filter: Optional[CodeFilter] = None,
sample_rate: Optional[int] = None,
max_typed_dict_size: Optional[int] = None,
) -> None:
self.logger = logger
self.traces: Dict[FrameType, CallTrace] = {}
self.sample_rate = sample_rate
self.cache: Dict[CodeType, Optional[Callable]] = {}
self.should_trace = code_filter
self.max_typed_dict_size = max_typed_dict_size