Skip to content
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

builtins.sum: Spurious error for operands having __add__ defined using partialmethod #7739

Open
kaushikcfd opened this issue Apr 29, 2022 · 2 comments
Labels
stubs: false positive Type checkers report false errors

Comments

@kaushikcfd
Copy link

Here's a minimal reproducer:

from __future__ import annotations
from functools import partialmethod

class A:
    def my_add(self, other: A, reverse: bool) -> A:
        return self

    __add__ = partialmethod(my_add, reverse=False)

a = A()
aa = A()
aaa = A()
result = sum([aa, a], start=aaa)

And the commands to reproduce it:

$ mypy --version
mypy 0.950 (compiled: yes)

$ mypy --show-error-codes --strict  mypy_bug_report.py 
mypy_bug_report.py:15: error: No overload variant of "sum" matches argument types "List[A]", "A"  [call-overload]
mypy_bug_report.py:15: note: Possible overload variants:
mypy_bug_report.py:15: note:     def [_SumT <: _SupportsSum] sum(Iterable[_SumT]) -> Union[_SumT, Literal[0]]
mypy_bug_report.py:15: note:     def [_SumT <: _SupportsSum, _SumS <: _SupportsSum] sum(Iterable[_SumT], start: _SumS) -> Union[_SumT, _SumS]
Found 1 error in 1 file (checked 1 source file)

Related #7735, #7578

@kaushikcfd kaushikcfd changed the title builtins.sum: Spurious error for operand error having __add__ defined using partialmethod Apr 29, 2022
@inducer
Copy link

inducer commented Apr 29, 2022

It appears that this is a regression. mypy 0.942 does not object to the code in the OP.

@AlexWaygood AlexWaygood added the stubs: false positive Type checkers report false errors label Jun 12, 2022
@AlexWaygood
Copy link
Member

This still (unsurprisingly) reproduces after the recent changes to the sum stub. Here's the current error message:

(.venv) C:\Users\alexw\coding\typeshed>mypy test.py --strict --show-error-codes --custom-typeshed-dir .
test.py:13: error: No overload variant of "sum" matches argument types "List[A]", "A"  [call-overload]
test.py:13: note: Possible overload variants:
test.py:13: note:     def sum(__iterable, Iterable[bool], start: int = ...) -> int
test.py:13: note:     def [_SupportsSumNoDefaultT <: _SupportsSumWithNoDefaultGiven] sum(__iterable, Iterable[_SupportsSumNoDefaultT]) -> Union[_SupportsSumNoDefaultT, Literal[0]]
test.py:13: note:     def [_AddableT1 <: SupportsAdd[Any, Any], _AddableT2 <: SupportsAdd[Any, Any]] sum(__iterable, Iterable[_AddableT1], start: _AddableT2) -> Union[_AddableT1, _AddableT2]
Found 1 error in 1 file (checked 1 source file)

The reason why there's now an error where previously there was none is almost certainly due to changes made in typeshed between 0.942 and 0.950 that made the stub forsum` more complicated. Nonetheless, I feel like this is sort-of a bug in mypy's understanding of higher-order callables rather than a bug in the stub. In an ideal world, mypy should be able to understand that the arguments being passed in here match the third overload here:

typeshed/stdlib/builtins.pyi

Lines 1664 to 1665 in 1828ba2

@overload
def sum(__iterable: Iterable[_AddableT1], start: _AddableT2) -> _AddableT1 | _AddableT2: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
3 participants