From 842044e171b15caad33f41ffb88c5ec41df99947 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 14 Dec 2021 16:10:46 +0000 Subject: [PATCH] Don't crash on unsupported Python 3.10 match statement (#11738) Generate a syntax error instead. --- mypy/fastparse.py | 4 ++++ mypy/test/testcheck.py | 2 ++ test-data/unit/check-python310.test | 7 +++++++ 3 files changed, 13 insertions(+) create mode 100644 test-data/unit/check-python310.test diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 456e2e9a9a1b..5b0bbb1ada19 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1285,6 +1285,10 @@ def visit_Index(self, n: Index) -> Node: # cast for mypyc's benefit on Python 3.9 return self.visit(cast(Any, n).value) + def visit_Match(self, n: Any) -> None: + self.fail("Match statement is not supported", + line=n.lineno, column=n.col_offset, blocker=True) + class TypeConverter: def __init__(self, diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index c71b9aeea626..b90647fbd26d 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -103,6 +103,8 @@ typecheck_files.append('check-python38.test') if sys.version_info >= (3, 9): typecheck_files.append('check-python39.test') +if sys.version_info >= (3, 10): + typecheck_files.append('check-python310.test') # Special tests for platforms with case-insensitive filesystems. if sys.platform in ('darwin', 'win32'): diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test new file mode 100644 index 000000000000..3bcac61855b4 --- /dev/null +++ b/test-data/unit/check-python310.test @@ -0,0 +1,7 @@ +[case testMatchStatementNotSupported] +# flags: --python-version 3.10 +match str(): # E: Match statement is not supported + case 'x': + 1 + '' + case _: + 1 + b''