Repository navigation
Fix keyword-form assignability for standard dest parameters - #11670
Conversation
In assignFunction, the main loop over positionalsToMatch and the surplus-dest-positional branch both only checked for the presence of a source parameter's kwargsIndex to determine if the keyword home was satisfied. It did not check if the source keyword parameter was assignable to the dest positional parameter. assignFunction also did not fallback to dest positional-or-keyword parameters whenever a dest keyword-only parameter was not found. This caused control to fall into the !destParamInfo branch where, if the source parameter had a defaultArgType, assignability between src and dest was never checked, resulting in a false negative. Move the source kwargsIndex condition into the inner branch scope, and add an assignParam check for when source kwargsIndex is present. Add a dest positional-or-keyword parameter fallback to allow the assignability check between source keyword parameter and dest positional-or-keyword parameter. Gate the new assignParam checks on PartialOverloadOverlap. Adds a test sample callbackProtocol12 which contains positive and negative assignments to a positional-or-keyword callback protocol for both kwargs and keyword-only source callback protocol forms. Adds regression test cases func35 and func36 in overloadOverlap1 to exercise the changes that involve PartialOverloadOverlap gating. Fixes microsoft#10237 Co-authored-by: hsusul <[email protected]>
This comment has been minimized.
This comment has been minimized.
|
🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR. |
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Please address the comments
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Adds a new helper function assignNamedParam to check whether a source's variadic **kwargs keyword parameter is assignable to the keyword form of a destination positional-or-keyword parameter. Replaces the duplicate keyword-form compatibility checks in assignFunction with calls to assignNamedParam.
During the check for if a keyword-only source parameter could be assigned to a positional-or-keyword dest parameter, the check did not account for the case where the source parameter had no default argument. This was a false negative, because the source can only ever satisfy the keyword form of the destination, not the positional form. Fixes the false negative by adding a check that fails when the destination kind is standard (positional-or-keyword) and the source kind is keyword-only and the source parameter has no default argument. This failure is guarded to not fire when in overload-overlap mode. Adds a regression case in CallbackProtocol12 to exercise the invalid assignment, and bumps the expected error count by +1.
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. |
|
Diff from mypy_primer, showing the effect of this PR on open source code: sympy (https://github.com/sympy/sympy)
+ .../projects/sympy/sympy/simplify/tests/test_hyperexpand.py:545:30 - error: Cannot access attribute "n" for class "Basic"
+ Attribute "n" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/simplify/tests/test_hyperexpand.py:547:32 - error: Cannot access attribute "n" for class "Basic"
+ Attribute "n" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/ode/lie_group.py:619:61 - error: Operator "-" not supported for type "Unknown | Basic" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/single.py:867:9 - error: Expression with type "tuple[ComplexInfinity | NaN | Rational | Zero | Infinity | NegativeInfinity | Float | NotImplementedType | Expr | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown]] | tuple[ComplexInfinity | NaN | Rational | Zero | Infinity | NegativeInfinity | Float | NotImplementedType | Expr | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" cannot be assigned to target tuple
- Type "tuple[ComplexInfinity | NaN | Rational | Zero | Infinity | NegativeInfinity | Float | NotImplementedType | Expr | Unknown | Any, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" is incompatible with target tuple
- Tuple size mismatch; expected 2 but received 3 (reportAssignmentType)
- .../projects/sympy/sympy/solvers/recurr.py:424:16 - error: Cannot access attribute "is_polynomial" for class "tuple[Unknown, list[Unknown]]"
+ .../projects/sympy/sympy/solvers/recurr.py:424:16 - error: Cannot access attribute "is_polynomial" for class "tuple[Unknown, list[Expr | Unknown]]"
- .../projects/sympy/sympy/solvers/recurr.py:424:16 - error: Cannot access attribute "is_polynomial" for class "tuple[Unknown, Unknown | Unbound]"
+ .../projects/sympy/sympy/solvers/recurr.py:424:16 - error: Cannot access attribute "is_polynomial" for class "tuple[Unknown | tuple[Unknown, list[Poly]], list[Poly] | Unknown | Unbound]"
+ .../projects/sympy/sympy/solvers/recurr.py:424:16 - error: Cannot access attribute "is_polynomial" for class "tuple[Unknown, list[Poly]]"
+ Attribute "is_polynomial" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/recurr.py:425:20 - error: Cannot access attribute "as_numer_denom" for class "tuple[Unknown, list[Unknown]]"
+ .../projects/sympy/sympy/solvers/recurr.py:425:20 - error: Cannot access attribute "as_numer_denom" for class "tuple[Unknown, list[Expr | Unknown]]"
+ .../projects/sympy/sympy/solvers/recurr.py:425:20 - error: Cannot access attribute "as_numer_denom" for class "tuple[Unknown | tuple[Unknown, list[Poly]], list[Poly] | Unknown | Unbound]"
+ Attribute "as_numer_denom" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/recurr.py:425:20 - error: Cannot access attribute "as_numer_denom" for class "tuple[Unknown, Unknown | Unbound]"
+ .../projects/sympy/sympy/solvers/recurr.py:425:20 - error: Cannot access attribute "as_numer_denom" for class "tuple[Unknown, list[Poly]]"
+ .../projects/sympy/sympy/solvers/recurr.py:440:26 - error: Cannot access attribute "subs" for class "Alg"
+ Attribute "subs" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/recurr.py:442:26 - error: Cannot access attribute "subs" for class "Alg"
+ Attribute "subs" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/recurr.py:449:13 - error: No overloads for "__setitem__" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/recurr.py:449:13 - error: Argument of type "Expr | Unknown | Poly" cannot be assigned to parameter "value" of type "Zero" in function "__setitem__"
+ Type "Expr | Unknown | Poly" is not assignable to type "Zero"
+ "Expr" is not assignable to "Zero" (reportArgumentType)
- .../projects/sympy/sympy/solvers/tests/test_solveset.py:1433:21 - error: Operator "-" not supported for types "Unknown | Tuple" and "Literal[1]"
+ .../projects/sympy/sympy/solvers/tests/test_solveset.py:1433:21 - error: Operator "-" not supported for types "Unknown | Tuple | Indexed" and "Literal[1]"
- .../projects/sympy/sympy/stats/drv.py:269:22 - error: Argument of type "Generator[Self@Basic | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None, None, None]" cannot be assigned to parameter "iterable" of type "Iterable[_SupportsSumNoDefaultT@sum]" in function "sum"
+ .../projects/sympy/sympy/stats/drv.py:269:22 - error: Argument of type "Generator[Self@Basic | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | int | None, None, None]" cannot be assigned to parameter "iterable" of type "Iterable[_SupportsSumNoDefaultT@sum]" in function "sum"
- "Generator[Self@Basic | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None, None, None]" is not assignable to "Iterable[_SupportsSumNoDefaultT@sum]"
+ "Generator[Self@Basic | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | int | None, None, None]" is not assignable to "Iterable[_SupportsSumNoDefaultT@sum]"
- Type parameter "_T_co@Iterable" is covariant, but "Self@Basic | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None" is not a subtype of "_SupportsSumNoDefaultT@sum"
+ Type parameter "_T_co@Iterable" is covariant, but "Self@Basic | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | int | None" is not a subtype of "_SupportsSumNoDefaultT@sum"
- Type "Self@Basic | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+ Type "Self@Basic | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
- Type "Basic* | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+ Type "Basic* | tuple[Unknown, ...] | Unknown | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
- .../projects/sympy/sympy/stats/drv_types.py:293:16 - error: Operator "*" not supported for types "Expr" and "tuple[Unknown, ...] | Unknown | Self@Basic | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic"
+ .../projects/sympy/sympy/stats/drv_types.py:293:16 - error: Operator "*" not supported for types "Expr" and "tuple[Unknown, ...] | Unknown | Self@Basic | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic"
- .../projects/sympy/sympy/stats/rv.py:473:25 - error: Argument of type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | Self@Basic | tuple[Unknown, ...] | Sum | ZeroMatrix | NaN | Piecewise | Basic | int | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
+ .../projects/sympy/sympy/stats/rv.py:473:25 - error: Argument of type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | Self@Basic | tuple[Unknown, ...] | Sum | ZeroMatrix | Add | NaN | Piecewise | Basic | int | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
- Type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | Self@Basic | tuple[Unknown, ...] | Sum | ZeroMatrix | NaN | Piecewise | Basic | int | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number" is not assignable to type "Expr | complex"
+ Type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | Self@Basic | tuple[Unknown, ...] | Sum | ZeroMatrix | Add | NaN | Piecewise | Basic | int | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number" is not assignable to type "Expr | complex"
- .../projects/sympy/sympy/stats/stochastic_process_types.py:1839:25 - error: Argument of type "tuple[Unknown, ...] | Unknown | Self@Basic | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | Integral | Any | int" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
+ .../projects/sympy/sympy/stats/stochastic_process_types.py:1839:25 - error: Argument of type "tuple[Unknown, ...] | Unknown | Self@Basic | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | Integral | Any | int" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
- Type "tuple[Unknown, ...] | Unknown | Self@Basic | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | Integral | Any | int" is not assignable to type "Expr | complex"
+ Type "tuple[Unknown, ...] | Unknown | Self@Basic | Sum | Expr | ZeroMatrix | Add | Zero | NaN | Piecewise | Basic | Integral | Any | int" is not assignable to type "Expr | complex"
- Return type mismatch: base method returns type "Self@Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Self@Basic | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | Self@Expectation | Literal[0]"
+ Return type mismatch: base method returns type "Self@Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Self@Basic | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | Self@Expectation | Literal[0]"
... (truncated 139 lines) ...
xarray (https://github.com/pydata/xarray)
- Parameter name mismatch: "objs" versus "draw"
+ Parameter name mismatch: "objs" versus "draw" (reportInconsistentOverload)
- Extra parameter "objs" (reportInconsistentOverload)
- Parameter name mismatch: "objs" versus "draw"
+ Parameter name mismatch: "objs" versus "draw" (reportInconsistentOverload)
- Extra parameter "objs" (reportInconsistentOverload)
|
Summary
Fixes #10237, where a function assignment was not rejected because the keyword form's type was never checked against the source. The destination's positional-or-keyword parameter can be passed by a name the source only accepts through an incompatible
**kwargsor keyword-only parameter.Root cause
In
assignFunction, the main loop overpositionalsToMatchand the surplus-dest-positional branch both only checked for the presence of a source parameter'skwargsIndexto determine if the keyword home was satisfied. It did not check if the source keyword parameter was assignable to the dest positional-or-keyword parameter.assignFunctionalso did not fallback to dest positional-or-keyword parameters whenever a dest keyword-only parameter was not found. This caused control to fall into the!destParamInfobranch where, if the source parameter had adefaultArgType, assignability between src and dest was never checked, resulting in a false negative.Fix
Move the source
kwargsIndexcondition into the inner branch scope, and add anassignParamcheck for when sourcekwargsIndexis present.Add a dest positional-or-keyword parameter fallback to allow the assignability check between source keyword parameter and dest positional-or-keyword parameter.
Gate the new
assignParamchecks onPartialOverloadOverlap.Tests
callbackProtocol12which contains positive and negative assignments to a positional-or-keyword callback protocol for both kwargs and keyword-only source callback protocol forms.func35andfunc36inoverloadOverlap1to exercise the changes that involvePartialOverloadOverlapgating.