コード例 #1
0
 def test_build_module_stubs(self):
     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
コード例 #2
0
 def test_render_typed_dict_in_list(self):
     function = FunctionDefinition.from_callable_and_traced_types(
         Dummy.an_instance_method,
         {
             'foo': List[TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int})],
             'bar': int,
         },
         int,
         None,
         existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE,
     )
     entries = [function]
     expected = '\n'.join([
         'from mypy_extensions import TypedDict',
         'from typing import List',
         '',
         '',
         'class FooTypedDict(TypedDict):',
         '    a: int',
         '',
         '',
         'class Dummy:',
         f'    def an_instance_method(self, foo: List[{make_forward_ref("FooTypedDict")}], bar: int) -> int: ...'])
     self.maxDiff = None
     assert build_module_stubs(entries)['tests.util'].render() == expected
コード例 #3
0
 def test_render_yield_typed_dict(self):
     function = FunctionDefinition.from_callable_and_traced_types(
         Dummy.an_instance_method,
         {
             'foo': int,
             'bar': int,
         },
         int,
         yield_type=TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
         existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE
     )
     entries = [function]
     expected = '\n'.join([
         'from mypy_extensions import TypedDict',
         'from typing import Generator',
         '',
         '',
         'class DummyAnInstanceMethodYieldTypedDict(TypedDict):',
         '    a: int',
         '    b: str',
         '',
         '',
         'class Dummy:',
         '    def an_instance_method(',
         '        self,',
         '        foo: int,',
         '        bar: int',
         f'    ) -> Generator[{repr_forward_ref()}'
         + '(\'DummyAnInstanceMethodYieldTypedDict\'), None, int]: ...',
     ])
     self.maxDiff = None
     assert build_module_stubs(entries)['tests.util'].render() == expected
コード例 #4
0
 def test_render_return_typed_dict(self):
     function = FunctionDefinition.from_callable_and_traced_types(
         Dummy.an_instance_method,
         {
             'foo': int,
             'bar': int,
         },
         TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
         yield_type=None,
         existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE
     )
     entries = [function]
     expected = '\n'.join([
         'from mypy_extensions import TypedDict',
         '',
         '',
         'class DummyAnInstanceMethodTypedDict(TypedDict):',
         '    a: int',
         '    b: str',
         '',
         '',
         'class Dummy:',
         '    def an_instance_method(self, foo: int, bar: int) -> \'DummyAnInstanceMethodTypedDict\': ...',
     ])
     self.maxDiff = None
     assert build_module_stubs(entries)['tests.util'].render() == expected
コード例 #5
0
 def test_from_callable_and_traced_types(self, func, arg_types,
                                         return_type, yield_type, expected):
     function = FunctionDefinition.from_callable_and_traced_types(
         func, arg_types,
         return_type, yield_type,
         existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE
     )
     assert function == expected
コード例 #6
0
class TestFunctionDefinition:
    @pytest.mark.parametrize(
        'func, expected',
        [
            (Dummy.a_static_method, False),
            (Dummy.a_class_method.__func__, True),
            (Dummy.an_instance_method, True),
            (Dummy.a_property.fget, True),
            (Dummy.a_cached_property.func, True),
            (a_module_func, False),
        ],
    )
    def test_has_self(self, func, expected):
        defn = FunctionDefinition.from_callable(func)
        assert defn.has_self == expected

    @pytest.mark.parametrize(
        'func, expected',
        [
            (Dummy.a_static_method,
             FunctionDefinition(
                 'tests.util', 'Dummy.a_static_method', FunctionKind.STATIC,
                 Signature.from_callable(Dummy.a_static_method))),
            (Dummy.a_class_method.__func__,
             FunctionDefinition(
                 'tests.util', 'Dummy.a_class_method', FunctionKind.CLASS,
                 Signature.from_callable(Dummy.a_class_method.__func__))),
            (Dummy.an_instance_method,
             FunctionDefinition(
                 'tests.util', 'Dummy.an_instance_method',
                 FunctionKind.INSTANCE,
                 Signature.from_callable(Dummy.an_instance_method))),
            (Dummy.a_property.fget,
             FunctionDefinition(
                 'tests.util', 'Dummy.a_property', FunctionKind.PROPERTY,
                 Signature.from_callable(Dummy.a_property.fget))),
            (Dummy.a_cached_property.func,
             FunctionDefinition(
                 'tests.util', 'Dummy.a_cached_property',
                 FunctionKind.DJANGO_CACHED_PROPERTY,
                 Signature.from_callable(Dummy.a_cached_property.func))),
            (a_module_func,
             FunctionDefinition('tests.test_stubs', 'a_module_func',
                                FunctionKind.MODULE,
                                Signature.from_callable(a_module_func))),
            (an_async_func,
             FunctionDefinition('tests.test_stubs',
                                'an_async_func',
                                FunctionKind.MODULE,
                                Signature.from_callable(a_module_func),
                                is_async=True)),
        ],
    )
    def test_from_callable(self, func, expected):
        defn = FunctionDefinition.from_callable(func)
        assert defn == expected
コード例 #7
0
 def test_build_module_stubs_typed_dict_parameter(self):
     function = FunctionDefinition.from_callable_and_traced_types(
         Dummy.an_instance_method,
         {
             'foo': TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
             'bar': int,
         },
         TypedDict(DUMMY_TYPED_DICT_NAME, {'c': int}),
         None,
         existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE
     )
     entries = [function]
     expected = module_stub_for_method_with_typed_dict
     self.maxDiff = None
     assert build_module_stubs(entries) == expected
