-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Reject ParamSpec-typed callables calls with insufficient arguments #17323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5cdb753
3b2297f
a32ad3f
63995e3
be2c49b
0a658a7
63f9438
786fb55
1903402
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1716,36 +1716,16 @@ def check_callable_call( | |
| callee = callee.copy_modified(ret_type=fresh_ret_type) | ||
|
|
||
| if callee.is_generic(): | ||
| need_refresh = any( | ||
| isinstance(v, (ParamSpecType, TypeVarTupleType)) for v in callee.variables | ||
| callee, formal_to_actual = self.adjust_generic_callable_params_mapping( | ||
| callee, args, arg_kinds, arg_names, formal_to_actual, context | ||
| ) | ||
| callee = freshen_function_type_vars(callee) | ||
| callee = self.infer_function_type_arguments_using_context(callee, context) | ||
| if need_refresh: | ||
| # Argument kinds etc. may have changed due to | ||
| # ParamSpec or TypeVarTuple variables being replaced with an arbitrary | ||
| # number of arguments; recalculate actual-to-formal map | ||
| formal_to_actual = map_actuals_to_formals( | ||
| arg_kinds, | ||
| arg_names, | ||
| callee.arg_kinds, | ||
| callee.arg_names, | ||
| lambda i: self.accept(args[i]), | ||
| ) | ||
| callee = self.infer_function_type_arguments( | ||
| callee, args, arg_kinds, arg_names, formal_to_actual, need_refresh, context | ||
| ) | ||
| if need_refresh: | ||
| formal_to_actual = map_actuals_to_formals( | ||
| arg_kinds, | ||
| arg_names, | ||
| callee.arg_kinds, | ||
| callee.arg_names, | ||
| lambda i: self.accept(args[i]), | ||
| ) | ||
|
|
||
| param_spec = callee.param_spec() | ||
| if param_spec is not None and arg_kinds == [ARG_STAR, ARG_STAR2]: | ||
| if ( | ||
| param_spec is not None | ||
| and arg_kinds == [ARG_STAR, ARG_STAR2] | ||
| and len(formal_to_actual) == 2 | ||
| ): | ||
| arg1 = self.accept(args[0]) | ||
| arg2 = self.accept(args[1]) | ||
| if ( | ||
|
|
@@ -2351,6 +2331,9 @@ def check_argument_count( | |
| # Positional argument when expecting a keyword argument. | ||
| self.msg.too_many_positional_arguments(callee, context) | ||
| ok = False | ||
| elif callee.param_spec() is not None and not formal_to_actual[i]: | ||
| self.msg.too_few_arguments(callee, context, actual_names) | ||
| ok = False | ||
| return ok | ||
|
|
||
| def check_for_extra_actual_arguments( | ||
|
|
@@ -2625,7 +2608,7 @@ def check_overload_call( | |
| arg_types = self.infer_arg_types_in_empty_context(args) | ||
| # Step 1: Filter call targets to remove ones where the argument counts don't match | ||
| plausible_targets = self.plausible_overload_call_targets( | ||
| arg_types, arg_kinds, arg_names, callee | ||
| args, arg_types, arg_kinds, arg_names, callee, context | ||
| ) | ||
|
|
||
| # Step 2: If the arguments contain a union, we try performing union math first, | ||
|
|
@@ -2743,12 +2726,52 @@ def check_overload_call( | |
| self.chk.fail(message_registry.TOO_MANY_UNION_COMBINATIONS, context) | ||
| return result | ||
|
|
||
| def adjust_generic_callable_params_mapping( | ||
| self, | ||
| callee: CallableType, | ||
| args: list[Expression], | ||
| arg_kinds: list[ArgKind], | ||
| arg_names: Sequence[str | None] | None, | ||
| formal_to_actual: list[list[int]], | ||
| context: Context, | ||
| ) -> tuple[CallableType, list[list[int]]]: | ||
| need_refresh = any( | ||
| isinstance(v, (ParamSpecType, TypeVarTupleType)) for v in callee.variables | ||
| ) | ||
| callee = freshen_function_type_vars(callee) | ||
| callee = self.infer_function_type_arguments_using_context(callee, context) | ||
| if need_refresh: | ||
| # Argument kinds etc. may have changed due to | ||
| # ParamSpec or TypeVarTuple variables being replaced with an arbitrary | ||
| # number of arguments; recalculate actual-to-formal map | ||
| formal_to_actual = map_actuals_to_formals( | ||
| arg_kinds, | ||
| arg_names, | ||
| callee.arg_kinds, | ||
| callee.arg_names, | ||
| lambda i: self.accept(args[i]), | ||
| ) | ||
| callee = self.infer_function_type_arguments( | ||
| callee, args, arg_kinds, arg_names, formal_to_actual, need_refresh, context | ||
| ) | ||
| if need_refresh: | ||
| formal_to_actual = map_actuals_to_formals( | ||
| arg_kinds, | ||
| arg_names, | ||
| callee.arg_kinds, | ||
| callee.arg_names, | ||
| lambda i: self.accept(args[i]), | ||
| ) | ||
| return callee, formal_to_actual | ||
|
|
||
| def plausible_overload_call_targets( | ||
| self, | ||
| args: list[Expression], | ||
| arg_types: list[Type], | ||
| arg_kinds: list[ArgKind], | ||
| arg_names: Sequence[str | None] | None, | ||
| overload: Overloaded, | ||
| context: Context, | ||
| ) -> list[CallableType]: | ||
| """Returns all overload call targets that having matching argument counts. | ||
|
|
||
|
|
@@ -2782,8 +2805,12 @@ def has_shape(typ: Type) -> bool: | |
| formal_to_actual = map_actuals_to_formals( | ||
| arg_kinds, arg_names, typ.arg_kinds, typ.arg_names, lambda i: arg_types[i] | ||
| ) | ||
|
|
||
| with self.msg.filter_errors(): | ||
| if typ.is_generic() and typ.param_spec() is not None: | ||
| typ, formal_to_actual = self.adjust_generic_callable_params_mapping( | ||
| typ, args, arg_kinds, arg_names, formal_to_actual, context | ||
| ) | ||
|
||
|
|
||
| if self.check_argument_count( | ||
| typ, arg_types, arg_kinds, arg_names, formal_to_actual, None | ||
| ): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't need to be deduplicated anymore because it's only called once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I'd rather keep this separated: that block is rather long and not absolutely trivial, it looks better as a separate method to my eyes. However, there was another problem not detected by linter for some reason (uhm, sorry, because ARG001 is not enabled?.. Why?..), pushed to remove arguments that are no longer necessary.