-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Workaround parenthesised context manager issue #16949
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we could do this? It feels slightly more elegant than the current implementation as well as fixing the issue, as an added bonus :-)
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -4,7 +4,7 @@ from __future__ import annotations
import itertools
from collections import defaultdict
-from contextlib import contextmanager, nullcontext
+from contextlib import ExitStack, contextmanager
from typing import (
AbstractSet,
Callable,
@@ -526,17 +526,12 @@ class TypeChecker(NodeVisitor[None], CheckerPluginInterface):
# print("XXX in pass %d, class %s, function %s" %
# (self.pass_num, type_name, node.fullname or node.name))
done.add(node)
- with (
- self.tscope.class_scope(active_typeinfo)
- if active_typeinfo
- else nullcontext()
- ):
- with (
- self.scope.push_class(active_typeinfo)
- if active_typeinfo
- else nullcontext()
- ):
- self.check_partial(node)
+ with ExitStack() as stack:
+ if active_typeinfo:
+ stack.enter_context(self.tscope.class_scope(active_typeinfo))
+ stack.enter_context(self.scope.push_class(active_typeinfo))
+ self.check_partial(node)
+
return True
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Thank you both! |
Fixes #16945