From 4554bd0c6d49506a4a1c77dcf344dca4efee92e1 Mon Sep 17 00:00:00 2001 From: Katrina Connors <32425204+katconnors@users.noreply.github.com> Date: Wed, 18 Sep 2024 14:22:06 -0700 Subject: [PATCH 1/5] Added error code for overlapping function signatures (#17597) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #17570. This is my first contribution to mypy! 🐍 Added an error code for overlapping function signatures. Test in check-errorcodes.test is a derivative of this post: https://stackoverflow.com/q/69341607 Co-authored-by: Jelle Zijlstra Co-authored-by: Alex Waygood --- docs/source/error_code_list.rst | 28 ++++++++++++++++++++++++++++ mypy/errorcodes.py | 8 ++++++++ mypy/messages.py | 1 + test-data/unit/check-errorcodes.test | 14 ++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/docs/source/error_code_list.rst b/docs/source/error_code_list.rst index 85c8d437a856..ad73bc999f00 100644 --- a/docs/source/error_code_list.rst +++ b/docs/source/error_code_list.rst @@ -1149,6 +1149,34 @@ types you expect. See :ref:`overloading ` for more explanation. + +.. _code-overload-cannot-match: + +Check for overload signatures that cannot match [overload-cannot-match] +-------------------------------------------------------------------------- + +Warn if an ``@overload`` variant can never be matched, because an earlier +overload has a wider signature. For example, this can happen if the two +overloads accept the same parameters and each parameter on the first overload +has the same type or a wider type than the corresponding parameter on the second +overload. + +Example: + +.. code-block:: python + + from typing import overload, Union + + @overload + def process(response1: object, response2: object) -> object: + ... + @overload + def process(response1: int, response2: int) -> int: # E: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [overload-cannot-match] + ... + + def process(response1: object, response2: object) -> object: + return response1 + response2 + .. _code-annotation-unchecked: Notify about an annotation in an unchecked function [annotation-unchecked] diff --git a/mypy/errorcodes.py b/mypy/errorcodes.py index 6e8763264ddd..ad061b161af1 100644 --- a/mypy/errorcodes.py +++ b/mypy/errorcodes.py @@ -273,6 +273,14 @@ def __hash__(self) -> int: # This is a catch-all for remaining uncategorized errors. MISC: Final[ErrorCode] = ErrorCode("misc", "Miscellaneous other checks", "General") +OVERLOAD_CANNOT_MATCH: Final[ErrorCode] = ErrorCode( + "overload-cannot-match", + "Warn if an @overload signature can never be matched", + "General", + sub_code_of=MISC, +) + + OVERLOAD_OVERLAP: Final[ErrorCode] = ErrorCode( "overload-overlap", "Warn if multiple @overload variants overlap in unsafe ways", diff --git a/mypy/messages.py b/mypy/messages.py index 62846c536f3d..dadce149680e 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -1653,6 +1653,7 @@ def overloaded_signature_will_never_match( index1=index1, index2=index2 ), context, + code=codes.OVERLOAD_CANNOT_MATCH, ) def overloaded_signatures_typevar_specific(self, index: int, context: Context) -> None: diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index cca13347dd86..10cc145d0c70 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -1222,3 +1222,17 @@ def f(x: str) -> TypeIs[int]: # E: Narrowed type "int" is not a subtype of inpu pass [builtins fixtures/tuple.pyi] + + +[case testOverloadedFunctionSignature] +from typing import overload, Union + +@overload +def process(response1: float,response2: float) -> float: + ... +@overload +def process(response1: int,response2: int) -> int: # E: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [overload-cannot-match] + ... + +def process(response1,response2)-> Union[float,int]: + return response1 + response2 From 2a8c91e6413f07f800833d6e3409070b91d725b4 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 19 Sep 2024 17:44:29 +0100 Subject: [PATCH 2/5] [PEP 695] Fix crash on invalid type var reference (#17788) Test case from typing conformance test suite: https://github.com/python/typing/blob/main/conformance/tests/generics_syntax_declarations.py#L45 --- mypy/semanal.py | 1 + test-data/unit/check-python312.test | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/mypy/semanal.py b/mypy/semanal.py index 27d2a3abf93f..780d0b614ae3 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -6955,6 +6955,7 @@ def name_not_defined(self, name: str, ctx: Context, namespace: str | None = None namespace is None and self.type and not self.is_func_scope() + and self.incomplete_type_stack and self.incomplete_type_stack[-1] and not self.final_iteration ): diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index d0a39f7e56a6..bf1115dc51c5 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -1320,6 +1320,14 @@ class P[T](Protocol[T]): # E: No arguments expected for "Protocol" base class class P2[T](Protocol[S]): # E: No arguments expected for "Protocol" base class pass +[case testPEP695CannotUseTypeVarFromOuterClass] +# mypy: enable-incomplete-feature=NewGenericSyntax +class ClassG[V]: + # This used to crash + class ClassD[T: dict[str, V]]: # E: Name "V" is not defined + ... +[builtins fixtures/dict.pyi] + [case testPEP695MixNewAndOldStyleGenerics] # mypy: enable-incomplete-feature=NewGenericSyntax from typing import TypeVar From a646f330641422d5bc9bf71d1f7751ab2d8a8d5d Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 19 Sep 2024 17:44:57 +0100 Subject: [PATCH 3/5] [PEP 695] Inherit variance if base class has explicit variance (#17787) Previously we only inferred variance based on member types, but if a base class has explicit variance for some type variables, we need to consider it as well. --- mypy/subtypes.py | 9 ++++++ test-data/unit/check-python312.test | 40 ++++++++++++++++++++++++ test-data/unit/fixtures/tuple-simple.pyi | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/mypy/subtypes.py b/mypy/subtypes.py index 5c4471cc5b62..df040dcb1311 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -2031,6 +2031,15 @@ def infer_variance(info: TypeInfo, i: int) -> bool: contra = False if settable: co = False + + # Infer variance from base classes, in case they have explicit variances + for base in info.bases: + base2 = expand_type(base, {tvar.id: object_type}) + if not is_subtype(base, base2): + co = False + if not is_subtype(base2, base): + contra = False + if co: v = COVARIANT elif contra: diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index bf1115dc51c5..5200395047bc 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -1891,3 +1891,43 @@ class A: a = A() v = a.f + +[case testPEP695VarianceInheritedFromBaseWithExplicitVariance] +# flags: --enable-incomplete-feature=NewGenericSyntax +from typing import TypeVar, Generic + +T = TypeVar("T") + +class ParentInvariant(Generic[T]): + pass + +class Invariant1[T](ParentInvariant[T]): + pass + +a1: Invariant1[int] = Invariant1[float]() # E: Incompatible types in assignment (expression has type "Invariant1[float]", variable has type "Invariant1[int]") +a2: Invariant1[float] = Invariant1[int]() # E: Incompatible types in assignment (expression has type "Invariant1[int]", variable has type "Invariant1[float]") + +T_contra = TypeVar("T_contra", contravariant=True) + +class ParentContravariant(Generic[T_contra]): + pass + +class Contravariant[T](ParentContravariant[T]): + pass + +b1: Contravariant[int] = Contravariant[float]() +b2: Contravariant[float] = Contravariant[int]() # E: Incompatible types in assignment (expression has type "Contravariant[int]", variable has type "Contravariant[float]") + +class Invariant2[T](ParentContravariant[T]): + def f(self) -> T: ... + +c1: Invariant2[int] = Invariant2[float]() # E: Incompatible types in assignment (expression has type "Invariant2[float]", variable has type "Invariant2[int]") +c2: Invariant2[float] = Invariant2[int]() # E: Incompatible types in assignment (expression has type "Invariant2[int]", variable has type "Invariant2[float]") + +class Multi[T, S](ParentInvariant[T], ParentContravariant[S]): + pass + +d1: Multi[int, str] = Multi[float, str]() # E: Incompatible types in assignment (expression has type "Multi[float, str]", variable has type "Multi[int, str]") +d2: Multi[float, str] = Multi[int, str]() # E: Incompatible types in assignment (expression has type "Multi[int, str]", variable has type "Multi[float, str]") +d3: Multi[str, int] = Multi[str, float]() +d4: Multi[str, float] = Multi[str, int]() # E: Incompatible types in assignment (expression has type "Multi[str, int]", variable has type "Multi[str, float]") diff --git a/test-data/unit/fixtures/tuple-simple.pyi b/test-data/unit/fixtures/tuple-simple.pyi index 6c816c1c5b7a..07f9edf63cdd 100644 --- a/test-data/unit/fixtures/tuple-simple.pyi +++ b/test-data/unit/fixtures/tuple-simple.pyi @@ -5,7 +5,7 @@ from typing import Iterable, TypeVar, Generic -T = TypeVar('T') +T = TypeVar('T', covariant=True) class object: def __init__(self): pass From 18fee78645a32b4333a1b80720c7b7765775ad4b Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Fri, 20 Sep 2024 12:49:14 +0100 Subject: [PATCH 4/5] [PEP 695] Generate error if new-style type alias used as base class (#17789) It doesn't work at runtime. --- mypy/semanal.py | 15 ++++++++++++ test-data/unit/check-python312.test | 38 ++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 780d0b614ae3..e239fbf1f141 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1824,6 +1824,8 @@ def analyze_class(self, defn: ClassDef) -> None: defn, bases, context=defn ) + self.check_type_alias_bases(bases) + for tvd in tvar_defs: if isinstance(tvd, TypeVarType) and any( has_placeholder(t) for t in [tvd.upper_bound] + tvd.values @@ -1895,6 +1897,19 @@ def analyze_class(self, defn: ClassDef) -> None: self.analyze_class_body_common(defn) + def check_type_alias_bases(self, bases: list[Expression]) -> None: + for base in bases: + if isinstance(base, IndexExpr): + base = base.base + if ( + isinstance(base, RefExpr) + and isinstance(base.node, TypeAlias) + and base.node.python_3_12_type_alias + ): + self.fail( + 'Type alias defined using "type" statement not valid as base class', base + ) + def setup_type_vars(self, defn: ClassDef, tvar_defs: list[TypeVarLikeType]) -> None: defn.type_vars = tvar_defs defn.info.type_vars = [] diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index 5200395047bc..d9737694c262 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -591,6 +591,40 @@ a4: A4 reveal_type(a4) # N: Revealed type is "Union[builtins.int, builtins.str]" [builtins fixtures/type.pyi] +[case testPEP695TypeAliasNotValidAsBaseClass] +# flags: --enable-incomplete-feature=NewGenericSyntax +from typing import TypeAlias + +import m + +type A1 = int +class Bad1(A1): # E: Type alias defined using "type" statement not valid as base class + pass + +type A2[T] = list[T] +class Bad2(A2[int]): # E: Type alias defined using "type" statement not valid as base class + pass + +class Bad3(m.A1): # E: Type alias defined using "type" statement not valid as base class + pass + +class Bad4(m.A2[int]): # E: Type alias defined using "type" statement not valid as base class + pass + +B1 = int +B2 = list +B3: TypeAlias = int +class Good1(B1): pass +class Good2(B2[int]): pass +class Good3(list[A1]): pass +class Good4(list[A2[int]]): pass +class Good5(B3): pass + +[file m.py] +type A1 = str +type A2[T] = list[T] +[typing fixtures/typing-medium.pyi] + [case testPEP695TypeAliasWithUnusedTypeParams] # flags: --enable-incomplete-feature=NewGenericSyntax type A[T] = int @@ -637,9 +671,7 @@ class D: pass type A = C -# Note that this doesn't actually work at runtime, but we currently don't -# keep track whether a type alias is valid in various runtime type contexts. -class D(A): +class D(A): # E: Type alias defined using "type" statement not valid as base class pass class C: pass From 5dfc7d941253553ab77836e9845cb8fdfb9d23a9 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Fri, 20 Sep 2024 14:31:03 +0100 Subject: [PATCH 5/5] [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. --- docs/source/command_line.rst | 15 +- mypy/fastparse.py | 66 ++---- mypy/options.py | 4 +- mypyc/test-data/run-python312.test | 2 - mypyc/test/test_run.py | 1 - test-data/unit/check-python312.test | 240 ++++----------------- test-data/unit/deps.test | 2 +- test-data/unit/diff.test | 16 +- test-data/unit/fine-grained-python312.test | 17 +- test-data/unit/parse-python312.test | 8 +- test-data/unit/pythoneval.test | 4 +- 11 files changed, 85 insertions(+), 290 deletions(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index c085b63107b0..a89a3c85d4ee 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -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 @@ -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 `, for example: diff --git a/mypy/fastparse.py b/mypy/fastparse.py index abcce74c6064..18858b0fa0b8 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -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, @@ -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( @@ -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, @@ -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: diff --git a/mypy/options.py b/mypy/options.py index 5e64d5e40035..56bd92957b41 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -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: diff --git a/mypyc/test-data/run-python312.test b/mypyc/test-data/run-python312.test index 5e8a388fd8d3..a5a3f058d1e2 100644 --- a/mypyc/test-data/run-python312.test +++ b/mypyc/test-data/run-python312.test @@ -1,5 +1,4 @@ [case testPEP695Basics] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import Any, TypeAliasType, cast from testutil import assertRaises @@ -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 diff --git a/mypyc/test/test_run.py b/mypyc/test/test_run.py index 37de192a9291..668e5b124841 100644 --- a/mypyc/test/test_run.py +++ b/mypyc/test/test_run.py @@ -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. diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index d9737694c262..2f0b912c439e 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -1,70 +1,56 @@ -[case test695TypeAlias] -type MyInt = int # E: PEP 695 type aliases are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support +[case testPEP695TypeAliasBasic] +type MyInt = int def f(x: MyInt) -> MyInt: return reveal_type(x) # N: Revealed type is "builtins.int" -type MyList[T] = list[T] # E: PEP 695 type aliases are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: Name "T" is not defined +type MyList[T] = list[T] -def g(x: MyList[int]) -> MyList[int]: # E: Variable "__main__.MyList" is not valid as a type \ - # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases - return reveal_type(x) # N: Revealed type is "MyList?[builtins.int]" +def g(x: MyList[int]) -> MyList[int]: + return reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]" -type MyInt2 = int # type: ignore[valid-type] +type MyInt2 = int def h(x: MyInt2) -> MyInt2: return reveal_type(x) # N: Revealed type is "builtins.int" -[case test695Class] -class MyGen[T]: # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support - def __init__(self, x: T) -> None: # E: Name "T" is not defined +[case testPEP695Class] +class MyGen[T]: + def __init__(self, x: T) -> None: self.x = x -def f(x: MyGen[int]): # E: "MyGen" expects no type arguments, but 1 given - reveal_type(x.x) # N: Revealed type is "Any" +def f(x: MyGen[int]): + reveal_type(x.x) # N: Revealed type is "builtins.int" -[case test695Function] -def f[T](x: T) -> T: # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: Name "T" is not defined - return reveal_type(x) # N: Revealed type is "Any" +[case testPEP695Function] +def f[T](x: T) -> T: + return reveal_type(x) # N: Revealed type is "T`-1" -reveal_type(f(1)) # N: Revealed type is "Any" +reveal_type(f(1)) # N: Revealed type is "builtins.int" -async def g[T](x: T) -> T: # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: Name "T" is not defined - return reveal_type(x) # N: Revealed type is "Any" +async def g[T](x: T) -> T: + return reveal_type(x) # N: Revealed type is "T`-1" -reveal_type(g(1)) # E: Value of type "Coroutine[Any, Any, Any]" must be used \ +reveal_type(g(1)) # E: Value of type "Coroutine[Any, Any, int]" must be used \ # N: Are you missing an await? \ - # N: Revealed type is "typing.Coroutine[Any, Any, Any]" + # N: Revealed type is "typing.Coroutine[Any, Any, builtins.int]" -[case test695TypeVar] +[case testPEP695TypeVarBasic] from typing import Callable -type Alias1[T: int] = list[T] # E: PEP 695 type aliases are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: Name "T" is not defined -type Alias2[**P] = Callable[P, int] # E: PEP 695 type aliases are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: Value of type "int" is not indexable \ - # E: Name "P" is not defined -type Alias3[*Ts] = tuple[*Ts] # E: PEP 695 type aliases are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: Name "Ts" is not defined - -class Cls1[T: int]: ... # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support -class Cls2[**P]: ... # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support -class Cls3[*Ts]: ... # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support - -def func1[T: int](x: T) -> T: ... # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: Name "T" is not defined - -def func2[**P](x: Callable[P, int]) -> Callable[P, str]: ... # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: The first argument to Callable must be a list of types, parameter specification, or "..." \ - # N: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas \ - # E: Name "P" is not defined -def func3[*Ts](x: tuple[*Ts]) -> tuple[int, *Ts]: ... # E: PEP 695 generics are not yet supported. Use --enable-incomplete-feature=NewGenericSyntax for experimental support \ - # E: Name "Ts" is not defined +type Alias1[T: int] = list[T] +type Alias2[**P] = Callable[P, int] +type Alias3[*Ts] = tuple[*Ts] + +class Cls1[T: int]: ... +class Cls2[**P]: ... +class Cls3[*Ts]: ... + +def func1[T: int](x: T) -> T: ... +def func2[**P](x: Callable[P, int]) -> Callable[P, str]: ... +def func3[*Ts](x: tuple[*Ts]) -> tuple[int, *Ts]: ... [builtins fixtures/tuple.pyi] -[case test695TypeAliasType] +[case testPEP695TypeAliasType] from typing import Callable, TypeAliasType, TypeVar, TypeVarTuple T = TypeVar("T") @@ -86,9 +72,13 @@ reveal_type(ba2) # N: Revealed type is "def (*Any) -> builtins.str" [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] -[case testPEP695GenericFunctionSyntax] -# flags: --enable-incomplete-feature=NewGenericSyntax +[case testPEP695IncompleteFeatureIsAcceptedButHasNoEffect] +# mypy: enable-incomplete-feature=NewGenericSyntax +def f[T](x: T) -> T: + return x +reveal_type(f(1)) # N: Revealed type is "builtins.int" +[case testPEP695GenericFunctionSyntax] def ident[TV](x: TV) -> TV: y: TV = x y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "TV") @@ -107,8 +97,6 @@ reveal_type(tup(1, 'x')) # N: Revealed type is "Tuple[builtins.int, builtins.st [builtins fixtures/tuple.pyi] [case testPEP695GenericClassSyntax] -# flags: --enable-incomplete-feature=NewGenericSyntax - class C[T]: x: T @@ -128,8 +116,6 @@ reveal_type(c.x) # N: Revealed type is "builtins.int" reveal_type(c.ident(1)) # N: Revealed type is "builtins.int" [case testPEP695GenericMethodInGenericClass] -# flags: --enable-incomplete-feature=NewGenericSyntax - class C[T]: def m[S](self, x: S) -> T | S: ... @@ -139,8 +125,6 @@ b: C[object] = C[int]() reveal_type(C[str]().m(1)) # N: Revealed type is "Union[builtins.str, builtins.int]" [case testPEP695InferVarianceSimpleFromMethod] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Invariant[T]: def f(self, x: T) -> None: pass @@ -178,8 +162,6 @@ if int(): f = e [case testPEP695InferVarianceSimpleFromAttribute] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Invariant1[T]: def __init__(self, x: T) -> None: self.x = x @@ -214,8 +196,6 @@ if int(): b3 = a3 # E: Incompatible types in assignment (expression has type "Invariant3[object]", variable has type "Invariant3[int]") [case testPEP695InferVarianceRecursive] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Invariant[T]: def f(self, x: Invariant[T]) -> Invariant[T]: return x @@ -250,7 +230,6 @@ if int(): f = e [case testPEP695InferVarianceInFrozenDataclass] -# flags: --enable-incomplete-feature=NewGenericSyntax from dataclasses import dataclass @dataclass(frozen=True) @@ -269,8 +248,6 @@ inv2: Invariant[int] = Invariant[float]([1]) # E: Incompatible types in assignm [builtins fixtures/tuple.pyi] [case testPEP695InferVarianceCalculateOnDemand] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Covariant[T]: def __init__(self) -> None: self.x = [1] @@ -286,8 +263,6 @@ class Covariant[T]: def h(self, x: Covariant[int]) -> None: pass [case testPEP695InferVarianceNotReadyWhenNeeded] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Covariant[T]: def f(self) -> None: c = Covariant[int]() @@ -328,8 +303,6 @@ if int(): b = a # E: Incompatible types in assignment (expression has type "Invariant[object]", variable has type "Invariant[int]") [case testPEP695InferVarianceNotReadyForJoin] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Invariant[T]: def f(self) -> None: # Assume covariance if variance us not ready @@ -342,8 +315,6 @@ class Invariant[T]: reveal_type([Invariant(1), Invariant(object())]) # N: Revealed type is "builtins.list[builtins.object]" [case testPEP695InferVarianceNotReadyForMeet] -# flags: --enable-incomplete-feature=NewGenericSyntax - from typing import TypeVar, Callable S = TypeVar("S") @@ -362,8 +333,6 @@ class Invariant[T]: reveal_type(c(a1, a2)) # N: Revealed type is "Never" [case testPEP695InferVarianceUnderscorePrefix] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Covariant1[T]: def __init__(self, x: T) -> None: self._x = x @@ -425,8 +394,6 @@ inv3_2: Invariant3[int] = Invariant3[float](1) # E: Incompatible types in assig [builtins fixtures/property.pyi] [case testPEP695InheritInvariant] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Invariant[T]: x: T @@ -448,7 +415,6 @@ if int(): b = a # E: Incompatible types in assignment (expression has type "Subclass[int]", variable has type "Subclass[object]") [case testPEP695InheritanceMakesInvariant] -# flags: --enable-incomplete-feature=NewGenericSyntax class Covariant[T]: def f(self) -> T: ... @@ -463,7 +429,6 @@ a: Subclass[int] = Subclass[object]() # E: Incompatible types in assignment (ex b: Subclass[object] = Subclass[int]() # E: Incompatible types in assignment (expression has type "Subclass[int]", variable has type "Subclass[object]") [case testPEP695InheritCoOrContravariant] -# flags: --enable-incomplete-feature=NewGenericSyntax class Contravariant[T]: def f(self, x: T) -> None: pass @@ -489,7 +454,6 @@ e: InvSubclass[int] = InvSubclass[object]() # E: Incompatible types in assignme f: InvSubclass[object] = InvSubclass[int]() # E: Incompatible types in assignment (expression has type "InvSubclass[int]", variable has type "InvSubclass[object]") [case testPEP695FinalAttribute] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import Final class C[T]: @@ -500,8 +464,6 @@ a: C[int] = C[object](1) # E: Incompatible types in assignment (expression has b: C[object] = C[int](1) [case testPEP695TwoTypeVariables] -# flags: --enable-incomplete-feature=NewGenericSyntax - class C[T, S]: def f(self, x: T) -> None: ... def g(self) -> S: ... @@ -512,8 +474,6 @@ c: C[int, int] = C[int, object]() # E: Incompatible types in assignment (expres d: C[int, object] = C[int, int]() [case testPEP695Properties] -# flags: --enable-incomplete-feature=NewGenericSyntax - class R[T]: @property def p(self) -> T: ... @@ -531,7 +491,6 @@ d: RW[object] = RW[int]() # E: Incompatible types in assignment (expression has [builtins fixtures/property.pyi] [case testPEP695Protocol] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import Protocol class PContra[T](Protocol): @@ -568,8 +527,6 @@ if int(): f = e # E: Incompatible types in assignment (expression has type "PInv[int]", variable has type "PInv[object]") [case testPEP695TypeAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax - class C[T]: pass class D[T, S]: pass @@ -592,7 +549,6 @@ reveal_type(a4) # N: Revealed type is "Union[builtins.int, builtins.str]" [builtins fixtures/type.pyi] [case testPEP695TypeAliasNotValidAsBaseClass] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import TypeAlias import m @@ -626,14 +582,11 @@ type A2[T] = list[T] [typing fixtures/typing-medium.pyi] [case testPEP695TypeAliasWithUnusedTypeParams] -# flags: --enable-incomplete-feature=NewGenericSyntax type A[T] = int a: A[str] reveal_type(a) # N: Revealed type is "builtins.int" [case testPEP695TypeAliasForwardReference1] -# flags: --enable-incomplete-feature=NewGenericSyntax - type A[T] = C[T] a: A[int] @@ -642,8 +595,6 @@ reveal_type(a) # N: Revealed type is "__main__.C[builtins.int]" class C[T]: pass [case testPEP695TypeAliasForwardReference2] -# flags: --enable-incomplete-feature=NewGenericSyntax - type X = C type A = X @@ -655,8 +606,6 @@ class C: pass [typing fixtures/typing-full.pyi] [case testPEP695TypeAliasForwardReference3] -# flags: --enable-incomplete-feature=NewGenericSyntax - type X = D type A = C[X] @@ -667,8 +616,6 @@ class C[T]: pass class D: pass [case testPEP695TypeAliasForwardReference4] -# flags: --enable-incomplete-feature=NewGenericSyntax - type A = C class D(A): # E: Type alias defined using "type" statement not valid as base class @@ -680,7 +627,6 @@ x: C = D() y: D = C() # E: Incompatible types in assignment (expression has type "C", variable has type "D") [case testPEP695TypeAliasForwardReference5] -# flags: --enable-incomplete-feature=NewGenericSyntax type A = str type B[T] = C[T] class C[T]: pass @@ -692,13 +638,11 @@ reveal_type(b) # N: Revealed type is "__main__.C[builtins.int]" reveal_type(c) # N: Revealed type is "__main__.C[builtins.str]" [case testPEP695TypeAliasWithUndefineName] -# flags: --enable-incomplete-feature=NewGenericSyntax type A[T] = XXX # E: Name "XXX" is not defined a: A[int] reveal_type(a) # N: Revealed type is "Any" [case testPEP695TypeAliasInvalidType] -# flags: --enable-incomplete-feature=NewGenericSyntax type A = int | 1 # E: Invalid type: try using Literal[1] instead? a: A @@ -709,13 +653,10 @@ reveal_type(b) # N: Revealed type is "Any" [builtins fixtures/type.pyi] [case testPEP695TypeAliasBoundForwardReference] -# mypy: enable-incomplete-feature=NewGenericSyntax type B[T: Foo] = list[T] class Foo: pass [case testPEP695UpperBound] -# flags: --enable-incomplete-feature=NewGenericSyntax - class D: x: int class E(D): pass @@ -738,8 +679,6 @@ reveal_type(f(E())) # N: Revealed type is "__main__.E" f(1) # E: Value of type variable "T" of "f" cannot be "int" [case testPEP695UpperBoundForwardReference1] -# flags: --enable-incomplete-feature=NewGenericSyntax - class C[T: D]: pass a: C[D] @@ -753,8 +692,6 @@ class D: pass class E(D): pass [case testPEP695UpperBoundForwardReference2] -# flags: --enable-incomplete-feature=NewGenericSyntax - type A = D class C[T: A]: pass @@ -769,8 +706,6 @@ reveal_type(b) # N: Revealed type is "__main__.C[__main__.E]" c: C[int] # E: Type argument "int" of "C" must be a subtype of "D" [case testPEP695UpperBoundForwardReference3] -# flags: --enable-incomplete-feature=NewGenericSyntax - class D[T]: pass class E[T](D[T]): pass @@ -788,8 +723,6 @@ reveal_type(b) # N: Revealed type is "__main__.C[__main__.E[__main__.X]]" c: C[D[int]] # E: Type argument "D[int]" of "C" must be a subtype of "D[X]" [case testPEP695UpperBoundForwardReference4] -# flags: --enable-incomplete-feature=NewGenericSyntax - def f[T: D](a: T) -> T: reveal_type(a.x) # N: Revealed type is "builtins.int" return a @@ -803,8 +736,6 @@ reveal_type(f(E())) # N: Revealed type is "__main__.E" f(1) # E: Value of type variable "T" of "f" cannot be "int" [case testPEP695UpperBoundUndefinedName] -# flags: --enable-incomplete-feature=NewGenericSyntax - class C[T: XX]: # E: Name "XX" is not defined pass @@ -815,8 +746,6 @@ def f[T: YY](x: T) -> T: # E: Name "YY" is not defined reveal_type(f) # N: Revealed type is "def [T <: Any] (x: T`-1) -> T`-1" [case testPEP695UpperBoundWithMultipleParams] -# flags: --enable-incomplete-feature=NewGenericSyntax - class C[T, S: int]: pass class D[A: int, B]: pass @@ -834,8 +763,6 @@ f('x', None) # E: Value of type variable "T" of "f" cannot be "str" \ # E: Value of type variable "S" of "f" cannot be "None" [case testPEP695InferVarianceOfTupleType] -# flags: --enable-incomplete-feature=NewGenericSyntax - class Cov[T](tuple[int, str]): def f(self) -> T: pass @@ -855,9 +782,7 @@ e: Contra[int] = Contra[object]() f: Contra[object] = Contra[int]() # E: Incompatible types in assignment (expression has type "Contra[int]", variable has type "Contra[object]") [builtins fixtures/tuple-simple.pyi] -[case testPEP695ValueRestiction] -# flags: --enable-incomplete-feature=NewGenericSyntax - +[case testPEP695ValueRestriction] def f[T: (int, str)](x: T) -> T: reveal_type(x) # N: Revealed type is "builtins.int" \ # N: Revealed type is "builtins.str" @@ -873,9 +798,7 @@ a: C[object] b: C[None] c: C[int] # E: Value of type variable "T" of "C" cannot be "int" -[case testPEP695ValueRestictionForwardReference] -# flags: --enable-incomplete-feature=NewGenericSyntax - +[case testPEP695ValueRestrictionForwardReference] class C[T: (int, D)]: def __init__(self, x: T) -> None: a = x @@ -891,9 +814,7 @@ class D: pass C(D()) -[case testPEP695ValueRestictionUndefinedName] -# flags: --enable-incomplete-feature=NewGenericSyntax - +[case testPEP695ValueRestrictionUndefinedName] class C[T: (int, XX)]: # E: Name "XX" is not defined pass @@ -901,7 +822,6 @@ def f[S: (int, YY)](x: S) -> S: # E: Name "YY" is not defined return x [case testPEP695ParamSpec] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import Callable def g[**P](f: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: @@ -923,7 +843,6 @@ reveal_type(a.m) # N: Revealed type is "def (builtins.int, builtins.str)" [builtins fixtures/tuple.pyi] [case testPEP695ParamSpecTypeAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import Callable type C[**P] = Callable[P, int] @@ -934,8 +853,6 @@ reveal_type(f) # N: Revealed type is "def (builtins.str, Union[builtins.int, No [typing fixtures/typing-full.pyi] [case testPEP695TypeVarTuple] -# flags: --enable-incomplete-feature=NewGenericSyntax - def f[*Ts](t: tuple[*Ts]) -> tuple[*Ts]: reveal_type(t) # N: Revealed type is "Tuple[Unpack[Ts`-1]]" return t @@ -955,7 +872,6 @@ b = c # E: Incompatible types in assignment (expression has type "C[str]", vari [builtins fixtures/tuple.pyi] [case testPEP695TypeVarTupleAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import Callable type C[*Ts] = tuple[*Ts, int] @@ -965,7 +881,6 @@ reveal_type(a) # N: Revealed type is "Tuple[builtins.str, None, builtins.int]" [builtins fixtures/tuple.pyi] [case testPEP695IncrementalFunction] -# flags: --enable-incomplete-feature=NewGenericSyntax import a [file a.py] @@ -992,7 +907,6 @@ tmp/a.py:4: error: Value of type variable "T" of "g" cannot be "str" tmp/a.py:5: error: Value of type variable "S" of "g" cannot be "int" [case testPEP695IncrementalClass] -# flags: --enable-incomplete-feature=NewGenericSyntax import a [file a.py] @@ -1025,7 +939,6 @@ tmp/a.py:12: error: Value of type variable "S" of "D" cannot be "SS" tmp/a.py:13: error: Type argument "object" of "D" must be a subtype of "int" [case testPEP695IncrementalParamSpecAndTypeVarTuple] -# flags: --enable-incomplete-feature=NewGenericSyntax import a [file a.py] @@ -1049,7 +962,6 @@ class D[**P]: tmp/a.py:6: note: Revealed type is "def (builtins.int, builtins.str)" [case testPEP695IncrementalTypeAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax import a [file a.py] @@ -1073,8 +985,6 @@ tmp/a.py:3: note: Revealed type is "builtins.str" tmp/a.py:5: note: Revealed type is "b.Foo[builtins.int]" [case testPEP695UndefinedNameInGenericFunction] -# mypy: enable-incomplete-feature=NewGenericSyntax - def f[T](x: T) -> T: return unknown() # E: Name "unknown" is not defined @@ -1083,7 +993,6 @@ class C: return unknown() # E: Name "unknown" is not defined [case testPEP695FunctionTypeVarAccessInFunction] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import cast class C: @@ -1095,8 +1004,6 @@ class C: reveal_type(C().m(1)) # N: Revealed type is "builtins.int" [case testPEP695ScopingBasics] -# mypy: enable-incomplete-feature=NewGenericSyntax - T = 1 def f[T](x: T) -> T: @@ -1113,8 +1020,6 @@ class C[T]: reveal_type(T) # N: Revealed type is "builtins.int" [case testPEP695ClassScoping] -# mypy: enable-incomplete-feature=NewGenericSyntax - class C: class D: pass @@ -1125,7 +1030,6 @@ C().m(C.D(), C.D()) C().m(1, C.D()) # E: Value of type variable "T" of "m" of "C" cannot be "int" [case testPEP695NestedGenericFunction] -# mypy: enable-incomplete-feature=NewGenericSyntax def f[T](x: T) -> T: reveal_type(f(x)) # N: Revealed type is "T`-1" reveal_type(f(1)) # N: Revealed type is "builtins.int" @@ -1149,7 +1053,6 @@ def f[T](x: T) -> T: return x [case testPEP695NonLocalAndGlobal] -# mypy: enable-incomplete-feature=NewGenericSyntax def f() -> None: T = 1 def g[T](x: T) -> T: @@ -1179,7 +1082,6 @@ class C[T]: return a [case testPEP695ArgumentDefault] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import cast def f[T]( @@ -1199,7 +1101,6 @@ class C: [typing fixtures/typing-full.pyi] [case testPEP695ListComprehension] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import cast def f[T](x: T) -> T: @@ -1208,8 +1109,6 @@ def f[T](x: T) -> T: return x [case testPEP695ReuseNameInSameScope] -# mypy: enable-incomplete-feature=NewGenericSyntax - class C[T]: def m[S](self, x: S, y: T) -> S | T: return x @@ -1231,7 +1130,6 @@ def g[T](x: T) -> T: return x [case testPEP695NestedScopingSpecialCases] -# mypy: enable-incomplete-feature=NewGenericSyntax # This is adapted from PEP 695 S = 0 @@ -1248,7 +1146,6 @@ def outer1[S]() -> None: global S [case testPEP695ScopingWithBaseClasses] -# mypy: enable-incomplete-feature=NewGenericSyntax # This is adapted from PEP 695 class Outer: class Private: @@ -1264,7 +1161,6 @@ class Outer: return a [case testPEP695RedefineTypeParameterInScope] -# mypy: enable-incomplete-feature=NewGenericSyntax class C[T]: def m[T](self, x: T) -> T: # E: "T" already defined as a type parameter return x @@ -1276,7 +1172,6 @@ def f[S, S](x: S) -> S: # E: "S" already defined as a type parameter return x [case testPEP695ClassDecorator] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import Any T = 0 @@ -1288,8 +1183,6 @@ class C[T]: pass [case testPEP695RecursiceTypeAlias] -# mypy: enable-incomplete-feature=NewGenericSyntax - type A = str | list[A] a: A reveal_type(a) # N: Revealed type is "Union[builtins.str, builtins.list[...]]" @@ -1302,8 +1195,6 @@ reveal_type(b) # N: Revealed type is "Union[__main__.C[builtins.int], builtins. [builtins fixtures/type.pyi] [case testPEP695BadRecursiveTypeAlias] -# mypy: enable-incomplete-feature=NewGenericSyntax - type A = A # E: Cannot resolve name "A" (possible cyclic definition) type B = B | int # E: Invalid recursive alias: a union item of itself a: A @@ -1314,8 +1205,6 @@ reveal_type(b) # N: Revealed type is "Any" [typing fixtures/typing-full.pyi] [case testPEP695RecursiveTypeAliasForwardReference] -# mypy: enable-incomplete-feature=NewGenericSyntax - def f(a: A) -> None: if isinstance(a, str): reveal_type(a) # N: Revealed type is "builtins.str" @@ -1334,7 +1223,6 @@ f(C[int]()) # E: Argument 1 to "f" has incompatible type "C[int]"; expected "A" [builtins fixtures/isinstance.pyi] [case testPEP695InvalidGenericOrProtocolBaseClass] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import Generic, Protocol, TypeVar S = TypeVar("S") @@ -1353,7 +1241,6 @@ class P2[T](Protocol[S]): # E: No arguments expected for "Protocol" base class pass [case testPEP695CannotUseTypeVarFromOuterClass] -# mypy: enable-incomplete-feature=NewGenericSyntax class ClassG[V]: # This used to crash class ClassD[T: dict[str, V]]: # E: Name "V" is not defined @@ -1361,7 +1248,6 @@ class ClassG[V]: [builtins fixtures/dict.pyi] [case testPEP695MixNewAndOldStyleGenerics] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import TypeVar S = TypeVar("S") @@ -1382,7 +1268,6 @@ class D[T](C[S]): # E: All type parameters should be declared ("S" not declared pass [case testPEP695MixNewAndOldStyleTypeVarTupleAndParamSpec] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import TypeVarTuple, ParamSpec, Callable Ts = TypeVarTuple("Ts") P = ParamSpec("P") @@ -1394,7 +1279,6 @@ def g[T](x: T, f: tuple[*Ts] # E: All type parameters should be declared ("Ts" [builtins fixtures/tuple.pyi] [case testPEP695MixNewAndOldStyleGenericsInTypeAlias] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import TypeVar, ParamSpec, TypeVarTuple, Callable T = TypeVar("T") @@ -1411,7 +1295,6 @@ type C = Callable[P, None] # E: All type parameters should be declared ("P" not [typing fixtures/typing-full.pyi] [case testPEP695NonGenericAliasToGenericClass] -# mypy: enable-incomplete-feature=NewGenericSyntax class C[T]: pass type A = C x: C @@ -1421,7 +1304,6 @@ reveal_type(y) # N: Revealed type is "__main__.C[Any]" z: A[int] # E: Bad number of arguments for type alias, expected 0, given 1 [case testPEP695SelfType] -# mypy: enable-incomplete-feature=NewGenericSyntax from typing import Self class C: @@ -1452,8 +1334,6 @@ reveal_type(F[str]().mm(b'x')) # N: Revealed type is "Tuple[__main__.F[builtins [builtins fixtures/tuple.pyi] [case testPEP695CallAlias] -# mypy: enable-incomplete-feature=NewGenericSyntax - class C: def __init__(self, x: str) -> None: ... type A = C @@ -1476,7 +1356,6 @@ B2[int]() [typing fixtures/typing-full.pyi] [case testPEP695IncrementalTypeAliasKinds] -# flags: --enable-incomplete-feature=NewGenericSyntax import a [file a.py] @@ -1499,7 +1378,6 @@ C: TypeAlias = int tmp/a.py:2: error: "TypeAliasType" not callable [case testPEP695TypeAliasBoundAndValueChecking] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import Any, cast class C: pass @@ -1540,8 +1418,6 @@ c4: A3[int, str] # E: Type argument "int" of "A3" must be a subtype of "C" [typing fixtures/typing-full.pyi] [case testPEP695TypeAliasInClassBodyOrFunction] -# flags: --enable-incomplete-feature=NewGenericSyntax - class C: type A = int type B[T] = list[T] | None @@ -1594,16 +1470,14 @@ reveal_type(E[str]().a) # N: Revealed type is "builtins.list[Any]" [typing fixtures/typing-full.pyi] [case testPEP695RedefineAsTypeAlias1] -# flags: --enable-incomplete-feature=NewGenericSyntax class C: pass -type C = int # E: Name "C" already defined on line 2 +type C = int # E: Name "C" already defined on line 1 A = 0 -type A = str # E: Name "A" already defined on line 5 +type A = str # E: Name "A" already defined on line 4 reveal_type(A) # N: Revealed type is "builtins.int" [case testPEP695RedefineAsTypeAlias2] -# flags: --enable-incomplete-feature=NewGenericSyntax from m import D type D = int # E: Name "D" already defined (possibly by an import) a: D @@ -1612,30 +1486,26 @@ reveal_type(a) # N: Revealed type is "m.D" class D: pass [case testPEP695RedefineAsTypeAlias3] -# flags: --enable-incomplete-feature=NewGenericSyntax D = list["Forward"] -type D = int # E: Name "D" already defined on line 2 +type D = int # E: Name "D" already defined on line 1 Forward = str x: D reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]" [case testPEP695MultiDefinitionsForTypeAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax if int(): type A[T] = list[T] else: - type A[T] = str # E: Name "A" already defined on line 3 + type A[T] = str # E: Name "A" already defined on line 2 x: T # E: Name "T" is not defined a: A[int] reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" [case testPEP695UndefinedNameInAnnotation] -# flags: --enable-incomplete-feature=NewGenericSyntax def f[T](x: foobar, y: T) -> T: ... # E: Name "foobar" is not defined reveal_type(f) # N: Revealed type is "def [T] (x: Any, y: T`-1) -> T`-1" [case testPEP695WrongNumberOfConstrainedTypes] -# flags: --enable-incomplete-feature=NewGenericSyntax type A[T: ()] = list[T] # E: Type variable must have at least two constrained types a: A[int] reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" @@ -1645,7 +1515,6 @@ b: B[str] reveal_type(b) # N: Revealed type is "builtins.list[builtins.str]" [case testPEP695UsingTypeVariableInOwnBoundOrConstraint] -# flags: --enable-incomplete-feature=NewGenericSyntax type A[T: list[T]] = str # E: Name "T" is not defined type B[S: (list[S], str)] = str # E: Name "S" is not defined type C[T, S: list[T]] = str # E: Name "T" is not defined @@ -1655,7 +1524,6 @@ class D[T: T]: # E: Name "T" is not defined pass [case testPEP695InvalidType] -# flags: --enable-incomplete-feature=NewGenericSyntax def f[T: 1](x: T) -> T: ... # E: Invalid type: try using Literal[1] instead? class C[T: (int, (1 + 2))]: pass # E: Invalid type comment or annotation type A = list[1] # E: Invalid type: try using Literal[1] instead? @@ -1666,7 +1534,6 @@ b: B reveal_type(b) # N: Revealed type is "Any" [case testPEP695GenericNamedTuple] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import NamedTuple # Invariant because of the signature of the generated _replace method @@ -1693,7 +1560,6 @@ e: M[bool] # E: Value of type variable "T" of "M" cannot be "bool" [builtins fixtures/tuple.pyi] [case testPEP695GenericTypedDict] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import TypedDict class D[T](TypedDict): @@ -1714,7 +1580,6 @@ d: E[int] # E: Type argument "int" of "E" must be a subtype of "str" [typing fixtures/typing-full.pyi] [case testCurrentClassWorksAsBound] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import Protocol class Comparable[T: Comparable](Protocol): @@ -1727,7 +1592,6 @@ x: Comparable[Good] y: Comparable[int] # E: Type argument "int" of "Comparable" must be a subtype of "Comparable[Any]" [case testPEP695TypeAliasWithDifferentTargetTypes] -# flags: --enable-incomplete-feature=NewGenericSyntax import types # We need GenericAlias from here, and test stubs don't bring in 'types' from typing import Any, Callable, List, Literal, TypedDict @@ -1779,7 +1643,7 @@ type I3 = None | C[TD] [typing fixtures/typing-full.pyi] [case testTypedDictInlineYesNewStyleAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax --enable-incomplete-feature=InlineTypedDict +# flags: --enable-incomplete-feature=InlineTypedDict type X[T] = {"item": T, "other": X[T] | None} x: X[str] reveal_type(x) # N: Revealed type is "TypedDict({'item': builtins.str, 'other': Union[..., None]})" @@ -1791,8 +1655,6 @@ type Y[T] = {"item": T, **Y[T]} # E: Overwriting TypedDict field "item" while m [typing fixtures/typing-full.pyi] [case testPEP695UsingIncorrectExpressionsInTypeVariableBound] -# flags: --enable-incomplete-feature=NewGenericSyntax - type X[T: (yield 1)] = Any # E: Yield expression cannot be used as a type variable bound type Y[T: (yield from [])] = Any # E: Yield expression cannot be used as a type variable bound type Z[T: (a := 1)] = Any # E: Named expression cannot be used as a type variable bound @@ -1824,8 +1686,6 @@ def fooz_nested[T: (1 + (a := 1))](): pass # E: Named expression cannot be used def fook_nested[T: (1 +(await 1))](): pass # E: Await expression cannot be used as a type variable bound [case testPEP695UsingIncorrectExpressionsInTypeAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax - type X = (yield 1) # E: Yield expression cannot be used within a type alias type Y = (yield from []) # E: Yield expression cannot be used within a type alias type Z = (a := 1) # E: Named expression cannot be used within a type alias @@ -1837,7 +1697,6 @@ type ZNested = (1 + (a := 1)) # E: Named expression cannot be used within a typ type KNested = (1 + (await 1)) # E: Await expression cannot be used within a type alias [case testPEP695TypeAliasAndAnnotated] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing_extensions import Annotated, Annotated as _Annotated import typing_extensions as t @@ -1859,7 +1718,6 @@ reveal_type(zz) # N: Revealed type is "builtins.str" [builtins fixtures/tuple.pyi] [case testPEP695NestedGenericClass1] -# flags: --enable-incomplete-feature=NewGenericSyntax class C[T]: def f(self) -> T: ... @@ -1882,7 +1740,6 @@ reveal_type(x) # N: Revealed type is "__main__.A.B[builtins.int]" reveal_type(A.b) # N: Revealed type is "__main__.A.B[builtins.str]" [case testPEP695NestedGenericClass2] -# flags: --enable-incomplete-feature=NewGenericSyntax class A: def m(self) -> None: class B[T]: @@ -1891,11 +1748,10 @@ class A: reveal_type(x.f()) # N: Revealed type is "builtins.int" self.a = B[str]() -reveal_type(A().a) # N: Revealed type is "__main__.B@4[builtins.str]" +reveal_type(A().a) # N: Revealed type is "__main__.B@3[builtins.str]" reveal_type(A().a.f()) # N: Revealed type is "builtins.str" [case testPEP695NestedGenericClass3] -# flags: --enable-incomplete-feature=NewGenericSyntax class C[T]: def f(self) -> T: ... class D[S]: @@ -1914,7 +1770,6 @@ class E[T]: c: E.F[int] [case testPEP695NestedGenericClass4] -# flags: --enable-incomplete-feature=NewGenericSyntax class A: class B[T]: def __get__(self, instance: A, owner: type[A]) -> T: @@ -1925,7 +1780,6 @@ a = A() v = a.f [case testPEP695VarianceInheritedFromBaseWithExplicitVariance] -# flags: --enable-incomplete-feature=NewGenericSyntax from typing import TypeVar, Generic T = TypeVar("T") diff --git a/test-data/unit/deps.test b/test-data/unit/deps.test index 3364dee6c696..84cea99bf2f6 100644 --- a/test-data/unit/deps.test +++ b/test-data/unit/deps.test @@ -1433,7 +1433,7 @@ class B(A): -> m [case testPEP695TypeAliasDeps] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 from a import C, E type A = C type A2 = A diff --git a/test-data/unit/diff.test b/test-data/unit/diff.test index 9212d902e8b2..10fb0a80cf38 100644 --- a/test-data/unit/diff.test +++ b/test-data/unit/diff.test @@ -1532,7 +1532,7 @@ __main__.C.get_by_team_and_id __main__.Optional [case testPEP695TypeAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 from typing_extensions import TypeAlias, TypeAliasType type A = int type B = str @@ -1544,7 +1544,7 @@ G = TypeAliasType("G", int) type H = int [file next.py] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 from typing_extensions import TypeAlias, TypeAliasType type A = str type B = str @@ -1566,7 +1566,7 @@ __main__.G __main__.H [case testPEP695TypeAlias2] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 type A[T: int] = list[T] type B[T: int] = list[T] type C[T: (int, str)] = list[T] @@ -1575,7 +1575,7 @@ type E[T: int] = list[T] type F[T: (int, str)] = list[T] [file next.py] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 type A[T] = list[T] type B[T: str] = list[T] type C[T: (int, None)] = list[T] @@ -1590,13 +1590,13 @@ __main__.C __main__.D [case testPEP695GenericFunction] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 def f[T](x: T) -> T: return x def g[T](x: T, y: T) -> T: return x [file next.py] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 def f[T](x: T) -> T: return x def g[T, S](x: T, y: S) -> S: @@ -1605,7 +1605,7 @@ def g[T, S](x: T, y: S) -> S: __main__.g [case testPEP695GenericClass] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 class C[T]: pass class D[T]: @@ -1615,7 +1615,7 @@ class E[T]: class F[T]: def f(self, x: object) -> T: ... [file next.py] -# flags: --enable-incomplete-feature=NewGenericSyntax --python-version=3.12 +# flags: --python-version=3.12 class C[T]: pass class D[T: int]: diff --git a/test-data/unit/fine-grained-python312.test b/test-data/unit/fine-grained-python312.test index 80e4e4ca3bd8..0e438ca06574 100644 --- a/test-data/unit/fine-grained-python312.test +++ b/test-data/unit/fine-grained-python312.test @@ -1,5 +1,4 @@ [case testPEP695TypeAliasDep] -# flags: --enable-incomplete-feature=NewGenericSyntax import m def g() -> m.C: return m.f() @@ -15,10 +14,9 @@ def f() -> int: pass [out] == -main:4: error: Incompatible return value type (got "int", expected "str") +main:3: error: Incompatible return value type (got "int", expected "str") [case testPEP695ChangeOldStyleToNewStyleTypeAlias] -# flags: --enable-incomplete-feature=NewGenericSyntax from m import A A() @@ -31,10 +29,9 @@ type A = int [builtins fixtures/tuple.pyi] [out] == -main:3: error: "TypeAliasType" not callable +main:2: error: "TypeAliasType" not callable [case testPEP695VarianceChangesDueToDependency] -# flags: --enable-incomplete-feature=NewGenericSyntax from a import C x: C[object] = C[int]() @@ -55,10 +52,9 @@ class A[T]: [out] == -main:4: error: Incompatible types in assignment (expression has type "C[int]", variable has type "C[object]") +main:3: error: Incompatible types in assignment (expression has type "C[int]", variable has type "C[object]") [case testPEP695TypeAliasChangesDueToDependency] -# flags: --enable-incomplete-feature=NewGenericSyntax from a import A x: A x = 0 @@ -78,11 +74,10 @@ from builtins import tuple as B [typing fixtures/typing-full.pyi] [out] == -main:4: error: Incompatible types in assignment (expression has type "int", variable has type "tuple[int, str]") -main:5: error: Incompatible types in assignment (expression has type "str", variable has type "tuple[int, str]") +main:3: error: Incompatible types in assignment (expression has type "int", variable has type "Tuple[int, str]") +main:4: error: Incompatible types in assignment (expression has type "str", variable has type "Tuple[int, str]") [case testPEP695NestedGenericClassMethodUpdated] -# flags: --enable-incomplete-feature=NewGenericSyntax from a import f class C: @@ -99,4 +94,4 @@ def f() -> None: pass def f(x: int) -> None: pass [out] == -main:8: error: Missing positional argument "x" in call to "f" +main:7: error: Missing positional argument "x" in call to "f" diff --git a/test-data/unit/parse-python312.test b/test-data/unit/parse-python312.test index 90ee96f38deb..2b1f9b42e0f7 100644 --- a/test-data/unit/parse-python312.test +++ b/test-data/unit/parse-python312.test @@ -1,5 +1,5 @@ [case testPEP695TypeAlias] -# mypy: enable-incomplete-feature=NewGenericSyntax +# comment type A[T] = C[T] [out] MypyFile:1( @@ -15,7 +15,7 @@ MypyFile:1( NameExpr(T))))))) [case testPEP695GenericFunction] -# mypy: enable-incomplete-feature=NewGenericSyntax +# comment def f[T](): pass def g[T: str](): pass @@ -46,7 +46,7 @@ MypyFile:1( PassStmt:5()))) [case testPEP695ParamSpec] -# mypy: enable-incomplete-feature=NewGenericSyntax +# comment def f[**P](): pass class C[T: int, **P]: pass @@ -68,7 +68,7 @@ MypyFile:1( PassStmt:4())) [case testPEP695TypeVarTuple] -# mypy: enable-incomplete-feature=NewGenericSyntax +# comment def f[*Ts](): pass class C[T: int, *Ts]: pass diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index dbf228623d7c..26fc419030bf 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -2099,7 +2099,7 @@ def f(d: Description) -> None: _testDataclassStrictOptionalAlwaysSet.py:9: note: Revealed type is "def (Union[builtins.int, None]) -> Union[builtins.str, None]" [case testPEP695VarianceInference] -# flags: --python-version=3.12 --enable-incomplete-feature=NewGenericSyntax +# flags: --python-version=3.12 from typing import Callable, Final class Job[_R_co]: @@ -2120,7 +2120,7 @@ def func( _testPEP695VarianceInference.py:17: error: Incompatible types in assignment (expression has type "Job[None]", variable has type "Job[int]") [case testPEP695TypeAliasWithDifferentTargetTypes] -# flags: --python-version=3.12 --enable-incomplete-feature=NewGenericSyntax +# flags: --python-version=3.12 from typing import Any, Callable, List, Literal, TypedDict, overload, TypeAlias, TypeVar, Never class C[T]: pass