Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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=[],
'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',
kind=Parameter.POSITIONAL_OR_KEYWORD,
annotation=int),
],
return_annotation=make_forward_ref('DummyAnInstanceMethodTypedDict'),
),
kind=FunctionKind.INSTANCE,
strip_modules=['mypy_extensions'],
is_async=False,
),
],
),
],
imports_stub=ImportBlockStub(typed_dict_import_map),
typed_dict_class_stubs=[
ClassStub(
(AttributeStub(name='foo', typ=make_forward_ref('Foo')), ' foo: \'Foo\''),
],
)
def test_simple_attribute(self, stub, expected):
assert stub.render(' ') == expected
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',
kind=Parameter.POSITIONAL_OR_KEYWORD,
annotation=int),
],
return_annotation=make_forward_ref('DummyAnInstanceMethodTypedDict'),
),
kind=FunctionKind.INSTANCE,
strip_modules=['mypy_extensions'],
is_async=False,
),
],
),
],
imports_stub=ImportBlockStub(typed_dict_import_map),
typed_dict_class_stubs=[
ClassStub(
name='FooTypedDict(TypedDict)',
function_stubs=[],
attribute_stubs=[
AttributeStub('a', int),
AttributeStub('b', str),
def stubs_from_typed_dict(type_dict: type, class_name: str) -> List['ClassStub']:
"""Return a list of class stubs for all TypedDicts found within `type_dict`."""
assert is_anonymous_typed_dict(type_dict)
class_stubs = []
attribute_stubs = []
for name, typ in type_dict.__annotations__.items():
if is_anonymous_typed_dict(typ):
_class_name = get_typed_dict_class_name(name)
class_stubs.extend(ClassStub.stubs_from_typed_dict(typ, _class_name))
typ = make_forward_ref(_class_name)
attribute_stubs.append(AttributeStub(name, typ))
class_stubs.append(ClassStub(name=f'{class_name}(TypedDict)',
function_stubs=[],
attribute_stubs=attribute_stubs))
return class_stubs
def from_callable_and_traced_types(
cls,
func: Callable,
arg_types: Dict[str, type],
return_type: Optional[type],
yield_type: Optional[type],
existing_annotation_strategy: ExistingAnnotationStrategy = ExistingAnnotationStrategy.REPLICATE,
) -> 'FunctionDefinition':
typed_dict_class_stubs: List[ClassStub] = []
new_arg_types = {}
for name, typ in arg_types.items():
if is_anonymous_typed_dict(typ):
class_name = get_typed_dict_class_name(name)
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(typ, class_name))
typ = make_forward_ref(class_name)
new_arg_types[name] = typ
if return_type and is_anonymous_typed_dict(return_type):
# Replace the dot in a qualified name.
class_name = get_typed_dict_class_name(func.__qualname__.replace('.', '_'))
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(return_type, class_name))
return_type = make_forward_ref(class_name)
if yield_type and is_anonymous_typed_dict(yield_type):
# Replace the dot in a qualified name.
class_name = get_typed_dict_class_name(func.__qualname__.replace('.', '_') + 'Yield')
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(yield_type, class_name))
yield_type = make_forward_ref(class_name)
function = FunctionDefinition.from_callable(func)
signature = function.signature
existing_annotation_strategy: ExistingAnnotationStrategy = ExistingAnnotationStrategy.REPLICATE,
) -> 'FunctionDefinition':
typed_dict_class_stubs: List[ClassStub] = []
new_arg_types = {}
for name, typ in arg_types.items():
if is_anonymous_typed_dict(typ):
class_name = get_typed_dict_class_name(name)
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(typ, class_name))
typ = make_forward_ref(class_name)
new_arg_types[name] = typ
if return_type and is_anonymous_typed_dict(return_type):
# Replace the dot in a qualified name.
class_name = get_typed_dict_class_name(func.__qualname__.replace('.', '_'))
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(return_type, class_name))
return_type = make_forward_ref(class_name)
if yield_type and is_anonymous_typed_dict(yield_type):
# Replace the dot in a qualified name.
class_name = get_typed_dict_class_name(func.__qualname__.replace('.', '_') + 'Yield')
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(yield_type, class_name))
yield_type = make_forward_ref(class_name)
function = FunctionDefinition.from_callable(func)
signature = function.signature
signature = update_signature_args(signature, new_arg_types,
function.has_self, existing_annotation_strategy)
signature = update_signature_return(signature, return_type,
yield_type, existing_annotation_strategy)
return FunctionDefinition(function.module, function.qualname,
function.kind, signature,
function.is_async, typed_dict_class_stubs)
class_name = get_typed_dict_class_name(name)
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(typ, class_name))
typ = make_forward_ref(class_name)
new_arg_types[name] = typ
if return_type and is_anonymous_typed_dict(return_type):
# Replace the dot in a qualified name.
class_name = get_typed_dict_class_name(func.__qualname__.replace('.', '_'))
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(return_type, class_name))
return_type = make_forward_ref(class_name)
if yield_type and is_anonymous_typed_dict(yield_type):
# Replace the dot in a qualified name.
class_name = get_typed_dict_class_name(func.__qualname__.replace('.', '_') + 'Yield')
typed_dict_class_stubs.extend(ClassStub.stubs_from_typed_dict(yield_type, class_name))
yield_type = make_forward_ref(class_name)
function = FunctionDefinition.from_callable(func)
signature = function.signature
signature = update_signature_args(signature, new_arg_types,
function.has_self, existing_annotation_strategy)
signature = update_signature_return(signature, return_type,
yield_type, existing_annotation_strategy)
return FunctionDefinition(function.module, function.qualname,
function.kind, signature,
function.is_async, typed_dict_class_stubs)