diff --git a/mypy/nativeparse.py b/mypy/nativeparse.py index 414426580fa7..d5af1c8c6ec8 100644 --- a/mypy/nativeparse.py +++ b/mypy/nativeparse.py @@ -167,8 +167,9 @@ class State: - def __init__(self, options: Options) -> None: + def __init__(self, options: Options, is_stub: bool = False) -> None: self.options = options + self.is_stub = is_stub self.errors: list[ParseError] = [] self.num_funcs = 0 @@ -180,6 +181,29 @@ def add_error( {"line": line, "column": column, "message": message, "blocker": blocker, "code": code} ) + def check_min_version( + self, + feature: str, + min_version: tuple[int, int], + line: int, + column: int, + *, + enforce_in_stubs: bool = False, + ) -> None: + """Report a non blocker syntax error if the target Python feature is older than min_version.""" + if self.is_stub and not enforce_in_stubs: + return + if self.options.python_version < min_version: + curr = self.options.python_version + self.add_error( + f"{feature}: requires Python {min_version[0]}.{min_version[1]} or newer " + f"(current target: Python {curr[0]}.{curr[1]})", + line, + column, + blocker=False, + code="syntax", + ) + def native_parse( filename: str, @@ -607,6 +631,13 @@ def read_parameters(state: State, data: ReadBuffer) -> tuple[list[Argument], boo return arguments, has_ann +def check_type_param_defaults( + state: State, type_params: list[TypeParam], line: int, column: int +) -> None: + if any(p.default is not None for p in type_params): + state.check_min_version("Type parameter defaults", (3, 13), line, column) + + def read_type_params(state: State, data: ReadBuffer) -> list[TypeParam]: """Read type parameters (PEP 695 generics).""" type_params: list[TypeParam] = [] @@ -680,6 +711,11 @@ def read_func_def(state: State, data: ReadBuffer) -> FuncDef: if is_async: func_def.is_coroutine = True read_loc(data, func_def) + if type_params: + state.check_min_version( + "Improved type parameter syntax", (3, 12), func_def.line, func_def.column + ) + check_type_param_defaults(state, type_params, func_def.line, func_def.column) if typ: typ.line = func_def.line typ.column = func_def.column @@ -727,6 +763,11 @@ def read_class_def(state: State, data: ReadBuffer) -> ClassDef: ) class_def.decorators = decorators read_loc(data, class_def) + if type_params: + state.check_min_version( + "Improved type parameter syntax", (3, 12), class_def.line, class_def.column + ) + check_type_param_defaults(state, type_params, class_def.line, class_def.column) expect_end_tag(data) return class_def @@ -781,6 +822,8 @@ def read_type_alias_stmt(state: State, data: ReadBuffer) -> TypeAliasStmt: stmt = TypeAliasStmt(name, type_params, lambda_expr) read_loc(data, stmt) + state.check_min_version('"type" statements', (3, 12), stmt.line, stmt.column) + check_type_param_defaults(state, type_params, stmt.line, stmt.column) expect_end_tag(data) return stmt @@ -832,6 +875,10 @@ def read_try_stmt(state: State, data: ReadBuffer) -> TryStmt: stmt = TryStmt(body, vars_list, types_list, handlers, else_body, finally_body) stmt.is_star = is_star read_loc(data, stmt) + if is_star: + state.check_min_version("Exception groups", (3, 11), stmt.line, stmt.column) + if state.options.python_version < (3, 11): + stmt.is_star = False expect_end_tag(data) return stmt @@ -967,6 +1014,8 @@ def read_type(state: State, data: ReadBuffer) -> Type: from_star_syntax = read_bool(data) unpack = UnpackType(inner_type, from_star_syntax=from_star_syntax) read_loc(data, unpack) + if from_star_syntax: + state.check_min_version("Star unpack syntax", (3, 11), unpack.line, unpack.column) expect_end_tag(data) return unpack elif tag == types.CALL_TYPE: @@ -1474,6 +1523,9 @@ def read_expression(state: State, data: ReadBuffer) -> Expression: titems.append(s) expr = TemplateStrExpr(titems) read_loc(data, expr) + state.check_min_version( + "t-strings", (3, 14), expr.line, expr.column, enforce_in_stubs=True + ) expect_end_tag(data) return expr elif tag == nodes.LAMBDA_EXPR: diff --git a/mypy/parse.py b/mypy/parse.py index a8fb5542a704..47e2f95f0f30 100644 --- a/mypy/parse.py +++ b/mypy/parse.py @@ -71,7 +71,7 @@ def load_from_raw( """ from mypy.nativeparse import State, deserialize_imports, read_statements - state = State(options) + state = State(options, is_stub=fnam.endswith(".pyi")) if imports_only: defs = [] else: diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 8d3f3cd80531..c5c1643cf0b0 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -578,6 +578,7 @@ reveal_type(b) # N: Revealed type is "def (*Any) -> builtins.int" [builtins fixtures/tuple.pyi] [case testTypeVarTuplePep646CallableNewSyntax] +# flags: --python-version 3.11 from typing import Callable, Generic, Tuple from typing_extensions import ParamSpec @@ -2155,6 +2156,7 @@ def f(x: int | None): [builtins fixtures/tuple.pyi] [case testJoinOfVariadicTupleCallablesNoCrash] +# flags: --python-version 3.11 from typing import Callable, Tuple f: Callable[[int, *Tuple[str, ...], int], None] @@ -2608,6 +2610,7 @@ def test(xs: tuple[Unpack[Ts]], xsi: tuple[int, Unpack[Ts]]) -> None: [builtins fixtures/tuple.pyi] [case testTypeVarTupleInferAgainstAnyCallableSuffix] +# flags: --python-version 3.11 from typing import Any, Callable, TypeVar, TypeVarTuple Ts = TypeVarTuple("Ts") @@ -2620,6 +2623,7 @@ reveal_type(deco(untyped)) # N: Revealed type is "def (*Any) -> Any" [builtins fixtures/tuple.pyi] [case testNoCrashOnNonNormalUnpackInCallable] +# flags: --python-version 3.11 from typing import Callable, Unpack, TypeVar T = TypeVar("T") diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 172e57cf1a4b..4de3d649c068 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -1116,6 +1116,7 @@ class D: [builtins fixtures/dict.pyi] [case testUnpackInCallableType] +# flags: --python-version 3.11 from typing import Callable, TypedDict from typing_extensions import Unpack diff --git a/test-data/unit/native-parser.test b/test-data/unit/native-parser.test index 8962875a9cf2..5e44978e0e5a 100644 --- a/test-data/unit/native-parser.test +++ b/test-data/unit/native-parser.test @@ -1300,12 +1300,12 @@ try: except* ValueError: y [out] +1:0: error: Exception groups: requires Python 3.11 or newer (current target: Python 3.10) MypyFile:1( TryStmt:1( Block:2( ExpressionStmt:2( NameExpr(x))) - * NameExpr(ValueError) Block:4( ExpressionStmt:4( @@ -1317,12 +1317,12 @@ try: except* Exception as e: y [out] +1:0: error: Exception groups: requires Python 3.11 or newer (current target: Python 3.10) MypyFile:1( TryStmt:1( Block:2( ExpressionStmt:2( NameExpr(x))) - * NameExpr(Exception) NameExpr(e) Block:4( @@ -1337,12 +1337,12 @@ except* ValueError: except* KeyError as e: z [out] +1:0: error: Exception groups: requires Python 3.11 or newer (current target: Python 3.10) MypyFile:1( TryStmt:1( Block:2( ExpressionStmt:2( NameExpr(x))) - * NameExpr(ValueError) Block:4( ExpressionStmt:4( @@ -1363,12 +1363,12 @@ else: finally: w [out] +1:0: error: Exception groups: requires Python 3.11 or newer (current target: Python 3.10) MypyFile:1( TryStmt:1( Block:2( ExpressionStmt:2( NameExpr(x))) - * NameExpr(ValueError) Block:4( ExpressionStmt:4( @@ -2022,6 +2022,7 @@ MypyFile:1( def f(*args: *Ts) -> None: pass [out] +1:13: error: Star unpack syntax: requires Python 3.11 or newer (current target: Python 3.10) MypyFile:1( FuncDef:1( f @@ -2034,6 +2035,7 @@ MypyFile:1( [case testUnpackTypeInTuple] x: tuple[int, *Ts, str] [out] +1:14: error: Star unpack syntax: requires Python 3.11 or newer (current target: Python 3.10) MypyFile:1( AssignmentStmt:1( NameExpr(x) @@ -3190,6 +3192,7 @@ MypyFile:1( # comment type A[T] = C[T] [out] +2:0: error: "type" statements: requires Python 3.12 or newer (current target: Python 3.10) MypyFile:1( TypeAliasStmt:2( NameExpr(A) @@ -3209,6 +3212,9 @@ def f[T](): pass def g[T: str](): pass def h[T: (int, str)](): pass [out] +3:0: error: Improved type parameter syntax: requires Python 3.12 or newer (current target: Python 3.10) +4:0: error: Improved type parameter syntax: requires Python 3.12 or newer (current target: Python 3.10) +5:0: error: Improved type parameter syntax: requires Python 3.12 or newer (current target: Python 3.10) MypyFile:1( FuncDef:3( f @@ -3239,6 +3245,8 @@ MypyFile:1( def f[**P](): pass class C[T: int, **P]: pass [out] +3:0: error: Improved type parameter syntax: requires Python 3.12 or newer (current target: Python 3.10) +4:0: error: Improved type parameter syntax: requires Python 3.12 or newer (current target: Python 3.10) MypyFile:1( FuncDef:3( f @@ -3261,6 +3269,8 @@ MypyFile:1( def f[*Ts](): pass class C[T: int, *Ts]: pass [out] +3:0: error: Improved type parameter syntax: requires Python 3.12 or newer (current target: Python 3.10) +4:0: error: Improved type parameter syntax: requires Python 3.12 or newer (current target: Python 3.10) MypyFile:1( FuncDef:3( f diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 659a8515455d..fa3001c3a482 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -2268,3 +2268,28 @@ _testNarrowingMappingAndAbstractSet.py:33: note: Revealed type is "dict[str, int _testNarrowingMappingAndAbstractSet.py:34: note: Revealed type is "set[str]" _testNarrowingMappingAndAbstractSet.py:38: note: Revealed type is "_collections_abc.dict_keys[str, int]" _testNarrowingMappingAndAbstractSet.py:39: note: Revealed type is "set[str]" + +[case testNativeParserTStringRequiresPython314] +# flags: --python-version=3.13 --native-parser --ignore-missing-imports +x = t"hello {1}" +[out] +_testNativeParserTStringRequiresPython314.py:2: error: T-strings: requires Python 3.14 or newer (current target: Python 3.13) + +[case testNativeParserTStringRequiresPython314InStub] +# flags: --python-version=3.13 --native-parser --ignore-missing-imports +import m +[file m.pyi] +x: object +def f() -> object: ... +y = t"hello {1}" +[out] +m.pyi:3: error: T-strings: requires Python 3.14 or newer (current target: Python 3.13) + +[case testNativeParserStarUnpackRequiresPython311] +# flags: --python-version=3.10 --native-parser +from typing_extensions import TypeVarTuple, Unpack +Ts = TypeVarTuple("Ts") +def f(x: tuple[Unpack[Ts]]) -> None: ... +def g(x: tuple[*Ts]) -> None: ... +[out] +_testNativeParserStarUnpackRequiresPython311.py:5: error: Star unpack syntax: requires Python 3.11 or newer (current target: Python 3.10)