FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Workaround parenthesised context manager issue by hauntsaninja · Pull Request #16949 · python/mypy · GitHub

Repository navigation

Workaround parenthesised context manager issue - #16949

Merged
hauntsaninja merged 4 commits into
python:masterfrom
hauntsaninja:ctx-fix
Feb 25, 2024
Merged

hauntsaninja merged 4 commits into
python:masterfrom
hauntsaninja:ctx-fix

Conversation

Copy link
Copy Markdown
Collaborator

Fixes #16945

Comment thread mypy/checker.py Outdated

AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks!

hauntsaninja merged commit 2037e4a into python:master Feb 25, 2024
hauntsaninja deleted the ctx-fix branch February 25, 2024 22:44

Copy link
Copy Markdown
Collaborator Author

Thank you both!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mypy fails to typecheck itself following the recent black upgrade

3 participants


Back | FazBrowse Home | New Git URL