Skip to content

Commit 42a4c69

Browse files
committed
Make TryStar not crash (#13991)
1 parent 5fad1ac commit 42a4c69

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

Diff for: mypy/fastparse.py

+22
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ def ast3_parse(
212212
MatchAs = Any
213213
MatchOr = Any
214214
AstNode = Union[ast3.expr, ast3.stmt, ast3.ExceptHandler]
215+
if sys.version_info >= (3, 11):
216+
TryStar = ast3.TryStar
217+
else:
218+
TryStar = Any
215219
except ImportError:
216220
try:
217221
from typed_ast import ast35 # type: ignore[attr-defined] # noqa: F401
@@ -1249,6 +1253,24 @@ def visit_Try(self, n: ast3.Try) -> TryStmt:
12491253
)
12501254
return self.set_line(node, n)
12511255

1256+
def visit_TryStar(self, n: TryStar) -> TryStmt:
1257+
# TODO: we treat TryStar exactly like Try, which makes mypy not crash. See #12840
1258+
vs = [
1259+
self.set_line(NameExpr(h.name), h) if h.name is not None else None for h in n.handlers
1260+
]
1261+
types = [self.visit(h.type) for h in n.handlers]
1262+
handlers = [self.as_required_block(h.body, h.lineno) for h in n.handlers]
1263+
1264+
node = TryStmt(
1265+
self.as_required_block(n.body, n.lineno),
1266+
vs,
1267+
types,
1268+
handlers,
1269+
self.as_block(n.orelse, n.lineno),
1270+
self.as_block(n.finalbody, n.lineno),
1271+
)
1272+
return self.set_line(node, n)
1273+
12521274
# Assert(expr test, expr? msg)
12531275
def visit_Assert(self, n: ast3.Assert) -> AssertStmt:
12541276
node = AssertStmt(self.visit(n.test), self.visit(n.msg))

Diff for: mypy/test/helpers.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,14 @@ def num_skipped_suffix_lines(a1: list[str], a2: list[str]) -> int:
282282

283283

284284
def testfile_pyversion(path: str) -> tuple[int, int]:
285-
if path.endswith("python310.test"):
285+
if path.endswith("python311.test"):
286+
return 3, 11
287+
elif path.endswith("python310.test"):
286288
return 3, 10
289+
elif path.endswith("python39.test"):
290+
return 3, 9
291+
elif path.endswith("python38.test"):
292+
return 3, 8
287293
else:
288294
return defaults.PYTHON3_VERSION
289295

Diff for: mypy/test/testcheck.py

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
typecheck_files.remove("check-python39.test")
4545
if sys.version_info < (3, 10):
4646
typecheck_files.remove("check-python310.test")
47+
if sys.version_info < (3, 11):
48+
typecheck_files.remove("check-python311.test")
4749

4850
# Special tests for platforms with case-insensitive filesystems.
4951
if sys.platform not in ("darwin", "win32"):

Diff for: test-data/unit/check-python311.test

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[case testTryStarDoesNotCrash]
2+
try:
3+
pass
4+
except* Exception as e:
5+
reveal_type(e) # N: Revealed type is "builtins.Exception"
6+
[builtins fixtures/exception.pyi]

0 commit comments

Comments
 (0)