Repository navigation
Fix #11470: Pyright is incorrectly handling overload functions - #11498
Conversation
An overload implementation's signature is ignored by the type checker (only the @overload signatures define the function's type), so per the typing spec its parameters are allowed to remain unannotated. Skip reportUnknownParameterType and reportMissingParameterType for the implementation function's parameters. Fixes microsoft#11470 Co-authored-by: Copilot <[email protected]>
|
Diff from mypy_primer, showing the effect of this PR on open source code: sympy (https://github.com/sympy/sympy)
+ .../projects/sympy/sympy/stats/frv_types.py:139:17 - error: Argument of type "NaN | ComplexInfinity | Rational | Unknown | Expr" cannot be assigned to parameter "value" of type "int" in function "__setitem__"
+ Type "NaN | ComplexInfinity | Rational | Unknown | Expr" is not assignable to type "int"
+ "Expr" is not assignable to "int" (reportArgumentType)
- .../projects/sympy/sympy/stats/rv.py:473:25 - error: Argument of type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum | ZeroMatrix | NaN | Piecewise | Basic | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | int" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
- Type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum | ZeroMatrix | NaN | Piecewise | Basic | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | int" is not assignable to type "Expr | complex"
+ .../projects/sympy/sympy/stats/rv.py:473:25 - error: Argument of type "Expr | Lambda | Zero | One | Integral | Unknown | NegativeOne | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Probability | tuple[Unknown, ...] | Sum | ZeroMatrix | Add | Piecewise | Basic | int" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
+ Type "Expr | Lambda | Zero | One | Integral | Unknown | NegativeOne | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Probability | tuple[Unknown, ...] | Sum | ZeroMatrix | Add | Piecewise | Basic | int" is not assignable to type "Expr | complex"
- .../projects/sympy/sympy/stats/stochastic_process_types.py:1839:25 - error: Argument of type "tuple[Unknown, ...] | Unknown | 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 | 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 | Sum | Expr | ZeroMatrix | Zero | NaN | Piecewise | Basic | Integral | Any | int" is not assignable to type "Expr | complex"
+ Type "tuple[Unknown, ...] | Unknown | 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 | 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 | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | Self@Expectation | Literal[0]"
- Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | Expectation* | Literal[0]" is not assignable to type "Basic"
+ Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | Expectation* | Literal[0]" is not assignable to type "Basic"
+ .../projects/sympy/sympy/stats/symbolic_probability.py:75:9 - error: Method "doit" overrides class "Basic" in an incompatible manner
+ Return type mismatch: base method returns type "Basic", override returns type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | Add | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]"
+ Type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | Add | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]" is not assignable to type "Basic"
+ "Literal[0]" is not assignable to "Basic" (reportIncompatibleMethodOverride)
- Return type mismatch: base method returns type "Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | Literal[0]"
+ Return type mismatch: base method returns type "Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | Literal[0]"
- Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | Literal[0]" is not assignable to type "Basic"
+ Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | Literal[0]" is not assignable to type "Basic"
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:97:29 - error: Cannot access attribute "symbol" for class "ConditionalFiniteDomain"
+ Attribute "symbol" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:367:12 - error: Operator "-" not supported for types "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" and "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]"
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:367:12 - error: Operator "-" not supported for types "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" and "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]"
- .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:415:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:415:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
- Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" is not assignable to type "Basic"
+ Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" is not assignable to type "Basic"
- .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1005:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1005:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
- Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" is not assignable to type "Basic"
+ Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" is not assignable to type "Basic"
- .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1060:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1060:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
- Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" is not assignable to type "Basic"
+ Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | ZeroMatrix | Add | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0]" is not assignable to type "Basic"
... (truncated 612 lines) ...
|
| // signatures define the function's type). Per the typing spec, the | ||
| // implementation's parameters are allowed to remain unannotated, so | ||
| // skip the unknown/missing parameter type checks for it. | ||
| const isOverloadImplementation = |
There was a problem hiding this comment.
This uses a non-canonical detection idiom. The same file already exposes the explicit, intended API for exactly this question: OverloadedType.getImplementation(functionTypeResult.decoratedType) === functionTypeResult.functionType (used at checker.ts:718-719, and several times in typeUtils.ts). This change instead copies the looser heuristic from skipUnsolvableTypeVarCheck (checker.ts:2531-2533), creating a third inline copy of overload-implementation detection. Prefer extracting a single private helper (e.g. isOverloadImplementation(funcTypeResult)) built on getImplementation(...) and reuse it both here and at the existing heuristic site. Functionally equivalent today, but self-documenting and single-source-of-truth.
| // implementation's parameters are allowed to remain unannotated, so | ||
| // skip the unknown/missing parameter type checks for it. | ||
| const isOverloadImplementation = | ||
| isOverloaded(functionTypeResult.decoratedType) && |
There was a problem hiding this comment.
Please confirm the premise before merging. The justification "the implementation signature is ignored by the type checker" applies to call-site typing, but the implementation body is still type-checked and the param still resolves to Unknown. Pyright's documented reportMissingParameterType exemptions are only self/cls, with no mention of overload implementations, so this may be intentional strict-mode behavior rather than a false positive. Since this is an upstream pyright change to documented strict-mode semantics, verify with the Pyright maintainer that this is genuinely considered a false positive to avoid a likely rejection / local divergence.
|
Targeted fix with a focused regression test. Two notes worth addressing before merge: (1) prefer the canonical |
|
Fix looks correct for the reported overload-implementation false positives. Two follow-ups worth considering before merge: (1) the same 'implementation signature is ignored' rationale arguably applies to the impl's return type, which is not exempted here, and (2) per the repo's Pyright↔Pylance split, the primary regression test should live in the Pylance harness with the pyright-internal test as additional coverage. |
|
Fix looks correct and the predicate uniquely selects the overload implementation. Two non-blocking notes: (1) per the Pyright↔Pylance split policy, a pylance-release fix should have its primary regression test in the Pylance harness; (2) the same 'implementation signature is ignored' rationale arguably applies to the implementation's return type, so it's worth either extending the exemption or documenting that this is params-only. |
Heejae Chang (heejaechang)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Description
Fixes a false positive where Pyright reported
reportUnknownParameterTypeandreportMissingParameterTypeon the implementation of an overloaded function. Per the typing spec, only the@overloadsignatures define the function's public type; the implementation signature is ignored by the type checker, so its parameters are permitted to remain unannotated. Pyright was incorrectly flagging unannotated parameters on the implementation under strict mode.How you figured out what to do
The unknown/missing parameter type checks in
checker.tsran unconditionally for every function definition. Overload implementations were not exempted, even though their signatures carry no type-checking weight. The fix is to detect overload implementations and skip these two specific diagnostics for them.Implementation
In
packages/pyright-internal/src/analyzer/checker.ts, compute whether the current function is an overload implementation:The decorated type is overloaded (the symbol has
@overloadsignatures), but the function type itself is not marked overloaded — which uniquely identifies the implementation function. This guard is then added to thereportUnknownParameterTypeandreportMissingParameterTypechecks so they are skipped for the implementation only. All other functions (including non-overloaded ones and the@overloadsignatures themselves) continue to be checked as before.Testing
Added
packages/pyright-internal/src/tests/samples/overloadImpl3.pyunder# pyright: strict, covering:Foo.foo) with an unannotatedvalueparameter on the implementation — should produce no errors.func1) with an unannotated implementation parameter — should produce no errors.func2) with an unannotated parameter — should still produce both the unknown-type and missing-annotation errors (2 expected errors).Wired up
OverloadImpl3intypeEvaluator6.test.ts, asserting exactly 2 errors so the regression coverage confirms both that the implementation is exempt and that ordinary functions are unaffected.Run with:
Addresses
Fixes https://github.com/microsoft/pylance-release/issues/11470
Generated by fix_all_my_issues pipeline