How to use MonkeyType - 10 common examples

To help you get started, we’ve selected a few MonkeyType examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Instagram / MonkeyType / tests / test_stubs.py View on Github external
def test_optional_parameter_annotation(self):
        """Optional should always be included in parameter annotations, even if the default value is None"""
        stub = FunctionStub('test', inspect.signature(has_optional_param), FunctionKind.MODULE)
        expected = 'def test(x: Optional[int] = ...) -> None: ...'
        assert stub.render() == expected
github Instagram / MonkeyType / tests / test_stubs.py View on Github external
def test_default_none_parameter_annotation(self):
        stub = FunctionStub('test', inspect.signature(default_none_parameter), FunctionKind.MODULE)
        expected = 'def test(x: Optional[int] = ...) -> None: ...'
        assert stub.render() == expected
github Instagram / MonkeyType / tests / test_stubs.py View on Github external
def test_newtype_parameter_annotation(self):
        stub = FunctionStub('test', inspect.signature(has_newtype_param), FunctionKind.MODULE)
        expected = 'def test(user_id: UserId) -> None: ...'
        assert stub.render() == expected
github Instagram / MonkeyType / tests / test_cli.py View on Github external
def test_apply_stub_init(store, db_file, stdout, stderr, collector):
    """Regression test for applying stubs to testmodule/__init__.py style module layout"""
    with trace_calls(collector):
        func_foo()

    store.add(collector.traces)

    with mock.patch.dict(os.environ, {DefaultConfig.DB_PATH_VAR: db_file.name}):
        ret = cli.main(['apply', Foo.__module__], stdout, stderr)

    assert ret == 0
    assert 'warning:' not in stdout.getvalue()
github Instagram / MonkeyType / tests / test_stubs.py View on Github external
def test_nonetype_annotation(self):
        """NoneType should always be rendered as None"""
        sig = Signature.from_callable(UpdateSignatureHelper.has_annos)
        sig = update_signature_args(sig, {'a': Dict[str, NoneType]}, has_self=False,
                                    existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE)
        stub = FunctionStub('test', sig, FunctionKind.MODULE)
        expected = 'def test(a: Dict[str, None], b) -> int: ...'
        assert stub.render() == expected
github Instagram / MonkeyType / tests / test_cli.py View on Github external
def test_generate_stub(store, db_file, stdout, stderr):
    traces = [
        CallTrace(func, {'a': int, 'b': str}, NoneType),
        CallTrace(func2, {'a': int, 'b': int}, NoneType),
    ]
    store.add(traces)
    ret = cli.main(['stub', func.__module__], stdout, stderr)
    expected = """def func(a: int, b: str) -> None: ...


def func2(a: int, b: int) -> None: ...
"""
    assert stdout.getvalue() == expected
    assert stderr.getvalue() == ''
    assert ret == 0
github Instagram / MonkeyType / tests / test_cli.py View on Github external
def test_verbose_failed_traces(store, db_file, stdout, stderr):
    traces = [
        CallTrace(func, {'a': int, 'b': str}, NoneType),
        CallTrace(func2, {'a': int, 'b': int}, NoneType),
    ]
    store.add(traces)
    with mock.patch("monkeytype.encoding.CallTraceRow.to_trace", side_effect=MonkeyTypeError("the-trace")):
        ret = cli.main(['-v', 'stub', func.__module__], stdout, stderr)
    assert "WARNING: Failed decoding trace: the-trace" in stderr.getvalue()
    assert ret == 0
github Instagram / MonkeyType / tests / test_tracing.py View on Github external
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
github Instagram / MonkeyType / tests / test_cli.py View on Github external
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,
github Instagram / MonkeyType / tests / test_cli.py View on Github external
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