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

false positive when type can be inferred by exception raised in called function #8013

Closed
gaetano-guerriero opened this issue Nov 26, 2019 · 1 comment

Comments

@gaetano-guerriero
Copy link

I'm not sure this is supposed to work:

from typing import Optional


class A:
    def __init__(self):
        self.n: Optional[int] = 1

    def f(self) -> None:
        if self.n is None:
            raise RuntimeError("")
        self.n += 1         # no mypy error here

    def f_false_positive(self) -> None:
        self.check_n()
        self.n += 1         # mypy error here

    def destroy(self) -> None:
        self.n = None

    def check_n(self):
        if self.n is None:
            raise RuntimeError("")

The error is at line 16:

$ mypy /tmp/1.py
/tmp/1.py:16: error: Unsupported operand types for + ("None" and "int")
/tmp/1.py:16: note: Left operand is of type "Optional[int]"

Line 11 is correct instead.

@ilevkivskyi
Copy link
Member

This is essentially a duplicate of #4063

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

No branches or pull requests

2 participants