-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
builtins.sum: Items in the iterable must support addition with int if no start value is given
#8000
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ee8448
`builtins.sum`: Iterable must support addition with `int` if no `star…
AlexWaygood a15b2ad
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 30d5339
Improve names
AlexWaygood 4d4ce35
Simplify: Just make `SupportsAdd` generic
AlexWaygood 90e6fa7
Address review
AlexWaygood b00b2d6
Fix
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # pyright: reportUnnecessaryTypeIgnoreComment=true | ||
|
|
||
| from typing import Any, List, Union | ||
| from typing_extensions import Literal, assert_type | ||
|
|
||
|
|
||
| class Foo: | ||
| def __add__(self, other: Any) -> "Foo": | ||
| return Foo() | ||
|
|
||
|
|
||
| class Bar: | ||
| def __radd__(self, other: Any) -> "Bar": | ||
| return Bar() | ||
|
|
||
|
|
||
| class Baz: | ||
| def __add__(self, other: Any) -> "Baz": | ||
| return Baz() | ||
|
|
||
| def __radd__(self, other: Any) -> "Baz": | ||
| return Baz() | ||
|
|
||
|
|
||
| assert_type(sum([2, 4]), int) | ||
| assert_type(sum([3, 5], 4), int) | ||
|
|
||
| assert_type(sum([True, False]), int) | ||
| assert_type(sum([True, False], True), int) | ||
|
|
||
| assert_type(sum([["foo"], ["bar"]], ["baz"]), List[str]) | ||
|
|
||
| assert_type(sum([Foo(), Foo()], Foo()), Foo) | ||
| assert_type(sum([Baz(), Baz()]), Union[Baz, Literal[0]]) | ||
|
|
||
| # mypy and pyright infer the types differently for these, so we can't use assert_type | ||
| # Just test that no error is emitted for any of these | ||
| sum([("foo",), ("bar", "baz")], ()) # mypy: `tuple[str, ...]`; pyright: `tuple[()] | tuple[str] | tuple[str, str]` | ||
| sum([5.6, 3.2]) # mypy: `float`; pyright: `float | Literal[0]` | ||
| sum([2.5, 5.8], 5) # mypy: `float`; pyright: `float | int` | ||
|
|
||
| # These all fail at runtime | ||
| sum("abcde") # type: ignore[arg-type] | ||
| sum([["foo"], ["bar"]]) # type: ignore[list-item] | ||
| sum([("foo",), ("bar", "baz")]) # type: ignore[list-item] | ||
| sum([Foo(), Foo()]) # type: ignore[list-item] | ||
| sum([Bar(), Bar()], Bar()) # type: ignore[call-overload] | ||
| sum([Bar(), Bar()]) # type: ignore[list-item] | ||
|
|
||
| # TODO: these pass pyright with the current stubs, but mypy erroneously emits an error: | ||
| # sum([3, Fraction(7, 22), complex(8, 0), 9.83]) | ||
| # sum([3, Decimal('0.98')]) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
mypy also emits an error for these tests on current master, so this PR wouldn't be introducing a regression here.