From 9b7b3830723ade186701e88c3a5d1e9506ee737b Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 18 Apr 2016 14:27:20 -0700 Subject: [PATCH] Make ConditionalTypeBinder a context manager. Now we can use `with binder: X` instead of `binder.push_frame(); X; binder.pop_frame()` --- mypy/checker.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mypy/checker.py b/mypy/checker.py index 8f46c3890a67..c3d1ad60c18f 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1,6 +1,7 @@ """Mypy type checker.""" import itertools +import contextlib from typing import ( Any, Dict, Set, List, cast, Tuple, TypeVar, Union, Optional, NamedTuple @@ -295,6 +296,12 @@ def push_loop_frame(self): def pop_loop_frame(self): self.loop_frames.pop() + def __enter__(self) -> None: + self.push_frame() + + def __exit__(self, *args: Any) -> None: + self.pop_frame() + def meet_frames(*frames: Frame) -> Frame: answer = Frame()