Repository navigation
Fix #11475: Cannot override a class's callable variable with a method - #11500
Merged
Merged
Conversation
When a base class declares a plain callable variable (e.g. `x: Callable[..., None]`) and a subclass overrides it with a method, bind the override method's `self` parameter before comparing signatures. Accessing the override on an instance binds `self`, so the signatures are compatible. This matches the behavior of mypy and ty. Fixes microsoft#11475 Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
Collaborator
Author
|
Looks good overall — the bound-comparison approach is reasonable and matches other type checkers. A few non-blocking suggestions: hoist the new declaration scans inside the callable-variable guard to avoid extra work on the common override path, and broaden the negative test beyond arity (return-type and param-type mismatches). |
…override - Compare the bound override and base callable as plain (static) functions so the override comparison no longer skips the first real parameter as an unbound self. This catches first-parameter type mismatches that were previously dropped. - Apply the same bind-before-compare handling to the multiple-inheritance override path, fixing a spurious reportIncompatibleMethodOverride for callable-variable-vs-method diamonds. - Extract the shared logic into _getCallableVariableOverrideComparison and hoist the declaration scans inside the function-typed guard so the common override path pays nothing extra. - Strengthen methodOverride7 with param-type and return-type negative cases plus diamond regression coverage. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
Collaborator
|
Looks good overall — the fix correctly binds the override method's self only when the base member is a callable variable. A few non-blocking notes below about overloaded-override handling and test/code-path coverage. |
Heejae Chang (heejaechang)
approved these changes
Jun 29, 2026
Heejae Chang (heejaechang)
left a comment
Collaborator
There was a problem hiding this comment.
Approved via Review Center.
… multi-inheritance test, cross-reference self-skip Co-authored-by: Copilot <[email protected]>
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/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
+ .../projects/sympy/sympy/solvers/ode/single.py:867:9 - error: Expression with type "tuple[Unknown | Any | NaN | Expr | Infinity | NegativeInfinity | Zero | Float, list[tuple[Unknown, Unknown]] | list[Unknown]] | tuple[Unknown | Any | NaN | Expr | Infinity | NegativeInfinity | Zero | Float, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" cannot be assigned to target tuple
+ Type "tuple[Unknown | Any | NaN | Expr | Infinity | NegativeInfinity | Zero | Float, list[tuple[Unknown, Unknown]] | list[Unknown], list[tuple[Unknown, Unknown]] | list[Unknown]]" is incompatible with target tuple
- .../projects/sympy/sympy/solvers/ode/single.py:1404:20 - error: Operator "/" not supported for types "Basic" and "Basic" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/single.py:1883:22 - error: "is_Rational" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/single.py:1886:22 - error: "is_Rational" is not a known attribute of "None" (reportOptionalMemberAccess)
- .../projects/sympy/sympy/solvers/ode/single.py:1905:25 - error: Operator "*" not supported for types "Zero | One | Expr | Unknown | None" and "Zero | One | Expr | Unknown | None"
- Operator "*" not supported for types "Expr" and "None"
- Operator "*" not supported for types "None" and "Expr"
- Operator "*" not supported for types "None" and "None" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/single.py:1905:33 - error: Operator "*" not supported for types "Zero | One | Expr | Unknown | None" and "Zero | One | Expr | Unknown | None"
- Operator "*" not supported for types "Expr" and "None"
- Operator "*" not supported for types "None" and "Expr"
- Operator "*" not supported for types "None" and "None" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/single.py:1914:21 - error: Operator "*" not supported for "None" (reportOptionalOperand)
- .../projects/sympy/sympy/solvers/ode/single.py:1914:29 - error: Operator "*" not supported for "None" (reportOptionalOperand)
- .../projects/sympy/sympy/solvers/ode/single.py:1914:44 - error: Operator "*" not supported for "None" (reportOptionalOperand)
- .../projects/sympy/sympy/solvers/ode/single.py:1914:52 - error: Operator "*" not supported for "None" (reportOptionalOperand)
+ .../projects/sympy/sympy/solvers/ode/single.py:2646:27 - error: Argument of type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Unknown | int" 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 | Unknown | int" is not assignable to type "SupportsIndex"
+ "Expr" is incompatible with protocol "SupportsIndex"
+ "__index__" is not present (reportArgumentType)
- .../projects/sympy/sympy/solvers/ode/single.py:2652:9 - error: Operator "+=" not supported for types "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Any | Unknown" and "Basic"
+ .../projects/sympy/sympy/solvers/ode/single.py:2652:9 - error: Operator "+=" not supported for types "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | Expr | Any | Unknown" and "Basic | Any | Unknown"
- .../projects/sympy/sympy/solvers/ode/single.py:2760:30 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/subscheck.py:150:48 - error: Argument of type "int | Unknown | str" cannot be assigned to parameter "order" of type "str" in function "checkodesol"
- Type "int | Unknown | str" is not assignable to type "str"
- "int" is not assignable to "str" (reportArgumentType)
+ .../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/crv_types.py:2544:9 - error: Method "_cdf" overrides class "SingleContinuousDistribution" in an incompatible manner
+ Return type mismatch: base method returns type "None", override returns type "ComplexInfinity | NaN | Rational | Unknown | Zero | Expr"
+ Type "ComplexInfinity | NaN | Rational | Unknown | Zero | Expr" is not assignable to type "None"
+ "Expr" is not assignable to "None" (reportIncompatibleMethodOverride)
+ .../projects/sympy/sympy/stats/crv_types.py:2723:9 - error: Method "_cdf" overrides class "SingleContinuousDistribution" in an incompatible manner
+ Return type mismatch: base method returns type "None", override returns type "Expr | Unknown | NaN | ComplexInfinity | Rational | One | NegativeOne | Zero | Integer"
+ Type "Expr | Unknown | NaN | ComplexInfinity | Rational | One | NegativeOne | Zero | Integer" is not assignable to type "None"
+ "Expr" is not assignable to "None" (reportIncompatibleMethodOverride)
+ .../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/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__"
+ .../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__"
- Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Unknown | Infinity | NegativeInfinity | Float | Number | Expr" is not assignable to type "SupportsIndex"
+ Type "One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | 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 | Unknown"
+ .../projects/sympy/sympy/stats/rv_interface.py:451:42 - error: Operator "-" not supported for types "Unknown | Basic | Any" and "Rational | NaN | ComplexInfinity"
+ Operator "-" not supported for types "Basic" and "Rational"
+ Operator "-" not supported for types "Basic" and "NaN"
- Operator "-" not supported for types "Basic" and "Rational" (reportOperatorIssue)
+ Operator "-" not supported for types "Basic" and "ComplexInfinity" (reportOperatorIssue)
- .../projects/sympy/sympy/stats/tests/test_finite_rv.py:35:23 - error: Operator "/" not supported for types "Unknown | Any | One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Expr | Infinity | NegativeInfinity | Float | NotImplementedType | tuple[Unknown, ...] | MatMul | int" 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 | One | NegativeOne | Zero | Integer | NaN | ComplexInfinity | Rational | Expr | tuple[Unknown, ...] | MatMul | Infinity | NegativeInfinity | int" 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:248:19 - error: Argument of type "Rational | Unknown" cannot be assigned to parameter "p" of type "Half" in function "Coin"
+ .../projects/sympy/sympy/stats/tests/test_finite_rv.py:248:19 - error: Argument of type "Rational | NaN | ComplexInfinity" cannot be assigned to parameter "p" of type "Half" in function "Coin"
- Type "Rational | Unknown" is not assignable to type "Half"
+ Type "Rational | NaN | ComplexInfinity" is not assignable to type "Half"
- "Rational" is not assignable to "Half" (reportArgumentType)
... (truncated 544 lines) ...
core (https://github.com/home-assistant/core)
- .../projects/core/homeassistant/components/mqtt/device_tracker.py:160:9 - error: Method "_process_update_extra_state_attributes" overrides class "MqttAttributesMixin" in an incompatible manner
- Positional parameter count mismatch; base method has 1, but override has 2 (reportIncompatibleMethodOverride)
- .../projects/core/homeassistant/components/mqtt/entity.py:1665:9 - error: Method "_message_callback" overrides class "MqttAttributesMixin" in an incompatible manner
- Positional parameter count mismatch; base method has 3, but override has 4
- Parameter 2 type mismatch: base parameter is type "set[str] | None", override parameter is type "MessageCallbackType"
- Parameter 3 type mismatch: base parameter is type "ReceiveMessage", override parameter is type "set[str] | None"
- Type "set[str] | None" is not assignable to type "MessageCallbackType"
- Type "set[str]" is not assignable to type "MessageCallbackType"
- Type "ReceiveMessage" is not assignable to type "set[str] | None"
- "ReceiveMessage" is not assignable to "set[str]"
- "ReceiveMessage" is not assignable to "None" (reportIncompatibleMethodOverride)
- .../projects/core/homeassistant/components/mqtt/entity.py:1665:9 - error: Method "_message_callback" overrides class "MqttAvailabilityMixin" in an incompatible manner
- Positional parameter count mismatch; base method has 3, but override has 4
- Parameter 2 type mismatch: base parameter is type "set[str] | None", override parameter is type "MessageCallbackType"
- Parameter 3 type mismatch: base parameter is type "ReceiveMessage", override parameter is type "set[str] | None"
- Type "set[str] | None" is not assignable to type "MessageCallbackType"
- Type "set[str]" is not assignable to type "MessageCallbackType"
- Type "ReceiveMessage" is not assignable to type "set[str] | None"
- "ReceiveMessage" is not assignable to "set[str]"
- "ReceiveMessage" is not assignable to "None" (reportIncompatibleMethodOverride)
- 37058 errors, 76 warnings, 0 informations
+ 37055 errors, 76 warnings, 0 informations
dedupe (https://github.com/dedupeio/dedupe)
- .../projects/dedupe/dedupe/variables/exact.py
- .../projects/dedupe/dedupe/variables/exact.py:12:9 - error: Method "comparator" overrides class "FieldType" in an incompatible manner
- Base method is declared as an instance method but override is not (reportIncompatibleMethodOverride)
- .../projects/dedupe/dedupe/variables/exists.py:25:9 - error: Method "comparator" overrides class "FieldType" in an incompatible manner
- Positional parameter count mismatch; base method has 2, but override has 3 (reportIncompatibleMethodOverride)
- .../projects/dedupe/dedupe/variables/latlong.py:17:9 - error: Method "comparator" overrides class "FieldType" in an incompatible manner
- Base method is declared as an instance method but override is not (reportIncompatibleMethodOverride)
- .../projects/dedupe/dedupe/variables/price.py
- .../projects/dedupe/dedupe/variables/price.py:18:9 - error: Method "comparator" overrides class "FieldType" in an incompatible manner
- Base method is declared as an instance method but override is not (reportIncompatibleMethodOverride)
- 56 errors, 1 warning, 0 informations
+ 52 errors, 1 warning, 0 informations
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Pylance/Pyright incorrectly flagged an error when a subclass overrode a base class's callable variable (e.g.
x: Callable[[], None]) with a real method. Other type checkers allow this, because when you access the override method on an instance, itsselfparameter is bound, leaving a signature that matches the base callable attribute.How you figured out what to do
The base-class member here is a plain
Callableattribute, not a method, so it has no implicitself. The override, being a method, does declareself. The override check compared the unbound method signature (which still hasself) against the base callable, producing a spuriousreportIncompatibleMethodOverrideerror. The fix is to bind the override method to the class before comparing, but only when the base member is actually a callable variable.Implementation
In
checker.ts, within the override-validation path for function-typed members:DeclarationType.Variable) and the override is a method (DeclarationType.Function).selfviabindFunctionToClassOrObjectand use the bound type forvalidateOverrideMethod.Testing
Added
methodOverride7.pysample plus aMethodOverride7test intypeEvaluator3.test.ts(withreportIncompatibleMethodOverride = 'error'):class B(A)overrides callable attributeshello/cbwith compatible methods — no error.class C(A)overrideshellowith an incompatible signature (extra required parameter) — still produces exactly 1 error, confirming the bound comparison continues to catch genuine mismatches.Ran the targeted Pyright type-evaluator tests to confirm the new case passes and the negative case still errors.
Addresses
Fixes https://github.com/microsoft/pylance-release/issues/11475
Generated by fix_all_my_issues pipeline