|
10 | 10 | from mypy.types import ( |
11 | 11 | TupleType, Instance, FunctionLike, Type, CallableType, TypeVarDef, Overloaded, |
12 | 12 | TypeVarType, TypeType, UninhabitedType, FormalArgument, UnionType, NoneType, |
13 | | - ProperType, get_proper_type, get_proper_types, copy_type |
| 13 | + AnyType, TypeOfAny, TypeType, ProperType, get_proper_type, get_proper_types, copy_type |
14 | 14 | ) |
15 | 15 | from mypy.nodes import ( |
16 | | - TypeInfo, TypeVar, ARG_STAR, |
| 16 | + FuncBase, FuncItem, OverloadedFuncDef, TypeInfo, TypeVar, ARG_STAR, ARG_STAR2, |
17 | 17 | ) |
18 | 18 | from mypy.maptype import map_instance_to_supertype |
19 | 19 | from mypy.expandtype import expand_type_by_instance, expand_type |
| 20 | +from mypy.sharedparse import argument_elide_name |
20 | 21 |
|
21 | 22 | from mypy.typevars import fill_typevars |
22 | 23 |
|
@@ -369,3 +370,50 @@ def erase_to_union_or_bound(typ: TypeVarType) -> ProperType: |
369 | 370 | return make_simplified_union(typ.values) |
370 | 371 | else: |
371 | 372 | return get_proper_type(typ.upper_bound) |
| 373 | + |
| 374 | + |
| 375 | +def function_type(func: FuncBase, fallback: Instance) -> FunctionLike: |
| 376 | + if func.type: |
| 377 | + assert isinstance(func.type, FunctionLike) |
| 378 | + return func.type |
| 379 | + else: |
| 380 | + # Implicit type signature with dynamic types. |
| 381 | + if isinstance(func, FuncItem): |
| 382 | + return callable_type(func, fallback) |
| 383 | + else: |
| 384 | + # Broken overloads can have self.type set to None. |
| 385 | + # TODO: should we instead always set the type in semantic analyzer? |
| 386 | + assert isinstance(func, OverloadedFuncDef) |
| 387 | + any_type = AnyType(TypeOfAny.from_error) |
| 388 | + dummy = CallableType([any_type, any_type], |
| 389 | + [ARG_STAR, ARG_STAR2], |
| 390 | + [None, None], any_type, |
| 391 | + fallback, |
| 392 | + line=func.line, is_ellipsis_args=True) |
| 393 | + # Return an Overloaded, because some callers may expect that |
| 394 | + # an OverloadedFuncDef has an Overloaded type. |
| 395 | + return Overloaded([dummy]) |
| 396 | + |
| 397 | + |
| 398 | +def callable_type(fdef: FuncItem, fallback: Instance, |
| 399 | + ret_type: Optional[Type] = None) -> CallableType: |
| 400 | + # TODO: somewhat unfortunate duplication with prepare_method_signature in semanal |
| 401 | + if fdef.info and not fdef.is_static: |
| 402 | + self_type = fill_typevars(fdef.info) # type: Type |
| 403 | + if fdef.is_class or fdef.name() == '__new__': |
| 404 | + self_type = TypeType.make_normalized(self_type) |
| 405 | + args = [self_type] + [AnyType(TypeOfAny.unannotated)] * (len(fdef.arg_names)-1) |
| 406 | + else: |
| 407 | + args = [AnyType(TypeOfAny.unannotated)] * len(fdef.arg_names) |
| 408 | + |
| 409 | + return CallableType( |
| 410 | + args, |
| 411 | + fdef.arg_kinds, |
| 412 | + [None if argument_elide_name(n) else n for n in fdef.arg_names], |
| 413 | + ret_type or AnyType(TypeOfAny.unannotated), |
| 414 | + fallback, |
| 415 | + name=fdef.name(), |
| 416 | + line=fdef.line, |
| 417 | + column=fdef.column, |
| 418 | + implicit=True, |
| 419 | + ) |
0 commit comments