Skip to content

Fix #11478: Support overloads on property setters - #11499

Merged
Rich Chiodo (rchiodo) merged 4 commits into
microsoft:mainfrom
rchiodo:fix11478
Jul 15, 2026
Merged

Rich Chiodo (rchiodo) merged 4 commits into
microsoft:mainfrom
rchiodo:fix11478

Conversation

@rchiodo

Copy link
Copy Markdown
Collaborator

Description

Adds support for @overload-decorated property setters. Previously a property's fset accessor could only hold a single FunctionType, so multiple @x.setter @overload declarations 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, and reveal_type all respect the overloaded setter.

How you figured out what to do

The PropertyMethodInfo.methodType field was typed as a single FunctionType, and addSetMethodToPropertySymbolTable synthesized exactly one __set__. Tracing how clonePropertyWithSetter rebuilds the property on each @x.setter declaration showed that earlier setter signatures were discarded rather than merged, which is why overloaded setters did not type-check.

Implementation

  • Broadened PropertyMethodInfo.methodType to FunctionType | OverloadedType, and updated all property-accessor consumers (checker.ts, typeEvaluator.ts, typeGuards.ts, typeUtils.ts, packageTypeVerifier.ts) to guard with isFunction(...) / handle the overloaded case.
  • Added combineSetterOverloads in properties.ts to accumulate setter overloads across declarations, treating a non-overloaded final setter as the implementation when overloads are present.
  • Reworked addSetMethodToPropertySymbolTable to synthesize one __set__ per setter overload (excluding the implementation), via the extracted createSetMethodFromSetter helper.

Testing

  • Added property19.py (overloaded setter accepts matching types, rejects str, deleter still works) and property20.py (deprecation resolves to the matching setter overload).
  • Wired up Property19 and Property20 cases in typeEvaluator8.test.ts, including a reportDeprecated = 'error' run.
  • Ran the relevant typeEvaluator8 property tests to confirm expected diagnostics.

Addresses

Fixes https://github.com/microsoft/pylance-release/issues/11478


Generated by fix_all_my_issues pipeline

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]>
@github-actions

This comment has been minimized.

@rchiodo

Copy link
Copy Markdown
Collaborator Author

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)
The added isFunction(...) guards make an OverloadedType setter fail the check, so property override validation is silently skipped for overloaded setters — a subclass that incompatibly overrides (or is overridden by) an overloaded-setter property won't get reportIncompatibleMethodOverride. Handle the overloaded case or fall back to the implementation signature, and add a test covering override of an overloaded-setter property (currently untested). [unverified] (structurally confirmed).

[verified]

@rchiodo

Copy link
Copy Markdown
Collaborator Author

Solid core change for overloaded setters. The main gap is that widening PropertyMethodInfo.methodType to FunctionType | OverloadedType was not fully threaded through the consumers: assignProperty and the property-override loops still narrow with isFunction(...), so overloaded setters get incorrect/skipped diagnostics. Worth completing those sites (and adding override coverage) plus tightening the new test's reveal_type to use expected_text=.

- 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]>
@github-actions

This comment has been minimized.

@heejaechang

Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

@github-actions

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]>
@github-actions

Copy link
Copy Markdown
Contributor

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) ...

@rchiodo
Rich Chiodo (rchiodo) merged commit 231b66e into microsoft:main Jul 15, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants