Fix a malformed instance by replacing all type arguments with TypeVar default or Any. Also emit a suitable error if this is not due to implicit Any's.
(
t: Instance,
fail: MsgCallback,
note: MsgCallback,
disallow_any: bool,
options: Options,
use_generic_error: bool = False,
unexpanded_type: Type | None = None,
)
| 2064 | |
| 2065 | |
| 2066 | def fix_instance( |
| 2067 | t: Instance, |
| 2068 | fail: MsgCallback, |
| 2069 | note: MsgCallback, |
| 2070 | disallow_any: bool, |
| 2071 | options: Options, |
| 2072 | use_generic_error: bool = False, |
| 2073 | unexpanded_type: Type | None = None, |
| 2074 | ) -> None: |
| 2075 | """Fix a malformed instance by replacing all type arguments with TypeVar default or Any. |
| 2076 | |
| 2077 | Also emit a suitable error if this is not due to implicit Any's. |
| 2078 | """ |
| 2079 | arg_count = len(t.args) |
| 2080 | min_tv_count = sum(not tv.has_default() for tv in t.type.defn.type_vars) |
| 2081 | max_tv_count = len(t.type.type_vars) |
| 2082 | if arg_count < min_tv_count or arg_count > max_tv_count: |
| 2083 | # Don't use existing args if arg_count doesn't match |
| 2084 | if arg_count > max_tv_count: |
| 2085 | # Already wrong arg count error, don't emit missing type parameters error as well. |
| 2086 | disallow_any = False |
| 2087 | t.args = () |
| 2088 | |
| 2089 | args: list[Type] = [*(t.args[:max_tv_count])] |
| 2090 | any_type: AnyType | None = None |
| 2091 | env: dict[TypeVarId, Type] = {} |
| 2092 | |
| 2093 | for tv, arg in itertools.zip_longest(t.type.defn.type_vars, t.args, fillvalue=None): |
| 2094 | if tv is None: |
| 2095 | continue |
| 2096 | if arg is None: |
| 2097 | if tv.has_default(): |
| 2098 | arg = tv.default |
| 2099 | else: |
| 2100 | if any_type is None: |
| 2101 | fullname = None if use_generic_error else t.type.fullname |
| 2102 | any_type = get_omitted_any( |
| 2103 | disallow_any, fail, note, t, options, fullname, unexpanded_type |
| 2104 | ) |
| 2105 | arg = any_type |
| 2106 | args.append(arg) |
| 2107 | env[tv.id] = arg |
| 2108 | t.args = tuple(args) |
| 2109 | fix_type_var_tuple_argument(t) |
| 2110 | if not t.type.has_type_var_tuple_type: |
| 2111 | with state.strict_optional_set(True): |
| 2112 | fixed = expand_type(t, env) |
| 2113 | assert isinstance(fixed, Instance) |
| 2114 | t.args = fixed.args |
| 2115 | |
| 2116 | |
| 2117 | def instantiate_type_alias( |
no test coverage detected
searching dependent graphs…