Skip to content

Commit

Permalink
[PEP 695] Enable new type parameter syntax by default (#17798)
Browse files Browse the repository at this point in the history
I think the PEP 695 syntax is supported well enough now to enable it by
default.
  • Loading branch information
JukkaL authored Sep 20, 2024
1 parent 18fee78 commit 5dfc7d9
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 290 deletions.
15 changes: 1 addition & 14 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ format into the specified directory.
Enabling incomplete/experimental features
*****************************************

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

Some features may require several mypy releases to implement, for example
due to their complexity, potential for backwards incompatibility, or
Expand Down Expand Up @@ -1055,19 +1055,6 @@ List of currently incomplete/experimental features:
# Without PreciseTupleTypes: tuple[int, ...]
# With PreciseTupleTypes: tuple[()] | tuple[int] | tuple[int, int]
* ``NewGenericSyntax``: this feature enables support for syntax defined
by :pep:`695`. For example:

.. code-block:: python
class Container[T]: # defines a generic class
content: T
def first[T](items: list[T]) -> T: # defines a generic function
return items[0]
type Items[T] = list[tuple[T, T]] # defines a generic type alias
* ``InlineTypedDict``: this feature enables non-standard syntax for inline
:ref:`TypedDicts <typeddict>`, for example:

Expand Down
66 changes: 14 additions & 52 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
YieldFromExpr,
check_arg_names,
)
from mypy.options import NEW_GENERIC_SYNTAX, Options
from mypy.options import Options
from mypy.patterns import (
AsPattern,
ClassPattern,
Expand Down Expand Up @@ -965,19 +965,7 @@ def do_func_def(
return_type = AnyType(TypeOfAny.from_error)
else:
if sys.version_info >= (3, 12) and n.type_params:
if NEW_GENERIC_SYNTAX in self.options.enable_incomplete_feature:
explicit_type_params = self.translate_type_params(n.type_params)
else:
self.fail(
ErrorMessage(
"PEP 695 generics are not yet supported. "
"Use --enable-incomplete-feature=NewGenericSyntax for experimental support",
code=codes.VALID_TYPE,
),
n.type_params[0].lineno,
n.type_params[0].col_offset,
blocker=False,
)
explicit_type_params = self.translate_type_params(n.type_params)

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

if sys.version_info >= (3, 12) and n.type_params:
if NEW_GENERIC_SYNTAX in self.options.enable_incomplete_feature:
explicit_type_params = self.translate_type_params(n.type_params)
else:
self.fail(
ErrorMessage(
"PEP 695 generics are not yet supported. "
"Use --enable-incomplete-feature=NewGenericSyntax for experimental support",
code=codes.VALID_TYPE,
),
n.type_params[0].lineno,
n.type_params[0].col_offset,
blocker=False,
)
explicit_type_params = self.translate_type_params(n.type_params)

cdef = ClassDef(
n.name,
Expand Down Expand Up @@ -1843,31 +1819,17 @@ def validate_type_alias(self, n: ast_TypeAlias) -> None:
# TypeAlias(identifier name, type_param* type_params, expr value)
def visit_TypeAlias(self, n: ast_TypeAlias) -> TypeAliasStmt | AssignmentStmt:
node: TypeAliasStmt | AssignmentStmt
if NEW_GENERIC_SYNTAX in self.options.enable_incomplete_feature:
type_params = self.translate_type_params(n.type_params)
self.validate_type_alias(n)
value = self.visit(n.value)
# Since the value is evaluated lazily, wrap the value inside a lambda.
# This helps mypyc.
ret = ReturnStmt(value)
self.set_line(ret, n.value)
value_func = LambdaExpr(body=Block([ret]))
self.set_line(value_func, n.value)
node = TypeAliasStmt(self.visit_Name(n.name), type_params, value_func)
return self.set_line(node, n)
else:
self.fail(
ErrorMessage(
"PEP 695 type aliases are not yet supported. "
"Use --enable-incomplete-feature=NewGenericSyntax for experimental support",
code=codes.VALID_TYPE,
),
n.lineno,
n.col_offset,
blocker=False,
)
node = AssignmentStmt([NameExpr(n.name.id)], self.visit(n.value))
return self.set_line(node, n)
type_params = self.translate_type_params(n.type_params)
self.validate_type_alias(n)
value = self.visit(n.value)
# Since the value is evaluated lazily, wrap the value inside a lambda.
# This helps mypyc.
ret = ReturnStmt(value)
self.set_line(ret, n.value)
value_func = LambdaExpr(body=Block([ret]))
self.set_line(value_func, n.value)
node = TypeAliasStmt(self.visit_Name(n.name), type_params, value_func)
return self.set_line(node, n)


class TypeConverter:
Expand Down
4 changes: 2 additions & 2 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class BuildType:
PRECISE_TUPLE_TYPES: Final = "PreciseTupleTypes"
NEW_GENERIC_SYNTAX: Final = "NewGenericSyntax"
INLINE_TYPEDDICT: Final = "InlineTypedDict"
INCOMPLETE_FEATURES: Final = frozenset((PRECISE_TUPLE_TYPES, NEW_GENERIC_SYNTAX, INLINE_TYPEDDICT))
COMPLETE_FEATURES: Final = frozenset((TYPE_VAR_TUPLE, UNPACK))
INCOMPLETE_FEATURES: Final = frozenset((PRECISE_TUPLE_TYPES, INLINE_TYPEDDICT))
COMPLETE_FEATURES: Final = frozenset((TYPE_VAR_TUPLE, UNPACK, NEW_GENERIC_SYNTAX))


class Options:
Expand Down
2 changes: 0 additions & 2 deletions mypyc/test-data/run-python312.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[case testPEP695Basics]
# flags: --enable-incomplete-feature=NewGenericSyntax
from typing import Any, TypeAliasType, cast

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

[case testPEP695GenericTypeAlias]
# flags: --enable-incomplete-feature=NewGenericSyntax
from typing import Callable
from types import GenericAlias

Expand Down
1 change: 0 additions & 1 deletion mypyc/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
options.preserve_asts = True
options.allow_empty_bodies = True
options.incremental = self.separate
options.enable_incomplete_feature.append("NewGenericSyntax")

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

0 comments on commit 5dfc7d9

Please sign in to comment.