MCPcopy
hub / github.com/python/mypy / do_func_def

Method do_func_def

mypy/fastparse.py:909–1068  ·  view source on GitHub ↗

Helper shared between visit_FunctionDef and visit_AsyncFunctionDef.

(
        self, n: ast3.FunctionDef | ast3.AsyncFunctionDef, is_coroutine: bool = False
    )

Source from the content-addressed store, hash-verified

907 return self.do_func_def(n, is_coroutine=True)
908
909 def do_func_def(
910 self, n: ast3.FunctionDef | ast3.AsyncFunctionDef, is_coroutine: bool = False
911 ) -> FuncDef | Decorator:
912 """Helper shared between visit_FunctionDef and visit_AsyncFunctionDef."""
913 self.class_and_function_stack.append("D")
914 no_type_check = bool(
915 n.decorator_list and any(is_no_type_check_decorator(d) for d in n.decorator_list)
916 )
917
918 lineno = n.lineno
919 args = self.transform_args(n.args, lineno, no_type_check=no_type_check)
920 if self.options.pos_only_special_methods and special_function_elide_names(n.name):
921 for arg in args:
922 arg.pos_only = True
923
924 arg_kinds = [arg.kind for arg in args]
925 arg_names = [None if arg.pos_only else arg.variable.name for arg in args]
926 # Type parameters, if using new syntax for generics (PEP 695)
927 explicit_type_params: list[TypeParam] | None = None
928
929 arg_types: list[Type | None] = []
930 if no_type_check:
931 arg_types = [None] * len(args)
932 return_type = None
933 elif n.type_comment is not None:
934 try:
935 func_type_ast = ast3_parse(n.type_comment, "<func_type>", "func_type")
936 assert isinstance(func_type_ast, FunctionType)
937 # for ellipsis arg
938 if (
939 len(func_type_ast.argtypes) == 1
940 and isinstance(func_type_ast.argtypes[0], ast3.Constant)
941 and func_type_ast.argtypes[0].value is Ellipsis
942 ):
943 if n.returns:
944 # PEP 484 disallows both type annotations and type comments
945 self.fail(
946 message_registry.DUPLICATE_TYPE_SIGNATURES,
947 lineno,
948 n.col_offset,
949 blocker=False,
950 )
951 arg_types = [
952 (
953 a.type_annotation
954 if a.type_annotation is not None
955 else AnyType(TypeOfAny.unannotated)
956 )
957 for a in args
958 ]
959 else:
960 # PEP 484 disallows both type annotations and type comments
961 if n.returns or any(a.type_annotation is not None for a in args):
962 self.fail(
963 message_registry.DUPLICATE_TYPE_SIGNATURES,
964 lineno,
965 n.col_offset,
966 blocker=False,

Callers 2

visit_FunctionDefMethod · 0.95

Calls 15

transform_argsMethod · 0.95
failMethod · 0.95
translate_expr_listMethod · 0.95
noteMethod · 0.95
translate_type_paramsMethod · 0.95
set_type_optionalMethod · 0.95
as_required_blockMethod · 0.95
set_lineMethod · 0.95
AnyTypeClass · 0.90
CallableTypeClass · 0.90
FuncDefClass · 0.90

Tested by

no test coverage detected