-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Better support for variadic calls and indexing #16131
Conversation
This comment has been minimized.
This comment has been minimized.
It looks like the new errors are real. Because mypy previously handled |
Works good overall 👍 . I think there are still some issues to be uncovered, I haven't tested everything yet. But I can provide the following issues already. @ilevkivskyi Not sure all of the issues I'll report are within the scope of this MR. But I think It's good to mention them since you say "It was necessary to fix overload ambiguity logic to make some tests pass. This goes beyond TypeVarTuple support". I let you filter out issues that you think are irrelevant here :)
from __future__ import annotations
from typing import Any, TypeVar, overload
_T = TypeVar("_T")
class A:
@overload
def __call__(self, *c: Any, return_value: _T, **kwargs: Any) -> _T:
...
@overload
def __call__( # test.py:14: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [misc]
self, *c: Any, **kwargs: Any
) -> Any:
...
def __call__(self, *c: Any, **kwargs: Any) -> Any:
...
from __future__ import annotations
from typing import TypeVarTuple, Unpack
_Ts = TypeVarTuple("_Ts")
def f(tuple_data: tuple[Unpack[_Ts]]) -> None:
# test.py:13: error: Tuple index out of range [misc]
# test.py:13: note: Variadic tuple can have length 0
tuple_data[0] I don't think it should since it's equivalent to this, where no error is reported from __future__ import annotations
from typing import Any, TypeVar
_T = TypeVar("_T", bound=tuple[Any, ...])
def f(tuple_data: _T) -> None:
tuple_data[0] |
mypy infers tuple[Any, ...] here, I'd expect it to infer tuple[Any] from __future__ import annotations
from typing import Any, TypeVar, overload
_T0 = TypeVar("_T0")
@overload
def returning(__ent0: type[_T0], /) -> tuple[_T0]:
...
@overload
def returning(*cols: type[Any] | None) -> tuple[Any, ...]:
...
def returning(*cols: type[Any] | None) -> tuple[Any, ...]:
...
def f(col: type[Any]):
reveal_type(returning(col)) |
@mehdigmira OK, thanks! So about these three:
@JukkaL @jhance As you can see from responses above I am not going to alter this PR now, no need to wait with your reviews. @jhance I just accidentally noticed #15254. It looks like test cases from there already work (even the one that is commented out). I will add them later, if you know any other missing Callable-related test cases, please do let me know. |
Regarding 2. Sorry, but I don't fully get the point: tuple[T, ...] means "tuple with arbitrary number of items of type T". And "arbitrary" should allow user to handle the tuple in the way they want to, to avoid false positives. And this actually seems to be what mypy does: no error is logged here from __future__ import annotations
from typing import Any
def f(tuple_data: tuple[Any, ...]) -> None:
tuple_data[0] So I don't get why mypy would log errors when using a typevartuple. -- EDIT: tuple[Unpack[tuple[Any, ...]]] and tuple[Any, ...] are the same, but mypy handles them differently from __future__ import annotations
from typing import Any, TypeVarTuple, Unpack
_Ts = TypeVarTuple("_Ts")
def f(tuple_data1: tuple[Unpack[tuple[Any, ...]]], tuple_data2: tuple[Any, ...]) -> None:
tuple_data1[0] # error
tuple_data2[0] # no error |
This is a totally different question. And, unfortunately, we should allow this (for consistency and compatibility), and not just for |
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/apply.py:1604: error: Incompatible return value type (got "tuple[defaultdict[Any, Any], tuple[Any, ...], ndarray[Any, dtype[signedinteger[Any]]]]", expected "tuple[dict[Any, Any], list[str], ndarray[Any, dtype[signedinteger[Any]]]]") [return-value]
+ pandas/io/formats/format.py:944: error: List comprehension has incompatible type List[list[Any]]; expected List[tuple[Any, ...]] [misc]
+ pandas/io/formats/format.py:950: error: List comprehension has incompatible type List[list[str]]; expected List[tuple[Any, ...]] [misc]
+ pandas/io/formats/format.py:954: error: Incompatible return value type (got "list[tuple[Any, ...]]", expected "list[list[str]]") [return-value]
+ pandas/core/generic.py:7825: error: Incompatible types in assignment (expression has type "list[Never]", variable has type "tuple[Any, ...]") [assignment]
+ pandas/core/generic.py:7840: error: Incompatible types in assignment (expression has type "list[Never]", variable has type "tuple[Any, ...]") [assignment]
+ pandas/core/reshape/melt.py:211: error: Incompatible types in assignment (expression has type "tuple[Any, ...]", variable has type "list[Any]") [assignment]
+ pandas/core/reshape/concat.py:866: error: Incompatible types in assignment (expression has type "Index", variable has type "tuple[Any, ...]") [assignment]
+ pandas/core/indexes/multi.py:3938: error: Argument 1 to "append" of "list" has incompatible type "list[Any]"; expected "tuple[Any, ...]" [arg-type]
+ pandas/core/indexes/multi.py:3945: error: Argument 1 to "append" of "list" has incompatible type "list[Any]"; expected "tuple[Any, ...]" [arg-type]
optuna (https://github.com/optuna/optuna)
+ optuna/importance/_fanova/_tree.py:67: error: Incompatible types in assignment (expression has type "tuple[Any, ...]", variable has type "list[Any]") [assignment]
vision (https://github.com/pytorch/vision)
+ torchvision/datasets/video_utils.py:139: error: Need type annotation for "video_fps" (hint: "video_fps: List[<type>] = ...") [var-annotated]
spack (https://github.com/spack/spack)
+ lib/spack/spack/parser.py:204: error: Unused "type: ignore" comment [unused-ignore]
+ lib/spack/spack/parser.py:204: error: Item "None" of "Optional[Match[Any]]" has no attribute "lastgroup" [union-attr]
+ lib/spack/spack/parser.py:204: note: Error code "union-attr" not covered by "type: ignore" comment
+ lib/spack/spack/parser.py:204: error: Invalid index type "Union[str, None, Any]" for "MappingProxyType[str, TokenType]"; expected type "str" [index]
+ lib/spack/spack/parser.py:204: note: Error code "index" not covered by "type: ignore" comment
+ lib/spack/spack/parser.py:205: error: Unused "type: ignore" comment [unused-ignore]
+ lib/spack/spack/parser.py:205: error: Item "None" of "Optional[Match[Any]]" has no attribute "group" [union-attr]
+ lib/spack/spack/parser.py:205: note: Error code "union-attr" not covered by "type: ignore" comment
+ lib/spack/spack/parser.py:206: error: Unused "type: ignore" comment [unused-ignore]
+ lib/spack/spack/parser.py:206: error: Item "None" of "Optional[Match[Any]]" has no attribute "start" [union-attr]
+ lib/spack/spack/parser.py:206: note: Error code "union-attr" not covered by "type: ignore" comment
+ lib/spack/spack/parser.py:207: error: Unused "type: ignore" comment [unused-ignore]
+ lib/spack/spack/parser.py:207: error: Item "None" of "Optional[Match[Any]]" has no attribute "end" [union-attr]
+ lib/spack/spack/parser.py:207: note: Error code "union-attr" not covered by "type: ignore" comment
+ lib/spack/spack/parser.py:216: error: Unused "type: ignore" comment [unused-ignore]
sockeye (https://github.com/awslabs/sockeye)
+ sockeye/data_io.py:695: error: Argument 1 to "combine_means" has incompatible type "tuple[Any, ...]"; expected "list[float | None]" [arg-type]
+ sockeye/data_io.py:704: error: Argument 2 to "combine_means" has incompatible type "tuple[Any, ...]"; expected "list[int]" [arg-type]
+ sockeye/data_io.py:705: error: Argument 3 to "combine_stds" has incompatible type "tuple[Any, ...]"; expected "list[int]" [arg-type]
- sockeye/training.py:86: error: Unused "type: ignore" comment [unused-ignore]
+ sockeye/translate.py:190: error: Incompatible types in assignment (expression has type "tuple[Any, ...]", variable has type "list[str]") [assignment]
|
This improves support for two features that were supported but only partially: variadic calls, and variadic indexing. Some notes:
@mehdigmira could you also please test this PR on your code base (and play with it in general)?