-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed bug that results in an "incompatible method override" false neg… #9707
Conversation
…ative when overriding a method that uses class-scoped type parameters with a method that uses method-scoped type parameters. This addresses #9405.
Diff from mypy_primer, showing the effect of this PR on open source code: pyodide (https://github.com/pyodide/pyodide)
- Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
- /tmp/mypy_primer/projects/pyodide/src/py/_pyodide/_core_docs.py:1105:9 - error: Method "pop" overrides class "MutableMapping" in an incompatible manner
- 749 errors, 14 warnings, 0 informations
+ 748 errors, 14 warnings, 0 informations
steam.py (https://github.com/Gobot1234/steam.py)
- /tmp/mypy_primer/projects/steam.py/steam/utils.py:565:9 - error: Method "pop" overrides class "ChainMap" in an incompatible manner
- Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
- /tmp/mypy_primer/projects/steam.py/steam/ext/commands/utils.py:49:9 - error: Method "get" overrides class "dict" in an incompatible manner
- Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
- 8443 errors, 93 warnings, 0 informations
+ 8441 errors, 93 warnings, 0 informations
sympy (https://github.com/sympy/sympy)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/autowrap.py:1131:30 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/autowrap.py:1132:28 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/matchpy_connector.py:288:16 - error: Type "Basic" is not assignable to declared type "Expr"
- "Basic" is not assignable to "Expr" (reportAssignmentType)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:54:33 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:54:39 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- Attribute "label" is unknown (reportAttributeAccessIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:55:54 - error: Cannot access attribute "label" for class "Basic"
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:55:63 - error: Cannot access attribute "label" for class "Basic"
- Attribute "label" is unknown (reportAttributeAccessIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:379:30 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:379:36 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:379:44 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:425:41 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:425:47 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:446:30 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:446:36 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:446:44 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1105:30 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1105:36 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1105:44 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1164:41 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1164:47 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1176:9 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1177:9 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1178:9 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1232:30 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1232:36 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1232:44 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1293:46 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen.py:1293:57 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_julia.py:484:43 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_julia.py:484:49 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_julia.py:484:57 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_julia.py:516:42 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_julia.py:516:48 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_julia.py:516:59 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_octave.py:459:43 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_octave.py:459:49 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_octave.py:459:57 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_octave.py:490:42 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_octave.py:490:48 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_codegen_octave.py:490:59 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_lambdify.py:1083:28 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_lambdify.py:1096:29 - error: "__getitem__" method not defined on type "Basic" (reportIndexIssue)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:120:18 - error: Argument of type "Equality | Unknown" cannot be assigned to parameter "expr" of type "Expr" in function "add"
+ /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:120:18 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer" cannot be assigned to parameter "expr" of type "Expr" in function "add"
- Type "Equality | Unknown" is not assignable to type "Expr"
+ Type "Equality | Unknown | Relational | Unequality | Integer" is not assignable to type "Expr"
- "Equality" is not assignable to "Expr" (reportArgumentType)
+ "Relational" is not assignable to "Expr" (reportArgumentType)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:123:18 - error: Argument of type "Equality | Unknown" cannot be assigned to parameter "expr" of type "Expr" in function "add"
+ /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:123:18 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer" cannot be assigned to parameter "expr" of type "Expr" in function "add"
- Type "Equality | Unknown" is not assignable to type "Expr"
+ Type "Equality | Unknown | Relational | Unequality | Integer" is not assignable to type "Expr"
- "Equality" is not assignable to "Expr" (reportArgumentType)
+ "Relational" is not assignable to "Expr" (reportArgumentType)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:126:9 - error: Argument of type "Equality | Unknown" cannot be assigned to parameter "expr" of type "Expr" in function "add"
+ /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:126:9 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer" cannot be assigned to parameter "expr" of type "Expr" in function "add"
- Type "Equality | Unknown" is not assignable to type "Expr"
+ Type "Equality | Unknown | Relational | Unequality | Integer" is not assignable to type "Expr"
- "Equality" is not assignable to "Expr" (reportArgumentType)
+ "Relational" is not assignable to "Expr" (reportArgumentType)
- /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:132:9 - error: Argument of type "Equality | Unknown" cannot be assigned to parameter "expr" of type "Expr" in function "add"
+ /tmp/mypy_primer/projects/sympy/sympy/utilities/tests/test_matchpy_connector.py:132:9 - error: Argument of type "Equality | Unknown | Relational | Unequality | Integer" cannot be assigned to parameter "expr" of type "Expr" in function "add"
- Type "Equality | Unknown" is not assignable to type "Expr"
+ Type "Equality | Unknown | Relational | Unequality | Integer" is not assignable to type "Expr"
- "Equality" is not assignable to "Expr" (reportArgumentType)
+ "Relational" is not assignable to "Expr" (reportArgumentType)
... (truncated 125 lines) ...
mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ /tmp/mypy_primer/projects/mongo-python-driver/bson/son.py:126:9 - error: Method "pop" overrides class "dict" in an incompatible manner
+ Return type mismatch: base method returns type "_Value@SON", override returns type "_Value@SON | _T@pop"
+ Type "_Value@SON | _T@pop" is not assignable to type "_Value@SON" (reportIncompatibleMethodOverride)
- 1167 errors, 28 warnings, 0 informations
+ 1168 errors, 28 warnings, 0 informations
bokeh (https://github.com/bokeh/bokeh)
+ /tmp/mypy_primer/projects/bokeh/src/bokeh/core/property/container.py:130:9 - error: Method "wrap" overrides class "Property" in an incompatible manner
+ Parameter 2 type mismatch: base parameter is type "T@List", override parameter is type "list[T@List]"
+ Return type mismatch: base method returns type "T@List", override returns type "PropertyValueList[T@List]"
+ "object*" is not assignable to "list[T@List]"
+ Type "PropertyValueList[T@List]" is not assignable to type "T@List" (reportIncompatibleMethodOverride)
+ /tmp/mypy_primer/projects/bokeh/src/bokeh/core/property/container.py:157:9 - error: Method "wrap" overrides class "Property" in an incompatible manner
+ Parameter 2 type mismatch: base parameter is type "T@Set", override parameter is type "set[T@Set]"
+ Return type mismatch: base method returns type "T@Set", override returns type "PropertyValueSet[T@Set]"
+ "object*" is not assignable to "set[T@Set]"
+ Type "PropertyValueSet[T@Set]" is not assignable to type "T@Set" (reportIncompatibleMethodOverride)
- "SupportsIndex" is not assignable to "int" (reportIncompatibleMethodOverride)
+ Return type mismatch: base method returns type "T@PropertyValueList", override returns type "T@PropertyValueList"
+ "SupportsIndex" is not assignable to "int"
+ Type "T@PropertyValueList" is not assignable to type "T@PropertyValueList" (reportIncompatibleMethodOverride)
+ /tmp/mypy_primer/projects/bokeh/src/bokeh/core/property/wrappers.py:371:9 - error: Method "pop" overrides class "dict" in an incompatible manner
+ Return type mismatch: base method returns type "T_Val@PropertyValueDict", override returns type "T_Val@PropertyValueDict"
+ Type "T_Val@PropertyValueDict" is not assignable to type "T_Val@PropertyValueDict" (reportIncompatibleMethodOverride)
+ /tmp/mypy_primer/projects/bokeh/src/bokeh/core/property/wrappers.py:375:9 - error: Method "popitem" overrides class "MutableMapping" in an incompatible manner
+ Return type mismatch: base method returns type "tuple[str, T_Val@PropertyValueDict]", override returns type "tuple[str, T_Val@PropertyValueDict]"
+ "builtins.tuple" is not assignable to "builtins.tuple"
+ Tuple entry 2 is incorrect type
+ Type "T_Val@PropertyValueDict" is not assignable to type "T_Val@PropertyValueDict" (reportIncompatibleMethodOverride)
+ /tmp/mypy_primer/projects/bokeh/src/bokeh/core/property/wrappers.py:379:9 - error: Method "setdefault" overrides class "MutableMapping" in an incompatible manner
+ Return type mismatch: base method returns type "T_Val@PropertyValueDict", override returns type "T_Val@PropertyValueDict"
+ Type "T_Val@PropertyValueDict" is not assignable to type "T_Val@PropertyValueDict" (reportIncompatibleMethodOverride)
- 3552 errors, 294 warnings, 0 informations
+ 3557 errors, 294 warnings, 0 informations
mkdocs (https://github.com/mkdocs/mkdocs)
+ /tmp/mypy_primer/projects/mkdocs/mkdocs/config/config_options.py:170:9 - error: Method "validate" overrides class "BaseConfigOption" in an incompatible manner
+ Return type mismatch: base method returns type "T@OptionallyRequired", override returns type "object | None"
+ Type "object | None" is not assignable to type "T@OptionallyRequired" (reportIncompatibleMethodOverride)
- 199 errors, 23 warnings, 0 informations
+ 200 errors, 23 warnings, 0 informations
prefect (https://github.com/PrefectHQ/prefect)
+ /tmp/mypy_primer/projects/prefect/src/prefect/_internal/concurrency/waiters.py:141:9 - error: Method "wait" overrides class "Waiter" in an incompatible manner
+ Return type mismatch: base method returns type "T@SyncWaiter", override returns type "Call[T@SyncWaiter]"
+ Type "Call[T@SyncWaiter]" is not assignable to type "T@SyncWaiter" (reportIncompatibleMethodOverride)
+ /tmp/mypy_primer/projects/prefect/src/prefect/_internal/concurrency/waiters.py:259:15 - error: Method "wait" overrides class "Waiter" in an incompatible manner
+ Return type mismatch: base method returns type "T@AsyncWaiter", override returns type "Coroutine[Any, Any, Call[T@AsyncWaiter]]"
+ Type "Coroutine[Any, Any, Call[T@AsyncWaiter]]" is not assignable to type "T@AsyncWaiter" (reportIncompatibleMethodOverride)
- 8274 errors, 283 warnings, 0 informations
+ 8276 errors, 283 warnings, 0 informations
werkzeug (https://github.com/pallets/werkzeug)
+ /tmp/mypy_primer/projects/werkzeug/src/werkzeug/datastructures/structures.py:910:9 - error: Method "items" overrides class "MultiDict" in an incompatible manner
+ No overload signature in override is compatible with base method (reportIncompatibleMethodOverride)
- 109 errors, 9 warnings, 0 informations
+ 110 errors, 9 warnings, 0 informations
bidict (https://github.com/jab/bidict)
- /tmp/mypy_primer/projects/bidict/bidict/_bidict.py
- /tmp/mypy_primer/projects/bidict/bidict/_bidict.py:126:9 - error: Method "pop" overrides class "MutableMapping" in an incompatible manner
- Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
- 29 errors, 3 warnings, 0 informations
+ 28 errors, 3 warnings, 0 informations
|
@@ -521,10 +521,20 @@ def method1(self, x: T) -> T: | |||
|
|||
|
|||
class Derived7_1(Base7[T]): | |||
# This should generate an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain why this should generate an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider the following:
d = Derived7_1[int]()
d.method1("")
def func(b: Base7[int]):
b.method1(1)
func(d)
For an instance of Derived7_1[int]
, the specialized type of the base class method is (int) -> int
, and the specialized type of the derived class method is (str) -> str
. It's a Liskov violation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Derived7_1.method1
extends Base7.method1
by making it polymorphic. (S@method1) -> S@method1
is assignable to (T) -> T
for any concrete T
that comes from the generic specialization, isn't it? I don't know what the intended behavior is, but it feels inconsistent with what was implemented in #9417. That's why I decided to point that out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is different from the case you reported in #9417 because that didn't involve a class-scoped type variable in the base class. In this case, the override is unsound as demonstrated in the sample above.
…ative when overriding a method that uses class-scoped type parameters with a method that uses method-scoped type parameters. This addresses #9405.