From 3443eba10601358bc6b2f200994af4592a77b585 Mon Sep 17 00:00:00 2001 From: zhengguangzhuo <100548634+zhengguangzhuo@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:37:30 +0800 Subject: [PATCH 1/2] Preserve import order in try blocks --- mypy/fastparse.py | 6 ++++-- test-data/unit/check-modules.test | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index a4aea55386abb..7c7e3418cc742 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1367,6 +1367,7 @@ def visit_Raise(self, n: ast3.Raise) -> RaiseStmt: # Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody) def visit_Try(self, n: ast3.Try) -> TryStmt: + body = self.as_required_block(n.body) vs = [ self.set_line(NameExpr(h.name), h) if h.name is not None else None for h in n.handlers ] @@ -1374,7 +1375,7 @@ def visit_Try(self, n: ast3.Try) -> TryStmt: handlers = [self.as_required_block(h.body) for h in n.handlers] node = TryStmt( - self.as_required_block(n.body), + body, vs, types, handlers, @@ -1384,6 +1385,7 @@ def visit_Try(self, n: ast3.Try) -> TryStmt: return self.set_line(node, n) def visit_TryStar(self, n: TryStar) -> TryStmt: + body = self.as_required_block(n.body) vs = [ self.set_line(NameExpr(h.name), h) if h.name is not None else None for h in n.handlers ] @@ -1391,7 +1393,7 @@ def visit_TryStar(self, n: TryStar) -> TryStmt: handlers = [self.as_required_block(h.body) for h in n.handlers] node = TryStmt( - self.as_required_block(n.body), + body, vs, types, handlers, diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 5fba65f276a43..98ab7a9354d56 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -3239,6 +3239,24 @@ import b # type: ignore[import-not-found] tmp/a.py:1: error: Unused "type: ignore" comment [out2] +[case testRepeatedMissingImportKeepsFirstUnsuppressedError] +try: + import someweirdpackage +except: + import someweirdpackage # type: ignore[import-not-found] +[out] +main:2: error: Cannot find implementation or library stub for module named "someweirdpackage" +main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports + +[case testRepeatedMissingImportKeepsFirstUnsuppressedError_old_parser] +try: + import someweirdpackage +except: + import someweirdpackage # type: ignore[import-not-found] +[out] +main:2: error: Cannot find implementation or library stub for module named "someweirdpackage" +main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports + [case testImportErrorStaleLoadedFromCacheIncremental] import a [file a.py] From c2bb607b8f1cf1a031ba09b9c384e52d42bdd165 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 22 Oct 2026 07:41:51 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/fastparse.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 7c7e3418cc742..db44b6749cd78 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1375,12 +1375,7 @@ def visit_Try(self, n: ast3.Try) -> TryStmt: handlers = [self.as_required_block(h.body) for h in n.handlers] node = TryStmt( - body, - vs, - types, - handlers, - self.as_block(n.orelse), - self.as_block(n.finalbody), + body, vs, types, handlers, self.as_block(n.orelse), self.as_block(n.finalbody) ) return self.set_line(node, n) @@ -1393,12 +1388,7 @@ def visit_TryStar(self, n: TryStar) -> TryStmt: handlers = [self.as_required_block(h.body) for h in n.handlers] node = TryStmt( - body, - vs, - types, - handlers, - self.as_block(n.orelse), - self.as_block(n.finalbody), + body, vs, types, handlers, self.as_block(n.orelse), self.as_block(n.finalbody) ) node.is_star = True return self.set_line(node, n)