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

overloaded method that has NoReturn for certain arguments not detected #15861

Closed
Dr-Irv opened this issue Aug 13, 2023 · 3 comments
Closed

overloaded method that has NoReturn for certain arguments not detected #15861

Dr-Irv opened this issue Aug 13, 2023 · 3 comments
Labels
bug mypy got something wrong

Comments

@Dr-Irv
Copy link

Dr-Irv commented Aug 13, 2023

Bug Report

If you have a method that has an overload for specific arguments that are not allowed, by having that overload return NoReturn, and a wider default that accepts Any, mypy does not detect the invalid argument.

To Reproduce

from typing import Any, NoReturn, overload


class Goo:
    @overload
    def __add__(self, other: str) -> NoReturn:
        ...

    @overload
    def __add__(self, other: Any) -> bool:
        ...

    def __add__(self, other: Any) -> bool:
        return True


y = Goo()
y + 4
y + "abc"

Expected Behavior

I expect that the line y + "abc" is reported as an invalid operation.

Actual Behavior

Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.16
@Dr-Irv Dr-Irv added the bug mypy got something wrong label Aug 13, 2023
@JelleZijlstra
Copy link
Member

Mypy is correct here. NoReturn is not a way to make the type checker emit errors; it indicates that the code path never returns. The standard example is sys.exit(): it returns NoReturn, but type checkers should not emit an error for every use of NoReturn.

You are looking for python/typing#1043.

@erictraut
Copy link

I agree with Jelle's analysis. I just noticed that pyright contains a bug that causes it to (incorrectly) emit an error when a binary operator like __add__ returns NoReturn. It should not emit an error here, consistent with mypy.

@Dr-Irv
Copy link
Author

Dr-Irv commented Aug 13, 2023

I agree with Jelle's analysis. I just noticed that pyright contains a bug that causes it to (incorrectly) emit an error when a binary operator like __add__ returns NoReturn. It should not emit an error here, consistent with mypy.

Interesting. I liked the pyright behavior in this case! That’s why I reported the issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants