Skip to content
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

Make TryStar not crash #13991

Merged
merged 3 commits into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions mypy/fastparse.py
Expand Up @@ -212,6 +212,10 @@ def ast3_parse(
MatchAs = Any
MatchOr = Any
AstNode = Union[ast3.expr, ast3.stmt, ast3.ExceptHandler]
if sys.version_info >= (3, 11):
TryStar = ast3.TryStar
else:
TryStar = Any
except ImportError:
try:
from typed_ast import ast35 # type: ignore[attr-defined] # noqa: F401
Expand Down Expand Up @@ -1249,6 +1253,24 @@ def visit_Try(self, n: ast3.Try) -> TryStmt:
)
return self.set_line(node, n)

def visit_TryStar(self, n: TryStar) -> TryStmt:
# TODO: we treat TryStar exactly like Try, which makes mypy not crash. See #12840
vs = [
self.set_line(NameExpr(h.name), h) if h.name is not None else None for h in n.handlers
]
types = [self.visit(h.type) for h in n.handlers]
handlers = [self.as_required_block(h.body, h.lineno) for h in n.handlers]

node = TryStmt(
self.as_required_block(n.body, n.lineno),
vs,
types,
handlers,
self.as_block(n.orelse, n.lineno),
self.as_block(n.finalbody, n.lineno),
)
return self.set_line(node, n)

# Assert(expr test, expr? msg)
def visit_Assert(self, n: ast3.Assert) -> AssertStmt:
node = AssertStmt(self.visit(n.test), self.visit(n.msg))
Expand Down
8 changes: 7 additions & 1 deletion mypy/test/helpers.py
Expand Up @@ -282,8 +282,14 @@ def num_skipped_suffix_lines(a1: list[str], a2: list[str]) -> int:


def testfile_pyversion(path: str) -> tuple[int, int]:
if path.endswith("python310.test"):
if path.endswith("python311.test"):
return 3, 11
elif path.endswith("python310.test"):
return 3, 10
elif path.endswith("python39.test"):
return 3, 9
elif path.endswith("python38.test"):
return 3, 8
else:
return defaults.PYTHON3_VERSION

Expand Down
2 changes: 2 additions & 0 deletions mypy/test/testcheck.py
Expand Up @@ -44,6 +44,8 @@
typecheck_files.remove("check-python39.test")
if sys.version_info < (3, 10):
typecheck_files.remove("check-python310.test")
if sys.version_info < (3, 11):
typecheck_files.remove("check-python311.test")

# Special tests for platforms with case-insensitive filesystems.
if sys.platform not in ("darwin", "win32"):
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-python311.test
@@ -0,0 +1,6 @@
[case testTryStarDoesNotCrash]
try:
pass
except* Exception as e:
reveal_type(e) # N: Revealed type is "builtins.Exception"
[builtins fixtures/exception.pyi]