|
54 | 54 | CONTRAVARIANT, |
55 | 55 | COVARIANT, |
56 | 56 | IMPLICITLY_ABSTRACT, |
| 57 | + INVARIANT, |
57 | 58 | LAMBDA_NAME, |
58 | 59 | LITERAL_TYPE, |
59 | 60 | REVEAL_LOCALS, |
@@ -292,9 +293,9 @@ class ExpressionChecker(ExpressionVisitor[Type], ExpressionCheckerSharedApi): |
292 | 293 | msg: MessageBuilder |
293 | 294 | # Type context for type inference |
294 | 295 | type_context: list[Type | None] |
| 296 | + expr_context: list[Expression] |
295 | 297 | # constraints for the type context, used for type inference |
296 | 298 | constraint_context: list[list[Constraint]] |
297 | | - callee_context: list[CallableType] |
298 | 299 |
|
299 | 300 | # cache resolved types in some cases |
300 | 301 | resolved_type: dict[Expression, ProperType] |
@@ -322,6 +323,7 @@ def __init__( |
322 | 323 | self.in_expression = False |
323 | 324 | self.type_context = [None] |
324 | 325 | self.constraint_context = [[]] |
| 326 | + self.expr_context = [] |
325 | 327 |
|
326 | 328 | # Temporary overrides for expression types. This is currently |
327 | 329 | # used by the union math in overloads. |
@@ -1590,6 +1592,15 @@ def check_call( |
1590 | 1592 | if overloaded_result is not None: |
1591 | 1593 | return overloaded_result |
1592 | 1594 |
|
| 1595 | + _show( |
| 1596 | + f"\n=== CHECKING ===================" |
| 1597 | + f"\n\tcallee={callee}" |
| 1598 | + f"\n\targs={args} " |
| 1599 | + f"\n\targ_kinds={arg_kinds}" |
| 1600 | + f"\n\ttype_context={self.type_context}" |
| 1601 | + f"\n\tconstraints={self.constraint_context}" |
| 1602 | + ) |
| 1603 | + |
1593 | 1604 | result = self.check_callable_call( |
1594 | 1605 | callee, |
1595 | 1606 | args, |
@@ -1602,11 +1613,6 @@ def check_call( |
1602 | 1613 | ) |
1603 | 1614 | _show( |
1604 | 1615 | f"\n=== RESULT ===================" |
1605 | | - f"\n\tcallee={callee}" |
1606 | | - f"\n\targs={args} " |
1607 | | - f"\n\targ_kinds={arg_kinds}" |
1608 | | - f"\n\ttype_context={self.type_context}" |
1609 | | - f"\n\tconstraints={self.constraint_context}" |
1610 | 1616 | f"\n\n\tret_type={result[0]}" |
1611 | 1617 | f"\n\tinferred_callee={result[1]}" |
1612 | 1618 | ) |
@@ -2053,6 +2059,9 @@ def infer_constraints_from_context( |
2053 | 2059 | # variables in an expression are inferred at the same time. |
2054 | 2060 | # (And this is hard, also we need to be careful with lambdas that require |
2055 | 2061 | # two passes.) |
| 2062 | + ctx = proper_ctx |
| 2063 | + ctx = update_type_context_using_constraints(ctx, self.constraint_context[-1]) |
| 2064 | + |
2056 | 2065 | if ( |
2057 | 2066 | isinstance(proper_ret, TypeVarType) |
2058 | 2067 | or isinstance(proper_ret, UnionType) |
@@ -2084,61 +2093,7 @@ def infer_constraints_from_context( |
2084 | 2093 | if not is_generic_instance(ctx) and not is_literal_type_like(ctx): |
2085 | 2094 | return [] |
2086 | 2095 |
|
2087 | | - NEW_CODE: bool = True |
2088 | | - if NEW_CODE: |
2089 | | - object_type = self.named_type("builtins.object") |
2090 | | - ctx = proper_ctx |
2091 | | - ctx = self.update_type_context_using_constraints(ctx) |
2092 | | - return infer_constraints(proper_ret, ctx, SUBTYPE_OF) |
2093 | | - else: |
2094 | | - erased_ctx = get_proper_type(replace_meta_vars(ctx, ErasedType())) |
2095 | | - return infer_constraints(proper_ret, erased_ctx, SUBTYPE_OF) |
2096 | | - |
2097 | | - def update_type_context_using_constraints(self, ctx: Type | None) -> Type | None: |
2098 | | - object_type = self.named_type("builtins.object") |
2099 | | - if ctx is None: |
2100 | | - return None |
2101 | | - |
2102 | | - tvars = get_all_type_vars(ctx) |
2103 | | - |
2104 | | - # concatenate self.constraint_context, which is a list of lists |
2105 | | - all_constraints = [c for cl in self.constraint_context for c in cl] |
2106 | | - upper_bounds = get_upper_bounds(tvars, all_constraints) |
2107 | | - lower_bounds = get_lower_bounds(tvars, all_constraints) |
2108 | | - |
2109 | | - for tvar in tvars: |
2110 | | - if not tvar.id.is_meta_var(): |
2111 | | - continue |
2112 | | - |
2113 | | - if isinstance(tvar, TypeVarType): |
2114 | | - variance = infer_variance_in_expr(ctx, tvar) |
2115 | | - if variance == COVARIANT: |
2116 | | - # If the upper bound is just object, erase it instead. |
2117 | | - # otherwise the solver may infer too general type |
2118 | | - contextual_upper_bound = upper_bounds[tvar.id] |
2119 | | - if is_same_type(contextual_upper_bound, object_type): |
2120 | | - pass |
2121 | | - ctx = replace_typevar(ctx, tvar.id, ErasedType()) |
2122 | | - else: |
2123 | | - ctx = replace_typevar(ctx, tvar.id, contextual_upper_bound) |
2124 | | - elif variance == CONTRAVARIANT: |
2125 | | - contextual_lower_bound = lower_bounds[tvar.id] |
2126 | | - |
2127 | | - ctx = replace_typevar(ctx, tvar.id, contextual_lower_bound) |
2128 | | - |
2129 | | - # if is_same_type(contextual_lower_bound, UninhabitedType()): |
2130 | | - # pass |
2131 | | - # ctx = replace_typevar(ctx, tvar.id, ErasedType()) |
2132 | | - # else: |
2133 | | - # ctx = replace_typevar(ctx, tvar.id, contextual_lower_bound) |
2134 | | - else: |
2135 | | - pass |
2136 | | - ctx = replace_typevar(ctx, tvar.id, ErasedType()) |
2137 | | - else: |
2138 | | - pass |
2139 | | - ctx = replace_typevar(ctx, tvar.id, ErasedType()) |
2140 | | - |
2141 | | - return ctx |
| 2096 | + return infer_constraints(proper_ret, ctx, SUBTYPE_OF) |
2142 | 2097 |
|
2143 | 2098 | def infer_function_type_arguments( |
2144 | 2099 | self, |
@@ -2159,32 +2114,31 @@ def infer_function_type_arguments( |
2159 | 2114 | if self.chk.in_checked_function(): |
2160 | 2115 | # compute the outer solution |
2161 | 2116 | outer_constraints = self.infer_constraints_from_context(callee_type, context) |
| 2117 | + minimize = True |
| 2118 | + maximize = False |
| 2119 | + # detect if we are in an outermost call context. |
| 2120 | + # if len(self.type_context) >=2 and self.type_context[-2] is None and self.type_context[-1] is not None: |
| 2121 | + # minimize = False |
| 2122 | + # maximize = True |
| 2123 | + # if len(self.type_context) >= 2 and self.type_context[-2] is None and self.type_context[-1] is not None: |
| 2124 | + # minimize = False |
| 2125 | + # maximize = True |
| 2126 | + |
2162 | 2127 | outer_solution, _ = solve_constraints( |
2163 | 2128 | callee_type.variables, |
2164 | 2129 | outer_constraints, # + self.constraint_context[-1], |
2165 | 2130 | strict=self.chk.in_checked_function(), |
2166 | 2131 | allow_polymorphic=False, |
| 2132 | + minimize=minimize, |
| 2133 | + maximize=maximize, |
2167 | 2134 | ) |
2168 | 2135 | outer_solution = filter_solution(outer_solution) |
2169 | 2136 | outer_callee = self.apply_generic_arguments( |
2170 | 2137 | callee_type, outer_solution, context, skip_unsatisfied=True |
2171 | 2138 | ) |
2172 | 2139 |
|
2173 | 2140 | # compute the trivial constraints |
2174 | | - # each TVar is a subtype of its upper bound |
2175 | | - trivial_constraints = [ |
2176 | | - Constraint(t, SUBTYPE_OF, t.upper_bound) |
2177 | | - for t in callee_type.variables |
2178 | | - if isinstance(t, TypeVarType) |
2179 | | - ] |
2180 | | - # a constrained TVar is a subtype of the union of its values |
2181 | | - # relevant for `testCallGenericFunctionWithTypeVarValueRestrictionUsingContext` |
2182 | | - trivial_constraints += [ |
2183 | | - Constraint(t, SUBTYPE_OF, make_simplified_union(t.values)) |
2184 | | - for t in callee_type.variables |
2185 | | - if isinstance(t, TypeVarType) and t.values |
2186 | | - ] |
2187 | | - # trivial_constraints = [] |
| 2141 | + trivial_constraints = get_trivial_constraints(callee_type.variables) |
2188 | 2142 |
|
2189 | 2143 | # _show( |
2190 | 2144 | # f"\n=== DEBUG ============================" |
@@ -2213,6 +2167,12 @@ def infer_function_type_arguments( |
2213 | 2167 | # relevant for `testWideOuterContext` unit tests. |
2214 | 2168 | constraints=outer_constraints + trivial_constraints, |
2215 | 2169 | ) |
| 2170 | + _show( |
| 2171 | + f"\n===== INFERRED ARGS =====" |
| 2172 | + f"\n\t{callee_type.arg_types}" |
| 2173 | + f"\n\tconstraints={outer_constraints + trivial_constraints}" |
| 2174 | + f"\n\t{arg_types=}" |
| 2175 | + ) |
2216 | 2176 |
|
2217 | 2177 | arg_pass_nums = self.get_arg_infer_passes( |
2218 | 2178 | callee_type, args, arg_types, formal_to_actual, len(args) |
@@ -2251,8 +2211,8 @@ def infer_function_type_arguments( |
2251 | 2211 | # HACK: convert "Literal?" constraints to their non-literal versions. |
2252 | 2212 | # relevant for `testLiteral*` tests. |
2253 | 2213 | inner_constraints = [ |
2254 | | - Constraint(c.original_type_var, c.op, forget_last_known_value(c.target)) |
2255 | | - # Constraint(c.original_type_var, c.op, use_last_known_value(c.target)) |
| 2214 | + # Constraint(c.original_type_var, c.op, forget_last_known_value(c.target)) |
| 2215 | + Constraint(c.original_type_var, c.op, c.target) |
2256 | 2216 | for c in _inner_constraints |
2257 | 2217 | ] |
2258 | 2218 | inner_solution, _ = solve_constraints( |
@@ -2285,6 +2245,14 @@ def infer_function_type_arguments( |
2285 | 2245 | callee_type, joint_solution, context, skip_unsatisfied=True |
2286 | 2246 | ) |
2287 | 2247 |
|
| 2248 | + # determine if solution was successful: |
| 2249 | + if is_proper_solution(callee_type, joint_solution): |
| 2250 | + extra_constraints = outer_constraints + inner_constraints + trivial_constraints |
| 2251 | + elif is_proper_solution(callee_type, outer_solution): |
| 2252 | + extra_constraints = outer_constraints + trivial_constraints |
| 2253 | + else: |
| 2254 | + extra_constraints = outer_constraints + inner_constraints |
| 2255 | + |
2288 | 2256 | # determine which solution to take |
2289 | 2257 | use_joint = all( |
2290 | 2258 | # only use joint if it solved at least the same variables as the outer solution |
@@ -2392,7 +2360,7 @@ def infer_function_type_arguments( |
2392 | 2360 | minimize=True, |
2393 | 2361 | ) |
2394 | 2362 | inferred_args = filter_solution(new_inner_solution) |
2395 | | - # _show(f"Two stage inference: \n\t{callee_type=}\n\t{inferred_args=}") |
| 2363 | + # _show(f"Two stage inference: \n\t{callee_type=}\n\t{inferred_args=}") |
2396 | 2364 | else: # END NEW CODE |
2397 | 2365 | pass |
2398 | 2366 |
|
@@ -2435,11 +2403,8 @@ def infer_function_type_arguments( |
2435 | 2403 | elif not first_arg or not is_subtype(self.named_type("builtins.str"), first_arg): |
2436 | 2404 | self.chk.fail(message_registry.KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPE, context) |
2437 | 2405 |
|
2438 | | - if not self.chk.options.old_type_inference and any( |
2439 | | - a is None |
2440 | | - or isinstance(get_proper_type(a), UninhabitedType) |
2441 | | - or set(get_type_vars(a)) & set(callee_type.variables) |
2442 | | - for a in inferred_args |
| 2406 | + if not self.chk.options.old_type_inference and not is_proper_solution( |
| 2407 | + callee_type, inferred_args |
2443 | 2408 | ): |
2444 | 2409 | if need_refresh: |
2445 | 2410 | # Technically we need to refresh formal_to_actual after *each* inference pass, |
@@ -2465,6 +2430,8 @@ def infer_function_type_arguments( |
2465 | 2430 | context=self.argument_infer_context(), |
2466 | 2431 | strict=self.chk.in_checked_function(), |
2467 | 2432 | allow_polymorphic=True, |
| 2433 | + extra_constraints=extra_constraints, |
| 2434 | + minimize=True, |
2468 | 2435 | ) |
2469 | 2436 | poly_callee_type = self.apply_generic_arguments( |
2470 | 2437 | callee_type, poly_inferred_args, context |
@@ -6315,6 +6282,7 @@ def accept( |
6315 | 6282 | self.constraint_context.append(constraints or []) |
6316 | 6283 | # type_context = self.update_type_context_using_constraints(type_context) |
6317 | 6284 | self.type_context.append(type_context) |
| 6285 | + self.expr_context.append(node) |
6318 | 6286 |
|
6319 | 6287 | # fix the type context using the contextual constraints |
6320 | 6288 | if HACKS and type_context is not None: |
@@ -6381,6 +6349,7 @@ def accept( |
6381 | 6349 | ) |
6382 | 6350 | self.is_callee = old_is_callee |
6383 | 6351 | self.type_context.pop() |
| 6352 | + self.expr_context.pop() |
6384 | 6353 | self.constraint_context.pop() |
6385 | 6354 | assert typ is not None |
6386 | 6355 | self.chk.store_type(node, typ) |
@@ -7151,21 +7120,25 @@ def _show(*args: object) -> None: |
7151 | 7120 |
|
7152 | 7121 |
|
7153 | 7122 | def get_trivial_constraints( |
7154 | | - tvars: Sequence[TypeVarLikeType], filter_object: bool = True |
| 7123 | + tvars: Sequence[TypeVarLikeType], /, *, filter_object: bool = True |
7155 | 7124 | ) -> Sequence[Constraint]: |
7156 | 7125 | """Every Type Variable is a subtype of its upper bound.""" |
7157 | 7126 | # each TVar is a subtype of its upper bound |
7158 | 7127 | constraints: list[Constraint] = [] |
7159 | 7128 | for t in tvars: |
7160 | 7129 | upper_bound = get_proper_type(t.upper_bound) |
| 7130 | + |
| 7131 | + # An upper bound of 'object' is not informative, and usually just means no |
| 7132 | + # constraint was specified. We skip these because the solver picks the upper |
| 7133 | + # bound as a solution if there are no lower constraints. |
7161 | 7134 | if ( |
7162 | 7135 | filter_object |
7163 | 7136 | and isinstance(upper_bound, Instance) |
7164 | 7137 | and upper_bound.type.fullname == "builtins.object" |
7165 | 7138 | ): |
7166 | | - # these are usually because no constraint was specified, not because the user gave one. |
7167 | | - continue |
7168 | | - constraints.append(Constraint(t, SUBTYPE_OF, upper_bound)) |
| 7139 | + pass |
| 7140 | + else: |
| 7141 | + constraints.append(Constraint(t, SUBTYPE_OF, upper_bound)) |
7169 | 7142 |
|
7170 | 7143 | if isinstance(t, TypeVarType) and t.values: |
7171 | 7144 | constraints.append(Constraint(t, SUBTYPE_OF, make_simplified_union(t.values))) |
@@ -7183,10 +7156,6 @@ def get_upper_bounds( |
7183 | 7156 | relevant_constraints = [ |
7184 | 7157 | c for c in constraints if c.type_var == tvar.id and c.op == SUBTYPE_OF |
7185 | 7158 | ] |
7186 | | - # if not relevant_constraints: |
7187 | | - # upper_bounds[tvar.id] = None |
7188 | | - # continue |
7189 | | - |
7190 | 7159 | top = tvar.upper_bound |
7191 | 7160 | for c in relevant_constraints: |
7192 | 7161 | top = meet_types(top, c.target) |
@@ -7212,3 +7181,51 @@ def get_lower_bounds( |
7212 | 7181 | bottom = join_types(bottom, c.target) |
7213 | 7182 | lower_bounds[tvar.id] = bottom |
7214 | 7183 | return lower_bounds |
| 7184 | + |
| 7185 | + |
| 7186 | +def update_type_context_using_constraints(typ: Type, /, constraints: list[Constraint]) -> Type: |
| 7187 | + tvars = get_all_type_vars(typ) |
| 7188 | + |
| 7189 | + # concatenate self.constraint_context, which is a list of lists |
| 7190 | + upper_bounds = get_upper_bounds(tvars, constraints) |
| 7191 | + lower_bounds = get_lower_bounds(tvars, constraints) |
| 7192 | + |
| 7193 | + for tvar in tvars: |
| 7194 | + if not tvar.id.is_meta_var(): |
| 7195 | + continue |
| 7196 | + |
| 7197 | + if isinstance(tvar, TypeVarType): |
| 7198 | + variance = infer_variance_in_expr(typ, tvar) |
| 7199 | + |
| 7200 | + if variance == INVARIANT: |
| 7201 | + typ = replace_typevar(typ, tvar.id, ErasedType()) |
| 7202 | + |
| 7203 | + elif variance == COVARIANT: |
| 7204 | + contextual_upper_bound = upper_bounds[tvar.id] |
| 7205 | + typ = replace_typevar(typ, tvar.id, contextual_upper_bound) |
| 7206 | + |
| 7207 | + elif variance == CONTRAVARIANT: |
| 7208 | + contextual_lower_bound = lower_bounds[tvar.id] |
| 7209 | + typ = replace_typevar(typ, tvar.id, contextual_lower_bound) |
| 7210 | + |
| 7211 | + else: |
| 7212 | + raise ValueError(f"Unexpected variance: {variance}") |
| 7213 | + |
| 7214 | + elif isinstance(tvar, TypeVarTupleType): |
| 7215 | + typ = replace_typevar(typ, tvar.id, ErasedType()) |
| 7216 | + elif isinstance(tvar, ParamSpecType): |
| 7217 | + typ = replace_typevar(typ, tvar.id, ErasedType()) |
| 7218 | + else: |
| 7219 | + # Add other branches if more TypeVarLikeType are added |
| 7220 | + raise TypeError(f"Unexpected type variable type: {tvar}") |
| 7221 | + |
| 7222 | + return typ |
| 7223 | + |
| 7224 | + |
| 7225 | +def is_proper_solution(callee_type: CallableType, inferred_args: Sequence[Type | None]) -> bool: |
| 7226 | + return not any( |
| 7227 | + a is None |
| 7228 | + or isinstance(get_proper_type(a), UninhabitedType) |
| 7229 | + or (set(get_type_vars(a)) & set(callee_type.variables)) |
| 7230 | + for a in inferred_args |
| 7231 | + ) |