Skip to content

Commit 5dfc7d9

Browse files
authored
[PEP 695] Enable new type parameter syntax by default (#17798)
I think the PEP 695 syntax is supported well enough now to enable it by default.
1 parent 18fee78 commit 5dfc7d9

11 files changed

+85
-290
lines changed

docs/source/command_line.rst

+1-14
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ format into the specified directory.
10081008
Enabling incomplete/experimental features
10091009
*****************************************
10101010

1011-
.. option:: --enable-incomplete-feature {PreciseTupleTypes, NewGenericSyntax, InlineTypedDict}
1011+
.. option:: --enable-incomplete-feature {PreciseTupleTypes, InlineTypedDict}
10121012

10131013
Some features may require several mypy releases to implement, for example
10141014
due to their complexity, potential for backwards incompatibility, or
@@ -1055,19 +1055,6 @@ List of currently incomplete/experimental features:
10551055
# Without PreciseTupleTypes: tuple[int, ...]
10561056
# With PreciseTupleTypes: tuple[()] | tuple[int] | tuple[int, int]
10571057
1058-
* ``NewGenericSyntax``: this feature enables support for syntax defined
1059-
by :pep:`695`. For example:
1060-
1061-
.. code-block:: python
1062-
1063-
class Container[T]: # defines a generic class
1064-
content: T
1065-
1066-
def first[T](items: list[T]) -> T: # defines a generic function
1067-
return items[0]
1068-
1069-
type Items[T] = list[tuple[T, T]] # defines a generic type alias
1070-
10711058
* ``InlineTypedDict``: this feature enables non-standard syntax for inline
10721059
:ref:`TypedDicts <typeddict>`, for example:
10731060

mypy/fastparse.py

+14-52
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
YieldFromExpr,
9393
check_arg_names,
9494
)
95-
from mypy.options import NEW_GENERIC_SYNTAX, Options
95+
from mypy.options import Options
9696
from mypy.patterns import (
9797
AsPattern,
9898
ClassPattern,
@@ -965,19 +965,7 @@ def do_func_def(
965965
return_type = AnyType(TypeOfAny.from_error)
966966
else:
967967
if sys.version_info >= (3, 12) and n.type_params:
968-
if NEW_GENERIC_SYNTAX in self.options.enable_incomplete_feature:
969-
explicit_type_params = self.translate_type_params(n.type_params)
970-
else:
971-
self.fail(
972-
ErrorMessage(
973-
"PEP 695 generics are not yet supported. "
974-
"Use --enable-incomplete-feature=NewGenericSyntax for experimental support",
975-
code=codes.VALID_TYPE,
976-
),
977-
n.type_params[0].lineno,
978-
n.type_params[0].col_offset,
979-
blocker=False,
980-
)
968+
explicit_type_params = self.translate_type_params(n.type_params)
981969

982970
arg_types = [a.type_annotation for a in args]
983971
return_type = TypeConverter(
@@ -1157,19 +1145,7 @@ def visit_ClassDef(self, n: ast3.ClassDef) -> ClassDef:
11571145
explicit_type_params: list[TypeParam] | None = None
11581146

11591147
if sys.version_info >= (3, 12) and n.type_params:
1160-
if NEW_GENERIC_SYNTAX in self.options.enable_incomplete_feature:
1161-
explicit_type_params = self.translate_type_params(n.type_params)
1162-
else:
1163-
self.fail(
1164-
ErrorMessage(
1165-
"PEP 695 generics are not yet supported. "
1166-
"Use --enable-incomplete-feature=NewGenericSyntax for experimental support",
1167-
code=codes.VALID_TYPE,
1168-
),
1169-
n.type_params[0].lineno,
1170-
n.type_params[0].col_offset,
1171-
blocker=False,
1172-
)
1148+
explicit_type_params = self.translate_type_params(n.type_params)
11731149

11741150
cdef = ClassDef(
11751151
n.name,
@@ -1843,31 +1819,17 @@ def validate_type_alias(self, n: ast_TypeAlias) -> None:
18431819
# TypeAlias(identifier name, type_param* type_params, expr value)
18441820
def visit_TypeAlias(self, n: ast_TypeAlias) -> TypeAliasStmt | AssignmentStmt:
18451821
node: TypeAliasStmt | AssignmentStmt
1846-
if NEW_GENERIC_SYNTAX in self.options.enable_incomplete_feature:
1847-
type_params = self.translate_type_params(n.type_params)
1848-
self.validate_type_alias(n)
1849-
value = self.visit(n.value)
1850-
# Since the value is evaluated lazily, wrap the value inside a lambda.
1851-
# This helps mypyc.
1852-
ret = ReturnStmt(value)
1853-
self.set_line(ret, n.value)
1854-
value_func = LambdaExpr(body=Block([ret]))
1855-
self.set_line(value_func, n.value)
1856-
node = TypeAliasStmt(self.visit_Name(n.name), type_params, value_func)
1857-
return self.set_line(node, n)
1858-
else:
1859-
self.fail(
1860-
ErrorMessage(
1861-
"PEP 695 type aliases are not yet supported. "
1862-
"Use --enable-incomplete-feature=NewGenericSyntax for experimental support",
1863-
code=codes.VALID_TYPE,
1864-
),
1865-
n.lineno,
1866-
n.col_offset,
1867-
blocker=False,
1868-
)
1869-
node = AssignmentStmt([NameExpr(n.name.id)], self.visit(n.value))
1870-
return self.set_line(node, n)
1822+
type_params = self.translate_type_params(n.type_params)
1823+
self.validate_type_alias(n)
1824+
value = self.visit(n.value)
1825+
# Since the value is evaluated lazily, wrap the value inside a lambda.
1826+
# This helps mypyc.
1827+
ret = ReturnStmt(value)
1828+
self.set_line(ret, n.value)
1829+
value_func = LambdaExpr(body=Block([ret]))
1830+
self.set_line(value_func, n.value)
1831+
node = TypeAliasStmt(self.visit_Name(n.name), type_params, value_func)
1832+
return self.set_line(node, n)
18711833

18721834

18731835
class TypeConverter:

mypy/options.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class BuildType:
7575
PRECISE_TUPLE_TYPES: Final = "PreciseTupleTypes"
7676
NEW_GENERIC_SYNTAX: Final = "NewGenericSyntax"
7777
INLINE_TYPEDDICT: Final = "InlineTypedDict"
78-
INCOMPLETE_FEATURES: Final = frozenset((PRECISE_TUPLE_TYPES, NEW_GENERIC_SYNTAX, INLINE_TYPEDDICT))
79-
COMPLETE_FEATURES: Final = frozenset((TYPE_VAR_TUPLE, UNPACK))
78+
INCOMPLETE_FEATURES: Final = frozenset((PRECISE_TUPLE_TYPES, INLINE_TYPEDDICT))
79+
COMPLETE_FEATURES: Final = frozenset((TYPE_VAR_TUPLE, UNPACK, NEW_GENERIC_SYNTAX))
8080

8181

8282
class Options:

mypyc/test-data/run-python312.test

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[case testPEP695Basics]
2-
# flags: --enable-incomplete-feature=NewGenericSyntax
32
from typing import Any, TypeAliasType, cast
43

54
from testutil import assertRaises
@@ -192,7 +191,6 @@ def test_recursive_type_alias() -> None:
192191
[typing fixtures/typing-full.pyi]
193192

194193
[case testPEP695GenericTypeAlias]
195-
# flags: --enable-incomplete-feature=NewGenericSyntax
196194
from typing import Callable
197195
from types import GenericAlias
198196

mypyc/test/test_run.py

-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
196196
options.preserve_asts = True
197197
options.allow_empty_bodies = True
198198
options.incremental = self.separate
199-
options.enable_incomplete_feature.append("NewGenericSyntax")
200199

201200
# Avoid checking modules/packages named 'unchecked', to provide a way
202201
# to test interacting with code we don't have types for.

0 commit comments

Comments
 (0)