85
85
from mypy .options import Options as MypyOptions
86
86
from mypy .types import (
87
87
Type , TypeStrVisitor , CallableType , UnboundType , NoneType , TupleType , TypeList , Instance ,
88
- AnyType
88
+ AnyType , get_proper_type
89
89
)
90
90
from mypy .visitor import NodeVisitor
91
91
from mypy .find_sources import create_source_list , InvalidSourceList
@@ -624,26 +624,24 @@ def visit_func_def(self, o: FuncDef, is_abstract: bool = False,
624
624
# name their 0th argument other than self/cls
625
625
is_self_arg = i == 0 and name == 'self'
626
626
is_cls_arg = i == 0 and name == 'cls'
627
- if (annotated_type is None
628
- and not arg_ .initializer
629
- and not is_self_arg
630
- and not is_cls_arg ):
631
- self .add_typing_import ("Any" )
632
- annotation = ": {}" .format (self .typing_name ("Any" ))
633
- elif annotated_type and not is_self_arg and not is_cls_arg :
634
- annotation = ": {}" .format (self .print_annotation (annotated_type ))
635
- else :
636
- annotation = ""
627
+ annotation = ""
628
+ if annotated_type and not is_self_arg and not is_cls_arg :
629
+ # Luckily, an argument explicitly annotated with "Any" has
630
+ # type "UnboundType" and will not match.
631
+ if not isinstance (get_proper_type (annotated_type ), AnyType ):
632
+ annotation = ": {}" .format (self .print_annotation (annotated_type ))
637
633
if arg_ .initializer :
638
- initializer = '...'
639
634
if kind in (ARG_NAMED , ARG_NAMED_OPT ) and not any (arg .startswith ('*' )
640
635
for arg in args ):
641
636
args .append ('*' )
642
637
if not annotation :
643
- typename = self .get_str_type_of_node (arg_ .initializer , True )
644
- annotation = ': {} = ...' .format (typename )
638
+ typename = self .get_str_type_of_node (arg_ .initializer , True , False )
639
+ if typename == '' :
640
+ annotation = '=...'
641
+ else :
642
+ annotation = ': {} = ...' .format (typename )
645
643
else :
646
- annotation += '={}' . format ( initializer )
644
+ annotation += ' = ...'
647
645
arg = name + annotation
648
646
elif kind == ARG_STAR :
649
647
arg = '*%s%s' % (name , annotation )
@@ -654,12 +652,16 @@ def visit_func_def(self, o: FuncDef, is_abstract: bool = False,
654
652
args .append (arg )
655
653
retname = None
656
654
if o .name != '__init__' and isinstance (o .unanalyzed_type , CallableType ):
657
- retname = self .print_annotation (o .unanalyzed_type .ret_type )
655
+ if isinstance (get_proper_type (o .unanalyzed_type .ret_type ), AnyType ):
656
+ # Luckily, a return type explicitly annotated with "Any" has
657
+ # type "UnboundType" and will enter the else branch.
658
+ retname = None # implicit Any
659
+ else :
660
+ retname = self .print_annotation (o .unanalyzed_type .ret_type )
658
661
elif isinstance (o , FuncDef ) and (o .is_abstract or o .name in METHODS_WITH_RETURN_VALUE ):
659
662
# Always assume abstract methods return Any unless explicitly annotated. Also
660
663
# some dunder methods should not have a None return type.
661
- retname = self .typing_name ('Any' )
662
- self .add_typing_import ("Any" )
664
+ retname = None # implicit Any
663
665
elif not has_return_statement (o ) and not is_abstract :
664
666
retname = 'None'
665
667
retfield = ''
@@ -1148,7 +1150,8 @@ def is_private_member(self, fullname: str) -> bool:
1148
1150
return False
1149
1151
1150
1152
def get_str_type_of_node (self , rvalue : Expression ,
1151
- can_infer_optional : bool = False ) -> str :
1153
+ can_infer_optional : bool = False ,
1154
+ can_be_any : bool = True ) -> str :
1152
1155
if isinstance (rvalue , IntExpr ):
1153
1156
return 'int'
1154
1157
if isinstance (rvalue , StrExpr ):
@@ -1165,8 +1168,11 @@ def get_str_type_of_node(self, rvalue: Expression,
1165
1168
isinstance (rvalue , NameExpr ) and rvalue .name == 'None' :
1166
1169
self .add_typing_import ('Any' )
1167
1170
return '{} | None' .format (self .typing_name ('Any' ))
1168
- self .add_typing_import ('Any' )
1169
- return self .typing_name ('Any' )
1171
+ if can_be_any :
1172
+ self .add_typing_import ('Any' )
1173
+ return self .typing_name ('Any' )
1174
+ else :
1175
+ return ''
1170
1176
1171
1177
def print_annotation (self , t : Type ) -> str :
1172
1178
printer = AnnotationPrinter (self )
0 commit comments