コード例 #8
0
 def test_render_nested_typed_dict(self):
     function = FunctionDefinition.from_callable_and_traced_types(
         Dummy.an_instance_method,
         {
             'foo': TypedDict(DUMMY_TYPED_DICT_NAME,
                              {
                                  # Naming the key 'z' to test a class name
                                  # that comes last in alphabetical order.
                                  'z': TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
                                  'b': str,
                              }),
             'bar': int,
         },
         int,
         None,
         existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE
     )
     entries = [function]
     expected = '\n'.join([
         'from mypy_extensions import TypedDict',
         '',
         '',
         'class FooTypedDict(TypedDict):',
         '    b: str',
         # We can forward-reference a class that is defined afterwards.
         '    z: \'ZTypedDict\'',
         '',
         '',
         'class ZTypedDict(TypedDict):',
         '    a: int',
         '    b: str',
         '',
         '',
         'class Dummy:',
         '    def an_instance_method(self, foo: \'FooTypedDict\', bar: int) -> int: ...'])
     self.maxDiff = None
     assert build_module_stubs(entries)['tests.util'].render() == expected
コード例 #9
0
 def test_from_callable(self, func, expected):
     defn = FunctionDefinition.from_callable(func)
     assert defn == expected
コード例 #10
0
class TestFunctionDefinition:
    @pytest.mark.parametrize(
        'func, expected',
        [
            (Dummy.a_static_method, False),
            (Dummy.a_class_method.__func__, True),
            (Dummy.an_instance_method, True),
            (Dummy.a_property.fget, True),
            (Dummy.a_cached_property.func, True),
            (a_module_func, False),
        ],
    )
    def test_has_self(self, func, expected):
        defn = FunctionDefinition.from_callable(func)
        assert defn.has_self == expected

    @pytest.mark.parametrize(
        'func, expected',
        [
            (Dummy.a_static_method, FunctionDefinition(
                'tests.util', 'Dummy.a_static_method', FunctionKind.STATIC,
                Signature.from_callable(Dummy.a_static_method))),
            (Dummy.a_class_method.__func__, FunctionDefinition(
                'tests.util', 'Dummy.a_class_method', FunctionKind.CLASS,
                Signature.from_callable(Dummy.a_class_method.__func__))),
            (Dummy.an_instance_method, FunctionDefinition(
                'tests.util', 'Dummy.an_instance_method', FunctionKind.INSTANCE,
                Signature.from_callable(Dummy.an_instance_method))),
            (Dummy.a_property.fget, FunctionDefinition(
                'tests.util', 'Dummy.a_property', FunctionKind.PROPERTY,
                Signature.from_callable(Dummy.a_property.fget))),
            (Dummy.a_cached_property.func, FunctionDefinition(
                'tests.util', 'Dummy.a_cached_property', FunctionKind.DJANGO_CACHED_PROPERTY,
                Signature.from_callable(Dummy.a_cached_property.func))),
            (a_module_func, FunctionDefinition(
                'tests.test_stubs', 'a_module_func', FunctionKind.MODULE,
                Signature.from_callable(a_module_func))),
            (an_async_func, FunctionDefinition(
                'tests.test_stubs', 'an_async_func', FunctionKind.MODULE,
                Signature.from_callable(a_module_func), is_async=True)),
        ],
    )
    def test_from_callable(self, func, expected):
        defn = FunctionDefinition.from_callable(func)
        assert defn == expected

    @pytest.mark.parametrize(
        'func, arg_types, return_type, yield_type, expected',
        [
            # Non-TypedDict case.
            (
                Dummy.an_instance_method,
                {'foo': int, 'bar': List[str]},
                int,
                None,
                FunctionDefinition(
                    'tests.util',
                    'Dummy.an_instance_method',
                    FunctionKind.INSTANCE,
                    Signature(
                        parameters=[
                            Parameter(name='self', kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=Parameter.empty),
                            Parameter(name='foo', kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=int),
                            Parameter(name='bar', kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=List[str]),
                        ],
                        return_annotation=int,
                    ),
                    False,
                    [],
                )
            ),
            # TypedDict: Add class definitions and use the class names as types.
            (
                Dummy.an_instance_method,
                {
                    'foo': TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
                    'bar': TypedDict(DUMMY_TYPED_DICT_NAME, {'c': int}),
                },
                int,
                None,
                FunctionDefinition(
                    'tests.util',
                    'Dummy.an_instance_method',
                    FunctionKind.INSTANCE,
                    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', kind=Parameter.POSITIONAL_OR_KEYWORD,
                                      annotation=make_forward_ref('BarTypedDict')),
                        ],
                        return_annotation=int,
                    ),
                    False,
                    [
                        ClassStub(
                            name='FooTypedDict(TypedDict)',
                            function_stubs=[],
                            attribute_stubs=[
                                AttributeStub('a', int),
                                AttributeStub('b', str),
                            ]
                        ),
                        ClassStub(
                            name='BarTypedDict(TypedDict)',
                            function_stubs=[],
                            attribute_stubs=[
                                AttributeStub('c', int),
                            ]
                        ),
                    ],
                )
            ),
        ],
    )
    def test_from_callable_and_traced_types(self, func, arg_types,
                                            return_type, yield_type, expected):
        function = FunctionDefinition.from_callable_and_traced_types(
            func, arg_types,
            return_type, yield_type,
            existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE
        )
        assert function == expected