Return a nice easily-readable representation of a callable type. For example: def [T <: int] f(self, x: int, y: T) -> None If skip_self is True, print an actual callable type, as it would appear when bound on an instance/class, rather than how it would appear in the defining
(tp: CallableType, options: Options, skip_self: bool = False)
| 3040 | |
| 3041 | |
| 3042 | def pretty_callable(tp: CallableType, options: Options, skip_self: bool = False) -> str: |
| 3043 | """Return a nice easily-readable representation of a callable type. |
| 3044 | For example: |
| 3045 | def [T <: int] f(self, x: int, y: T) -> None |
| 3046 | |
| 3047 | If skip_self is True, print an actual callable type, as it would appear |
| 3048 | when bound on an instance/class, rather than how it would appear in the |
| 3049 | defining statement. |
| 3050 | """ |
| 3051 | s = "" |
| 3052 | asterisk = False |
| 3053 | slash = False |
| 3054 | for i in range(len(tp.arg_types)): |
| 3055 | if s: |
| 3056 | s += ", " |
| 3057 | if tp.arg_kinds[i].is_named() and not asterisk: |
| 3058 | s += "*, " |
| 3059 | asterisk = True |
| 3060 | if tp.arg_kinds[i] == ARG_STAR: |
| 3061 | s += "*" |
| 3062 | asterisk = True |
| 3063 | if tp.arg_kinds[i] == ARG_STAR2: |
| 3064 | s += "**" |
| 3065 | name = tp.arg_names[i] |
| 3066 | if not name and not options.reveal_verbose_types: |
| 3067 | # Avoid ambiguous (and weird) formatting for anonymous args/kwargs. |
| 3068 | if tp.arg_kinds[i] == ARG_STAR and isinstance(tp.arg_types[i], UnpackType): |
| 3069 | name = "args" |
| 3070 | elif tp.arg_kinds[i] == ARG_STAR2 and tp.unpack_kwargs: |
| 3071 | name = "kwargs" |
| 3072 | if name: |
| 3073 | s += name + ": " |
| 3074 | type_str = format_type_bare(tp.arg_types[i], options) |
| 3075 | if tp.arg_kinds[i] == ARG_STAR2 and tp.unpack_kwargs: |
| 3076 | if options.reveal_verbose_types: |
| 3077 | type_str = f"Unpack[{type_str}]" |
| 3078 | else: |
| 3079 | type_str = f"**{type_str}" |
| 3080 | s += type_str |
| 3081 | if tp.arg_kinds[i].is_optional(): |
| 3082 | s += " = ..." |
| 3083 | if ( |
| 3084 | not slash |
| 3085 | and tp.arg_kinds[i].is_positional() |
| 3086 | and name is None |
| 3087 | and ( |
| 3088 | i == len(tp.arg_types) - 1 |
| 3089 | or (tp.arg_names[i + 1] is not None or not tp.arg_kinds[i + 1].is_positional()) |
| 3090 | ) |
| 3091 | ): |
| 3092 | s += ", /" |
| 3093 | slash = True |
| 3094 | |
| 3095 | definition = get_func_def(tp) |
| 3096 | |
| 3097 | # Extract function name, prefer the "human-readable" name if available. |
| 3098 | func_name = None |
| 3099 | if tp.name: |
no test coverage detected
searching dependent graphs…