8000 Make PEP 695 constructs give a reasonable error message (#16013) · python/mypy@0c29507 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c29507

Browse files
authored
Make PEP 695 constructs give a reasonable error message (#16013)
Mypy does not yet support PEP 695 Fixes #16011, linking #15238
1 parent 803f610 commit 0c29507

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-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< 67E6 div class="diff-text-inner">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"):

test-data/unit/check-python312.test

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[case test695TypeAlias]
2+
type MyInt = int # E: PEP 695 type aliases are not yet supported
3+
4+
def f(x: MyInt) -> MyInt:
5+
return reveal_type(x) # N: Revealed type is "builtins.int"
6+
7+
type MyList[T] = list[T] # E: PEP 695 type aliases are not yet supported \
8+
# E: Name "T" is not defined
9+
10+
def g(x: MyList[int]) -> MyList[int]: # E: Variable "__main__.MyList" is not valid as a type \
11+
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
12+
return reveal_type(x) # N: Revealed type is "MyList?[builtins.int]"
13+
14+
[case test695Class]
15+
class MyGen[T]: # E: PEP 695 generics are not yet supported
16+
def __init__(self, x: T) -> None: # E: Name "T" is not defined
17+
self.x = x
18+
19+
def f(x: MyGen[int]): # E: "MyGen" expects no type arguments, but 1 given
20+
reveal_type(x.x) # N: Revealed type is "Any"
21+
22+
[case test695Function]
23+
def f[T](x: T) -> T: # E: PEP 695 generics are not yet supported \
24+
# E: Name "T" is not defined
25+
return reveal_type(x) # N: Revealed type is "Any"
26+
27+
reveal_type(f(1)) # N: Revealed type is "Any"
28+
29+
async def g[T](x: T) -> T: # E: PEP 695 generics are not yet supported \
30+
# E: Name "T" is not defined
31+
return reveal_type(x) # N: Revealed type is "Any"
32+
33+
reveal_type(g(1)) # E: Value of type "Coroutine[Any, Any, Any]" must be used \
34+
# N: Are you missing an await? \
35+
# N: Revealed type is "typing.Coroutine[Any, Any, Any]"
36+
37+
[case test695TypeVar]
38+
from typing import Callable
39+
type Alias1[T: int] = list[T] # E: PEP 695 type aliases are not yet supported
40+
type Alias2[**P] = Callable[P, int] # E: PEP 695 type aliases are not yet supported \
41+
# E: Value of type "int" is not indexable \
42+
# E: Name "P" is not defined
43+
type Alias3[*Ts] = tuple[*Ts] # E: PEP 695 type aliases are not yet supported \
44+
# E: Type expected within [...] \
45+
# E: The type "Type[Tuple[Any, ...]]" is not generic and not indexable \
46+
# E: Name "Ts" is not defined
47+
48+
class Cls1[T: int]: ... # E: PEP 695 generics are not yet supported
49+
class Cls2[**P]: ... # E: PEP 695 generics are not yet supported
50+
class Cls3[*Ts]: ... # E: PEP 695 generics are not yet supported
51+
52+
def func1[T: int](x: T) -> T: ... # E: PEP 695 generics are not yet supported
53+
def func2[**P](x: Callable[P, int]) -> Callable[P, str]: ... # E: PEP 695 generics are not yet supported \
54+
# E: The first argument to Callable must be a list of types, parameter specification, or "..." \
55+
# N: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas \
56+
# E: Name "P" is not defined
57+
def func3[*Ts](x: tuple[*Ts]) -> tuple[int, *Ts]: ... # E: PEP 695 generics are not yet supported \
58+
# E: Name "Ts" is not defined
59+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)
0