Skip to content

Commit bc81191

Browse files
authored
[PEP 696] Report error if using unsupported type parameter defaults (#17876)
We don't yet support default types for type parameters when using the new type parameter syntax. Generate an error instead of just ignoring them. Also make it possible to write Python 3.13 specific tests.
1 parent 012d52c commit bc81191

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

mypy/fastparse.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,13 @@ def translate_type_params(self, type_params: list[Any]) -> list[TypeParam]:
11981198
for p in type_params:
11991199
bound = None
12001200
values: list[Type] = []
1201+
if sys.version_info >= (3, 13) and p.default_value is not None:
1202+
self.fail(
1203+
message_registry.TYPE_PARAM_DEFAULT_NOT_SUPPORTED,
1204+
p.lineno,
1205+
p.col_offset,
1206+
blocker=False,
1207+
)
12011208
if isinstance(p, ast_ParamSpec): # type: ignore[misc]
12021209
explicit_type_params.append(TypeParam(p.name, PARAM_SPEC_KIND, None, []))
12031210
elif isinstance(p, ast_TypeVarTuple): # type: ignore[misc]

mypy/message_registry.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,8 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
362362
TYPE_ALIAS_WITH_AWAIT_EXPRESSION: Final = ErrorMessage(
363363
"Await expression cannot be used within a type alias", codes.SYNTAX
364364
)
365+
366+
TYPE_PARAM_DEFAULT_NOT_SUPPORTED: Final = ErrorMessage(
367+
"Type parameter default types not supported when using Python 3.12 type parameter syntax",
368+
codes.MISC,
369+
)

mypy/test/helpers.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,9 @@ def local_sys_path_set() -> Iterator[None]:
256256

257257

258258
def testfile_pyversion(path: str) -> tuple[int, int]:
259-
if path.endswith("python312.test"):
260-
return 3, 12
261-
elif path.endswith("python311.test"):
262-
return 3, 11
263-
elif path.endswith("python310.test"):
264-
return 3, 10
265-
elif path.endswith("python39.test"):
266-
return 3, 9
267-
elif path.endswith("python38.test"):
268-
return 3, 8
259+
m = re.search(r"python3([0-9]+)\.test$", path)
260+
if m:
261+
return 3, int(m.group(1))
269262
else:
270263
return defaults.PYTHON3_VERSION
271264

mypy/test/testcheck.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
typecheck_files.remove("check-python311.test")
4646
if sys.version_info < (3, 12):
4747
typecheck_files.remove("check-python312.test")
48+
if sys.version_info < (3, 13):
49+
typecheck_files.remove("check-python313.test")
4850

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

test-data/unit/check-python313.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[case testPEP695TypeParameterDefaultNotSupported]
2+
class C[T = None]: # E: Type parameter default types not supported when using Python 3.12 type parameter syntax
3+
pass
4+
5+
def f[T = list[int]]() -> None: # E: Type parameter default types not supported when using Python 3.12 type parameter syntax
6+
pass
7+
8+
def g[**P = [int, str]]() -> None: # E: Type parameter default types not supported when using Python 3.12 type parameter syntax
9+
pass
10+
11+
type A[T, S = int, U = str] = list[T] # E: Type parameter default types not supported when using Python 3.12 type parameter syntax

0 commit comments

Comments
 (0)