Skip to content

Add NoReturn overload to builtins.sum #7606

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

Closed
wants to merge 2 commits into from

Conversation

AlexWaygood
Copy link
Member

sum fails if you try to pass an iterable of strings to it:

>>> sum(('a', 'b', 'c'), start='d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]

As pointed out by @Akuli, there's no ideal way to describe this in the stubs without python/typing#1043. However, a NoReturn overload is better than nothing.

Helps with #7574

@JelleZijlstra
Copy link
Member

Not sure this is an improvement. NoReturn makes mypy silently discard code as unreachable, and pyright doesn't give an error if you use it either.

Consider this code:

from typing import NoReturn

def f() -> NoReturn:
    raise Exception

def capybara() -> None:
    f()
    1 + "1"

Mypy type checks it without errors. Pyright detects the 1 + "1", but says nothing about the f() call.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 7, 2022

Diff from mypy_primer, showing the effect of this PR on open source code:

sockeye (https://github.com/awslabs/sockeye)
+ sockeye/data_io.py:673: error: Argument "num_sents_per_bucket" to "DataStatistics" has incompatible type "List[NoReturn]"; expected "List[int]"
+ sockeye/data_io.py:673: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ sockeye/data_io.py:673: note: Consider using "Sequence" instead, which is covariant

xarray (https://github.com/pydata/xarray)
+ xarray/core/formatting.py:195: error: Value of type "NoReturn" is not indexable  [index]

manticore (https://github.com/trailofbits/manticore)
+ manticore/core/smtlib/solver.py:297: error: Need type annotation for "lparen"
+ manticore/core/smtlib/solver.py:297: error: Need type annotation for "rparen"
+ manticore/core/smtlib/solver.py:298: error: Unsupported left operand type for == ("NoReturn")

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Apr 7, 2022

NoReturn makes mypy silently discard code as unreachable

I hoped that the implication for reachability was the advantage of this proposal -- that at least mypy would start raising errors for "unreachable code" if people had the --warn-unreachable setting switched on.

But I had forgotten that mypy simply starts ignoring type errors if it considers the code that contains the type error to be unreachable.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Apr 7, 2022

Too many mypy_primer false positives, and no clear indication that this would meaningfully improve things.

@AlexWaygood AlexWaygood closed this Apr 7, 2022
@AlexWaygood
Copy link
Member Author

NoReturn makes mypy silently discard code as unreachable

Maybe we should reconsider NoReturn overloads like this one:

if sys.version_info >= (3, 8):
def __add__(self: Self, __other: timedelta) -> Self: ...
def __radd__(self: Self, __other: timedelta) -> Self: ...
@overload
def __sub__(self: Self, __other: timedelta) -> Self: ...
@overload
def __sub__(self, __other: datetime) -> NoReturn: ...
@overload
def __sub__(self: _D, __other: _D) -> timedelta: ...

@AlexWaygood AlexWaygood deleted the sum branch April 7, 2022 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants