mypy
mypy copied to clipboard
False negative: Context manager which suppresses errors doesn't trigger `[possibly-undefined]`
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