mypy icon indicating copy to clipboard operation
mypy copied to clipboard

False negative: Context manager which suppresses errors doesn't trigger `[possibly-undefined]`

Open bzoracler opened this issue 1 year ago • 0 comments

Bug Report, To Reproduce, & Actual Behaviour

The following snippet will fail at runtime, which mypy should catch with [possibly-undefined] (see mypy Playground):

# mypy: enable-error-code=possibly-undefined

from typing import Literal

class ExceptionSuppressor:
    def __enter__(self) -> None:
        return

    def __exit__(self, *args: object, **kwargs: object) -> Literal[True]:
        return True

def get_num() -> int:
    raise RuntimeError

with ExceptionSuppressor():
    var = get_num()

print(var)  # Runtime failure

Expected Behavior

print(var)  # mypy: Name "var" may be undefined [possibly-undefined]

Your Environment

  • Mypy version used: 1.10.1, 1.11.1
  • Mypy command-line flags: enable-error-code=possibly-undefined
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10, 3.12

bzoracler avatar Aug 13 '24 06:08 bzoracler