Skip to content

Commit a20fa4f

Browse files
committed
Make PEP 695 constructs give a reasonable error message
Mypy does not yet support PEP 695 Fixes #16011, linking #15238
1 parent 2a6d9cb commit a20fa4f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

mypy/fastparse.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ def ast3_parse(
144144
NamedExpr = ast3.NamedExpr
145145
Constant = ast3.Constant
146146

147+
if sys.version_info >= (3, 12):
148+
ast_TypeAlias = ast3.TypeAlias
149+
else:
150+
ast_TypeAlias = Any
151+
147152
if sys.version_info >= (3, 10):
148153
Match = ast3.Match
149154
MatchValue = ast3.MatchValue
@@ -936,6 +941,14 @@ def do_func_def(
936941
arg_types = [AnyType(TypeOfAny.from_error)] * len(args)
937942
return_type = AnyType(TypeOfAny.from_error)
938943
else:
944+
if sys.version_info >= (3, 12) and n.type_params:
945+
self.fail(
946+
ErrorMessage("PEP 695 generics are not yet supported", code=codes.VALID_TYPE),
947+
n.type_params[0].lineno,
948+
n.type_params[0].col_offset,
949+
blocker=False,
950+
)
951+
939952
arg_types = [a.type_annotation for a in args]
940953
return_type = TypeConverter(
941954
self.errors, line=n.returns.lineno if n.returns else lineno
@@ -1110,6 +1123,14 @@ def visit_ClassDef(self, n: ast3.ClassDef) -> ClassDef:
11101123
self.class_and_function_stack.append("C")
11111124
keywords = [(kw.arg, self.visit(kw.value)) for kw in n.keywords if kw.arg]
11121125

1126+
if sys.version_info >= (3, 12) and n.type_params:
1127+
self.fail(
1128+
ErrorMessage("PEP 695 generics are not yet supported", code=codes.VALID_TYPE),
1129+
n.type_params[0].lineno,
1130+
n.type_params[0].col_offset,
1131+
blocker=False,
1132+
)
1133+
11131134
cdef = ClassDef(
11141135
n.name,
11151136
self.as_required_block(n.body),
@@ -1717,6 +1738,16 @@ def visit_MatchOr(self, n: MatchOr) -> OrPattern:
17171738
node = OrPattern([self.visit(pattern) for pattern in n.patterns])
17181739
return self.set_line(node, n)
17191740

1741+
def visit_TypeAlias(self, n: ast_TypeAlias) -> AssignmentStmt:
1742+
self.fail(
1743+
ErrorMessage("PEP 695 type aliases are not yet supported", code=codes.VALID_TYPE),
1744+
n.lineno,
1745+
n.col_offset,
1746+
blocker=False,
1747+
)
1748+
node = AssignmentStmt([NameExpr(n.name.id)], self.visit(n.value))
1749+
return self.set_line(node, n)
1750+
17201751

17211752
class TypeConverter:
17221753
def __init__(

mypy/test/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ def num_skipped_suffix_lines(a1: list[str], a2: list[str]) -> int:
241241

242242

243243
def testfile_pyversion(path: str) -> tuple[int, int]:
244-
if path.endswith("python311.test"):
244+
if path.endswith("python312.test"):
245+
return 3, 12
246+
elif path.endswith("python311.test"):
245247
return 3, 11
246248
elif path.endswith("python310.test"):
247249
return 3, 10

mypy/test/testcheck.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
typecheck_files.remove("check-python310.test")
4444
if sys.version_info < (3, 11):
4545
typecheck_files.remove("check-python311.test")
46+
if sys.version_info < (3, 12):
47+
typecheck_files.remove("check-python312.test")
4648

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

0 commit comments

Comments
 (0)