microsoft / pyright

Static Type Checker for Python
Other
13.12k stars 1.4k forks source link

Fixed bug that results in a false negative `reportIncompatibleMethodO… #8711

Closed erictraut closed 1 month ago

erictraut commented 1 month ago

…verride` when the child class method uses a type parameter that is scoped to the child class. This addresses #8622.

github-actions[bot] commented 1 month ago

Diff from mypy_primer, showing the effect of this PR on open source code:

mypy (https://github.com/python/mypy)
+ /tmp/mypy_primer/projects/mypy/mypy/typeshed/stdlib/shelve.pyi
+   /tmp/mypy_primer/projects/mypy/mypy/typeshed/stdlib/shelve.pyi:25:9 - error: Method "get" overrides class "Mapping" in an incompatible manner
+     Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
+     Parameter 3 type mismatch: base parameter is type "None", override parameter is type "_VT@WeakValueDictionary"
-     Parameter 3 mismatch: base parameter has default argument value, override parameter does not (reportIncompatibleMethodOverride)
+     Parameter 3 mismatch: base parameter has default argument value, override parameter does not
+       Type "None" is incompatible with type "_VT@WeakValueDictionary" (reportIncompatibleMethodOverride)
-       Type "None" is incompatible with constrained type variable "AnyStr" (reportIncompatibleMethodOverride)
+       Type "None" is incompatible with type "AnyStr@_Environ" (reportIncompatibleMethodOverride)
- 1738 errors, 108 warnings, 0 informations 
+ 1739 errors, 108 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:125:9 - error: Method "pop" overrides class "MutableMapping" in an incompatible manner
-     Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
- 36 errors, 3 warnings, 0 informations 
+ 35 errors, 3 warnings, 0 informations 

steam.py (https://github.com/Gobot1234/steam.py)
-   /tmp/mypy_primer/projects/steam.py/steam/utils.py:539: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)
- 8470 errors, 95 warnings, 0 informations 
+ 8468 errors, 95 warnings, 0 informations 

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+     Parameter 3 type mismatch: base parameter is type "None", override parameter is type "_Value@SON"
-     Parameter 3 mismatch: base parameter has default argument value, override parameter does not (reportIncompatibleMethodOverride)
+     Parameter 3 mismatch: base parameter has default argument value, override parameter does not
+       Type "None" is incompatible with type "_Value@SON" (reportIncompatibleMethodOverride)
+   /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 incompatible with type "_Value@SON" (reportIncompatibleMethodOverride)
- 1092 errors, 27 warnings, 0 informations 
+ 1093 errors, 27 warnings, 0 informations 

werkzeug (https://github.com/pallets/werkzeug)
-   /tmp/mypy_primer/projects/werkzeug/src/werkzeug/datastructures/mixins.pyi:87:9 - error: Method "pop" overrides class "dict" in an incompatible manner
-     Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
-   /tmp/mypy_primer/projects/werkzeug/src/werkzeug/datastructures/structures.pyi:38:9 - error: Method "get" overrides class "dict" in an incompatible manner
-     Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
-   /tmp/mypy_primer/projects/werkzeug/src/werkzeug/datastructures/structures.pyi:77:9 - error: Method "pop" overrides class "dict" in an incompatible manner
-     Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
-   /tmp/mypy_primer/projects/werkzeug/src/werkzeug/datastructures/structures.pyi:137:9 - error: Method "get" overrides class "dict" in an incompatible manner
-     Override does not handle all overloads of base method (reportIncompatibleMethodOverride)
- 151 errors, 10 warnings, 0 informations 
+ 147 errors, 10 warnings, 0 informations 

bokeh (https://github.com/bokeh/bokeh)
+     Return type mismatch: base method returns type "T@List", override returns type "PropertyValueList[T@List]"
+       "object*" is incompatible with "list[T@List]"
-       "object*" is incompatible with "list[T@List]" (reportIncompatibleMethodOverride)
+       Type "PropertyValueList[T@List]" is incompatible with type "T@List" (reportIncompatibleMethodOverride)
+     Return type mismatch: base method returns type "T@Set", override returns type "PropertyValueSet[T@Set]"
+       "object*" is incompatible with "set[T@Set]"
-       "object*" is incompatible with "set[T@Set]" (reportIncompatibleMethodOverride)
+       Type "PropertyValueSet[T@Set]" is incompatible with type "T@Set" (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/bokeh/src/bokeh/core/property/wrappers.py:229:9 - error: Method "__iadd__" overrides class "list" in an incompatible manner
+     Return type mismatch: base method returns type "PropertyValueList[T@PropertyValueList]", override returns type "Self@PropertyValueList[T@PropertyValueList]"
+       "PropertyValueList[T@PropertyValueList]*" is incompatible with "PropertyValueList[T@PropertyValueList]"
+         Type parameter "T@PropertyValueList" is invariant, but "T@PropertyValueList" is not the same as "T@PropertyValueList" (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/bokeh/src/bokeh/core/property/wrappers.py:234:9 - error: Method "__imul__" overrides class "list" in an incompatible manner
+     Return type mismatch: base method returns type "PropertyValueList[T@PropertyValueList]", override returns type "Self@PropertyValueList[T@PropertyValueList]"
+       "PropertyValueList[T@PropertyValueList]*" is incompatible with "PropertyValueList[T@PropertyValueList]"
+         Type parameter "T@PropertyValueList" is invariant, but "T@PropertyValueList" is not the same as "T@PropertyValueList" (reportIncompatibleMethodOverride)
-       "SupportsIndex" is incompatible with "int" (reportIncompatibleMethodOverride)
+     Return type mismatch: base method returns type "T@PropertyValueList", override returns type "T@PropertyValueList"
+       "SupportsIndex" is incompatible with "int"
+       Type "T@PropertyValueList" is incompatible with 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 incompatible with 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 incompatible with "builtins.tuple"
+         Tuple entry 2 is incorrect type
+           Type "T_Val@PropertyValueDict" is incompatible with 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 incompatible with type "T_Val@PropertyValueDict" (reportIncompatibleMethodOverride)
- 3512 errors, 305 warnings, 0 informations 
+ 3517 errors, 305 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 incompatible with type "T@OptionallyRequired" (reportIncompatibleMethodOverride)
- 198 errors, 23 warnings, 0 informations 
+ 199 errors, 23 warnings, 0 informations 

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1509:9 - error: Method "__and__" overrides class "OpsMixin" in an incompatible manner
+     No overload signature in override is compatible with base method (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1514:9 - error: Method "__ge__" overrides class "OpsMixin" in an incompatible manner
+     Return type mismatch: base method returns type "Series[S1@Series]", override returns type "Series[_bool]"
+       "Series[_bool]" is incompatible with "Series[S1@Series]"
+         Type parameter "S1@Series" is invariant, but "_bool" is not the same as "S1@Series" (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1517:9 - error: Method "__gt__" overrides class "OpsMixin" in an incompatible manner
+     Return type mismatch: base method returns type "Series[S1@Series]", override returns type "Series[_bool]"
+       "Series[_bool]" is incompatible with "Series[S1@Series]"
+         Type parameter "S1@Series" is invariant, but "_bool" is not the same as "S1@Series" (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1520:9 - error: Method "__le__" overrides class "OpsMixin" in an incompatible manner
+     Return type mismatch: base method returns type "Series[S1@Series]", override returns type "Series[_bool]"
+       "Series[_bool]" is incompatible with "Series[S1@Series]"
+         Type parameter "S1@Series" is invariant, but "_bool" is not the same as "S1@Series" (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1523:9 - error: Method "__lt__" overrides class "OpsMixin" in an incompatible manner
+     Return type mismatch: base method returns type "Series[S1@Series]", override returns type "Series[_bool]"
+       "Series[_bool]" is incompatible with "Series[S1@Series]"
+         Type parameter "S1@Series" is invariant, but "_bool" is not the same as "S1@Series" (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1541:9 - error: Method "__or__" overrides class "OpsMixin" in an incompatible manner
+     No overload signature in override is compatible with base method (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1552:9 - error: Method "__rand__" overrides class "OpsMixin" in an incompatible manner
+     No overload signature in override is compatible with base method (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1571:9 - error: Method "__ror__" overrides class "OpsMixin" in an incompatible manner
+     No overload signature in override is compatible with base method (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1580:9 - error: Method "__rxor__" overrides class "OpsMixin" in an incompatible manner
+     No overload signature in override is compatible with base method (reportIncompatibleMethodOverride)
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/series.pyi:1604:9 - error: Method "__xor__" overrides class "OpsMixin" in an incompatible manner
+     No overload signature in override is compatible with base method (reportIncompatibleMethodOverride)
+ /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/indexes/datetimelike.pyi
+   /tmp/mypy_primer/projects/pandas-stubs/pandas-stubs/core/indexes/datetimelike.pyi:22:9 - error: Method "__rsub__" overrides class "OpsMixin" in an incompatible manner
+     Return type mismatch: base method returns type "DatetimeIndexOpsMixin[S1@DatetimeIndexOpsMixin]", override returns type "TimedeltaIndex"
+       "TimedeltaIndex" is incompatible with "DatetimeIndexOpsMixin[S1@DatetimeIndexOpsMixin]"
+         Type parameter "S1@DatetimeIndexOpsMixin" is invariant, but "Timedelta" is not the same as "S1@DatetimeIndexOpsMixin" (reportIncompatibleMethodOverride)
- 1506 errors, 0 warnings, 0 informations 
+ 1517 errors, 0 warnings, 0 informations 
erictraut commented 1 month ago

@Dr-Irv, looks like you removed some # pyright: ignore comments from pandas-stubs recently. These will unfortunately need to be added back once pyright 1.1.376 is released. A recent regression in pyright resulted in a false negative for these cases, which explains why these errors disappeared in 1.1.374. They are legit type errors, and this fix will bring them back. Just wanted to give you a heads up. Apologies for the churn.

Dr-Irv commented 1 month ago

@Dr-Irv, looks like you removed some # pyright: ignore comments from pandas-stubs recently. These will unfortunately need to be added back once pyright 1.1.376 is released. A recent regression in pyright resulted in a false negative for these cases, which explains why these errors disappeared in 1.1.374. They are legit type errors, and this fix will bring them back. Just wanted to give you a heads up. Apologies for the churn.

Thanks for the heads up. Can you ping here or somewhere else once 1.1.376 is released?