Repository navigation
Fix #11478: Support overloads on property setters - #11499
Conversation
Property setters can now be overloaded using @overload. Previously, only the final implementation setter was used for type checking, so assignments were validated against the implementation signature and the overload signatures were ignored. The setter overloads are now accumulated across declarations and a synthesized overloaded __set__ method is built, mirroring how overloaded __get__ is synthesized. Per-overload metadata such as @deprecated also propagates. PropertyMethodInfo.methodType is widened to FunctionType | OverloadedType, and the consumers of fget/fset/fdel method types are updated to handle the overloaded case. Fixes microsoft#11478 Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
|
GitHub cannot anchor PR review comments to unchanged lines in the diff. Falling back to a general PR comment for packages/pyright-internal/src/analyzer/checker.ts:L6075. 📍 packages/pyright-internal/src/analyzer/checker.ts (_validatePropertyOverride / _validateMultipleInheritancePropertyOverride) [verified] |
|
Solid core change for overloaded setters. The main gap is that widening |
- Handle overloaded accessors in assignProperty so overloaded setters are compared (not skipped) during protocol/assignment checks, avoiding a false-positive missingSetter and a silently skipped setter-compat check. - Propagate an @deprecated setter implementation to all setter overloads in combineSetterOverloads, per PEP 702 (mirrors addOverloadsToFunctionType). - Strip the StaticMethod flag from each signature of overloaded accessors in packageTypeVerifier to avoid spurious verifytypes 'self not annotated' noise. - Document the intentional limitation that overloaded accessors are not yet validated in the property override-compatibility checks. - Clarify edge-case assumptions in combineSetterOverloads / addSetMethodToPropertySymbolTable. - Tighten property19 reveal_type to expected_text and add property21 covering implementation-level setter deprecation. Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
|
Solid implementation with strong test design (the dual-run reportDeprecated pattern is nice). Two non-blocking notes below worth a look; otherwise this looks good to merge. |
Heejae Chang (heejaechang)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
…rty setter samples Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
Adds property22.py to cover a setter with exactly one @overload plus an implementation (the synthesized __set__ single-overload shape), and property23.py to document that override-compatibility checking is not yet performed for overloaded property accessors. 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/solvers/tests/test_solveset.py:445:30 - error: Cannot access attribute "limit_denominator" for class "NaN"
- Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/tests/test_solveset.py:445:30 - error: Cannot access attribute "limit_denominator" for class "ComplexInfinity"
- Attribute "limit_denominator" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/stats/joint_rv_types.py:576:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
+ .../projects/sympy/sympy/stats/joint_rv_types.py:576:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Infinity | NegativeInfinity | Float | Number | Expr" cannot be assigned to parameter "stop" of type "SupportsIndex" in function "__new__"
- Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "SupportsIndex"
+ Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "SupportsIndex"
- .../projects/sympy/sympy/stats/rv_interface.py:451:42 - error: Operator "-" not supported for types "Unknown | Basic | Any" and "Rational | NaN | ComplexInfinity"
+ .../projects/sympy/sympy/stats/rv_interface.py:451:42 - error: Operator "-" not supported for types "Unknown | Basic | Any" and "Rational | Unknown"
- Operator "-" not supported for types "Basic" and "Rational"
- Operator "-" not supported for types "Basic" and "NaN"
- Operator "-" not supported for types "Basic" and "ComplexInfinity" (reportOperatorIssue)
+ Operator "-" not supported for types "Basic" and "Rational" (reportOperatorIssue)
+ .../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 | 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 | 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)
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1543:12 - error: No overloads for "simplify" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1543:21 - error: Argument of type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+ Type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | 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" (reportArgumentType)
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1545:12 - error: No overloads for "simplify" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1545:21 - error: Argument of type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+ Type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | 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" (reportArgumentType)
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1550:12 - error: No overloads for "simplify" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/stats/tests/test_continuous_rv.py:1550:21 - error: Argument of type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+ Type "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | 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" (reportArgumentType)
- .../projects/sympy/sympy/stats/tests/test_finite_rv.py:34:23 - error: Operator "/" not supported for types "Unknown | Any | BernoulliDistribution | Probability | Zero | One | Expr | Float | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | NotImplementedType | tuple[Unknown, ...] | Sum | ZeroMatrix | Piecewise | Basic | Integer | Lambda | Mul | NegativeOne | Number | Integral | Literal[0]" and "Unknown | Any | BernoulliDistribution | Probability | Zero | One | Expr | Float | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | NotImplementedType | tuple[Unknown, ...] | Sum | ZeroMatrix | Piecewise | Basic | Integer | Lambda | Mul | NegativeOne | Number | Integral | Literal[0]"
+ .../projects/sympy/sympy/stats/tests/test_finite_rv.py:34:23 - error: Operator "/" not supported for types "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]" and "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]"
- .../projects/sympy/sympy/stats/tests/test_finite_rv.py:35:23 - error: Operator "*" not supported for types "Unknown | Any | BernoulliDistribution | Probability | Zero | One | Expr | Float | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | NotImplementedType | tuple[Unknown, ...] | Sum | ZeroMatrix | Piecewise | Basic | Integer | Lambda | Mul | NegativeOne | Number | Integral | Literal[0]" and "Unknown | Any | BernoulliDistribution | Probability | Zero | One | Expr | Float | ComplexInfinity | NaN | Rational | Infinity | NegativeInfinity | NotImplementedType | tuple[Unknown, ...] | Sum | ZeroMatrix | Piecewise | Basic | Integer | Lambda | Mul | NegativeOne | Number | Integral | Literal[0]"
+ .../projects/sympy/sympy/stats/tests/test_finite_rv.py:35:23 - error: Operator "*" not supported for types "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]" and "Unknown | Any | BernoulliDistribution | Probability | Zero | One | tuple[Unknown, ...] | Sum | Expr | ZeroMatrix | NaN | Piecewise | Basic | ComplexInfinity | Float | Infinity | Integer | Lambda | Mul | NegativeInfinity | NegativeOne | Number | Rational | Integral | Literal[0]"
+ Operator "*" not supported for types "BernoulliDistribution" and "tuple[Unknown, ...]"
+ Operator "*" not supported for types "BernoulliDistribution" and "Sum"
- Operator "*" not supported for types "BernoulliDistribution" and "Float"
+ Operator "*" not supported for types "BernoulliDistribution" and "ZeroMatrix"
- Operator "*" not supported for types "BernoulliDistribution" and "ComplexInfinity"
- Operator "*" not supported for types "BernoulliDistribution" and "Rational"
- Operator "*" not supported for types "BernoulliDistribution" and "Infinity"
+ Operator "*" not supported for types "BernoulliDistribution" and "Piecewise"
... (truncated 138 lines) ...
|
Description
Adds support for
@overload-decorated property setters. Previously a property'sfsetaccessor could only hold a singleFunctionType, so multiple@x.setter @overloaddeclarations were not combined and the synthesized__set__method ignored the overload signatures. This change accumulates setter overloads and synthesizes one__set__overload per setter signature, so assignment type-checking, deprecation reporting, andreveal_typeall respect the overloaded setter.How you figured out what to do
The
PropertyMethodInfo.methodTypefield was typed as a singleFunctionType, andaddSetMethodToPropertySymbolTablesynthesized exactly one__set__. Tracing howclonePropertyWithSetterrebuilds the property on each@x.setterdeclaration showed that earlier setter signatures were discarded rather than merged, which is why overloaded setters did not type-check.Implementation
PropertyMethodInfo.methodTypetoFunctionType | OverloadedType, and updated all property-accessor consumers (checker.ts,typeEvaluator.ts,typeGuards.ts,typeUtils.ts,packageTypeVerifier.ts) to guard withisFunction(...)/ handle the overloaded case.combineSetterOverloadsinproperties.tsto accumulate setter overloads across declarations, treating a non-overloaded final setter as the implementation when overloads are present.addSetMethodToPropertySymbolTableto synthesize one__set__per setter overload (excluding the implementation), via the extractedcreateSetMethodFromSetterhelper.Testing
property19.py(overloaded setter accepts matching types, rejectsstr, deleter still works) andproperty20.py(deprecation resolves to the matching setter overload).Property19andProperty20cases intypeEvaluator8.test.ts, including areportDeprecated = 'error'run.typeEvaluator8property tests to confirm expected diagnostics.Addresses
Fixes https://github.com/microsoft/pylance-release/issues/11478
Generated by fix_all_my_issues pipeline