How to use the monkeytype.stubs.ModuleStub function in MonkeyType

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
FunctionDefinition.from_callable(Dummy.a_static_method),
            FunctionDefinition.from_callable(Dummy.a_class_method.__func__),
            FunctionDefinition.from_callable(Dummy.an_instance_method),
            FunctionDefinition.from_callable(simple_add),
        ]
        simple_add_stub = _func_stub_from_callable(simple_add)
        to_strip = ['typing']
        dummy_stub = ClassStub('Dummy', function_stubs=[
            _func_stub_from_callable(Dummy.a_class_method.__func__, to_strip),
            _func_stub_from_callable(Dummy.an_instance_method, to_strip),
            _func_stub_from_callable(Dummy.a_static_method, to_strip),
        ])
        imports = {'typing': {'Any', 'Optional'}}
        expected = {
            'tests.test_stubs': ModuleStub(function_stubs=[simple_add_stub]),
            'tests.util': ModuleStub(class_stubs=[dummy_stub], imports_stub=ImportBlockStub(imports)),
        }
        self.maxDiff = None
        assert build_module_stubs(entries) == expected
github Instagram / MonkeyType / tests / test_stubs.py View on Github external
def test_render(self):
        cm_stub = _func_stub_from_callable(Dummy.a_class_method)
        im_stub = _func_stub_from_callable(Dummy.an_instance_method)
        func_stubs = (cm_stub, im_stub)
        test_stub = ClassStub('Test', function_stubs=func_stubs)
        test2_stub = ClassStub('Test2', function_stubs=func_stubs)
        other_class_stubs = module_stub_for_method_with_typed_dict['tests.util'].class_stubs.values()
        class_stubs = (*other_class_stubs, test_stub, test2_stub)
        typed_dict_class_stubs = module_stub_for_method_with_typed_dict['tests.util'].typed_dict_class_stubs
        mod_stub = ModuleStub(function_stubs=func_stubs,
                              class_stubs=class_stubs,
                              typed_dict_class_stubs=typed_dict_class_stubs)
        expected = '\n'.join([
            'class DummyAnInstanceMethodTypedDict(TypedDict):',
            '    c: int',
            '',
            '',
            'class FooTypedDict(TypedDict):',
            '    a: int',
            '    b: str',
            '',
            '',
            '@classmethod',
            'def a_class_method(foo: Any) -> Optional[frame]: ...',
            '',
            '',
github Instagram / MonkeyType / tests / test_stubs.py View on Github external
TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
                [ClassStub(name='FooBar(TypedDict)', function_stubs=[], attribute_stubs=[
                    AttributeStub(name='a', typ=int),
                    AttributeStub(name='b', typ=str),
                ])],
            ),
        ],
    )
    def test_stubs_from_typed_dict(self, typ, expected):
        assert ClassStub.stubs_from_typed_dict(typ, class_name='FooBar') == expected


