mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-23 04:11:28 +08:00
builtins.sum: Items in the iterable must support addition with int if no start value is given (#8000)
This commit is contained in:
52
test_cases/stdlib/builtins/test_sum.py
Normal file
52
test_cases/stdlib/builtins/test_sum.py
Normal file
@@ -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')])
|
||||
Reference in New Issue
Block a user