Skip to content

[PEP 696] Report error if using unsupported type parameter defaults #17876

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

Merged
merged 1 commit into from
Oct 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,13 @@ def translate_type_params(self, type_params: list[Any]) -> list[TypeParam]:
for p in type_params:
bound = None
values: list[Type] = []
if sys.version_info >= (3, 13) and p.default_value is not None:
self.fail(
message_registry.TYPE_PARAM_DEFAULT_NOT_SUPPORTED,
p.lineno,
p.col_offset,
blocker=False,
)
if isinstance(p, ast_ParamSpec): # type: ignore[misc]
explicit_type_params.append(TypeParam(p.name, PARAM_SPEC_KIND, None, []))
elif isinstance(p, ast_TypeVarTuple): # type: ignore[misc]
Expand Down
5 changes: 5 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,8 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
TYPE_ALIAS_WITH_AWAIT_EXPRESSION: Final = ErrorMessage(
"Await expression cannot be used within a type alias", codes.SYNTAX
)

TYPE_PARAM_DEFAULT_NOT_SUPPORTED: Final = ErrorMessage(
"Type parameter default types not supported when using Python 3.12 type parameter syntax",
codes.MISC,
)
13 changes: 3 additions & 10 deletions mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,9 @@ def local_sys_path_set() -> Iterator[None]:


def testfile_pyversion(path: str) -> tuple[int, int]:
if path.endswith("python312.test"):
return 3, 12
elif 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
m = re.search(r"python3([0-9]+)\.test$", path)
if m:
return 3, int(m.group(1))
else:
return defaults.PYTHON3_VERSION

Expand Down
2 changes: 2 additions & 0 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
typecheck_files.remove("check-python311.test")
if sys.version_info < (3, 12):
typecheck_files.remove("check-python312.test")
if sys.version_info < (3, 13):
typecheck_files.remove("check-python313.test")

# Special tests for platforms with case-insensitive filesystems.
if sys.platform not in ("darwin", "win32"):
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-python313.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[case testPEP695TypeParameterDefaultNotSupported]
class C[T = None]: # E: Type parameter default types not supported when using Python 3.12 type parameter syntax
pass

def f[T = list[int]]() -> None: # E: Type parameter default types not supported when using Python 3.12 type parameter syntax
pass

def g[**P = [int, str]]() -> None: # E: Type parameter default types not supported when using Python 3.12 type parameter syntax
pass

type A[T, S = int, U = str] = list[T] # E: Type parameter default types not supported when using Python 3.12 type parameter syntax
Loading