-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
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 |
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")
|
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 But I had forgotten that mypy simply starts ignoring type errors if it considers the code that contains the type error to be unreachable. |
Too many mypy_primer false positives, and no clear indication that this would meaningfully improve things. |
Maybe we should reconsider Lines 73 to 81 in 17b2f31
|
sum
fails if you try to pass an iterable of strings to it: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