typed_dict_import_map = ImportMap()
typed_dict_import_map['mypy_extensions'] = {'TypedDict'}
module_stub_for_method_with_typed_dict = {
    'tests.util': ModuleStub(
        function_stubs=(),
        class_stubs=[
            ClassStub(
                name='Dummy',
                function_stubs=[
                    FunctionStub(
                        name='an_instance_method',
                        signature=Signature(
                            parameters=[
                                Parameter(name='self',
                                          kind=Parameter.POSITIONAL_OR_KEYWORD,
                                          annotation=Parameter.empty),
                                Parameter(name='foo',
                                          kind=Parameter.POSITIONAL_OR_KEYWORD,
                                          annotation=make_forward_ref('FooTypedDict')),
                                Parameter(name='bar',
github Instagram / MonkeyType / tests / test_stubs.py View on Github external
def test_build_index(self):
        idxb = StubIndexBuilder('tests')
        idxb.log(CallTrace(untyped_helper, {'x': int, 'y': str}, str))
        sig = Signature.from_callable(untyped_helper)
        sig = sig.replace(
            parameters=[
                Parameter('x', Parameter.POSITIONAL_OR_KEYWORD, annotation=int),
                Parameter('y', Parameter.POSITIONAL_OR_KEYWORD, annotation=str),
            ],
            return_annotation=str
        )
        mod_stub = ModuleStub(function_stubs=[FunctionStub('untyped_helper', sig, FunctionKind.MODULE)])
        expected = {'tests.test_stubs': mod_stub}
        assert idxb.get_stubs() == expected
github Instagram / MonkeyType / tests / test_stubs.py View on Github external
entries = [
            FunctionDefinition.from_callable(Dummy.a_static_method),
            FunctionDefinition.from_callable(Dummy.a_class_method.__func__),
            FunctionDefinition.from_callable(Dummy.an_instance_method),
            FunctionDefinition.from_callable(simple_add),
        ]
        simple_add_stub = _func_stub_from_callable(simple_add)
        to_strip = ['typing']
        dummy_stub = ClassStub('Dummy', function_stubs=[
            _func_stub_from_callable(Dummy.a_class_method.__func__, to_strip),
            _func_stub_from_callable(Dummy.an_instance_method, to_strip),
            _func_stub_from_callable(Dummy.a_static_method, to_strip),
        ])
        imports = {'typing': {'Any', 'Optional'}}
        expected = {
            'tests.test_stubs': ModuleStub(function_stubs=[simple_add_stub]),
            'tests.util': ModuleStub(class_stubs=[dummy_stub], imports_stub=ImportBlockStub(imports)),
        }
        self.maxDiff = None
        assert build_module_stubs(entries) == expected
github Instagram / MonkeyType / monkeytype / stubs.py View on Github external
def build_module_stubs(entries: Iterable[FunctionDefinition]) -> Dict[str, ModuleStub]:
    """Given an iterable of function definitions, build the corresponding stubs"""
    mod_stubs: Dict[str, ModuleStub] = {}
    for entry in entries:
        path = entry.qualname.split('.')
        name = path.pop()
        class_path = path
        # TODO: Handle nested classes
        klass = None
        if len(class_path) > 0:
            klass = '.'.join(class_path)
        if entry.module not in mod_stubs:
            mod_stubs[entry.module] = ModuleStub()
        mod_stub = mod_stubs[entry.module]
        imports = get_imports_for_signature(entry.signature)
        # Import TypedDict, if needed.
        if entry.typed_dict_class_stubs:
            imports['mypy_extensions'].add('TypedDict')
        func_stub = FunctionStub(name, entry.signature, entry.kind, list(imports.keys()), entry.is_async)
github Instagram / MonkeyType / monkeytype / stubs.py View on Github external
def build_module_stubs(entries: Iterable[FunctionDefinition]) -> Dict[str, ModuleStub]:
    """Given an iterable of function definitions, build the corresponding stubs"""
    mod_stubs: Dict[str, ModuleStub] = {}
    for entry in entries:
        path = entry.qualname.split('.')
        name = path.pop()
        class_path = path
        # TODO: Handle nested classes
        klass = None
        if len(class_path) > 0:
            klass = '.'.join(class_path)
        if entry.module not in mod_stubs:
            mod_stubs[entry.module] = ModuleStub()
        mod_stub = mod_stubs[entry.module]
        imports = get_imports_for_signature(entry.signature)
        # Import TypedDict, if needed.
        if entry.typed_dict_class_stubs:
            imports['mypy_extensions'].add('TypedDict')
        func_stub = FunctionStub(name, entry.signature, entry.kind, list(imports.keys()), entry.is_async)
        # Don't need to import anything from the same module
        imports.pop(entry.module, None)
        mod_stub.imports_stub.imports.merge(imports)
        if klass is not None:
            if klass not in mod_stub.class_stubs:
                mod_stub.class_stubs[klass] = ClassStub(klass)
            class_stub = mod_stub.class_stubs[klass]
            class_stub.function_stubs[func_stub.name] = func_stub
        else:
            mod_stub.function_stubs[func_stub.name] = func_stub