-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
[Typing] batched
revealed type?
#13470
Comments
Hi @ego-thales , It seems like the issue is arising because the reduce function expects a binary function as its first argument, but batched from itertools is not a binary function—it's a function that returns an iterator, which is why mypy is complaining about the type mismatch. To fix this, you should use batched directly with the iterable and not inside reduce. reduce is typically used with a binary function that takes two arguments (e.g., a summing function), but batched groups elements of the iterable into fixed-sized chunks, so it's not meant to be used in this way. |
Hi, this is clearly not an issue in CPython, because it does not even have annotations :) So, I transfered issue here. It might also be a mypy specific issue, or even not an issue at all. I didn't look deep enough. |
Pyright doesn't show an error here, but it produces this rather interesting type:
Similarly, pyright thinks
Which is incorrect since it's |
It feels like there are two reasons this happens. First: Lines 38 to 39 in 5caaf2e
This would not be applicable to functions such as, say, def function_append[_S](iter: Iterable[_S], append: _S) -> list[_S]:
return list(iter) + [append] There might be a good reason for such restriction, but I'm not type-fluent enough to say. Also, I don't really know how to fix it, we would somehow need to extend to def reduce(function: Callable[[_U, _S], _T], sequence: Iterable[_S], initial: _U, /) -> _T: ... with the added condition that Second: Lines 329 to 336 in 5caaf2e
Which obsiouvly doesn't sit well with the Callable expected by reduce . I don't know how to fix this.
|
@ego-thales all classes are callables. So, it is not really a problem. |
Ok, thanks for confirming that So in the end, I think the focus should turn to the restrictive stub for _S = TypeVar("_S")
_T = TypeVar("_T")
_U = TypeVar("_U", bound=_T)
@overload
def reduce(function: Callable[[_T, _S], _U], sequence: Iterable[_S], initial: _T, /) -> _U: ... |
If the runtime class has
TypeVars can't have bounds that depend on other TypeVars. Possibly a TypeVar default (PEP 696) would work but I haven't fully thought it through. |
Bug report
Bug description:
Hi!
Consider the following.
It makes
mypy
unhappy!error: Argument 1 to "reduce" has incompatible type "type[batched[Any]]"; expected "Callable[[tuple[int], int], tuple[int]]" [arg-type]
I did try to previously type hint the tuple with
But the error remains the same.
It may be that I'm not understanding something elementary in which case, thanks for the help! Otherwise, is this a problem of
batched
or something deeper?Thanks!
CPython versions tested on:
3.13
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: