Skip to content

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

@gaetano-guerriero

Description

@gaetano-guerriero

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions