Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Make PEP 695 constructs give a reasonable error message
Mypy does not yet support PEP 695

Fixes #16011, linking #15238
  • Loading branch information
hauntsaninja committed Sep 1, 2023
commit 62a1a70c88b23de7f1d530a6cebaebcb2b28129f
31 changes: 31 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ def ast3_parse(
NamedExpr = ast3.NamedExpr
Constant = ast3.Constant

if sys.version_info >= (3, 12):
ast_TypeAlias = ast3.TypeAlias
else:
ast_TypeAlias = Any

if sys.version_info >= (3, 10):
Match = ast3.Match
MatchValue = ast3.MatchValue
Expand Down Expand Up @@ -936,6 +941,14 @@ def do_func_def(
arg_types = [AnyType(TypeOfAny.from_error)] * len(args)
return_type = AnyType(TypeOfAny.from_error)
else:
if sys.version_info >= (3, 12) and n.type_params:
self.fail(
ErrorMessage("PEP 695 generics are not yet supported", code=codes.VALID_TYPE),
n.type_params[0].lineno,
n.type_params[0].col_offset,
blocker=False,
)

arg_types = [a.type_annotation for a in args]
return_type = TypeConverter(
self.errors, line=n.returns.lineno if n.returns else lineno
Expand Down Expand Up @@ -1110,6 +1123,14 @@ def visit_ClassDef(self, n: ast3.ClassDef) -> ClassDef:
self.class_and_function_stack.append("C")
keywords = [(kw.arg, self.visit(kw.value)) for kw in n.keywords if kw.arg]

if sys.version_info >= (3, 12) and n.type_params:
self.fail(
ErrorMessage("PEP 695 generics are not yet supported", code=codes.VALID_TYPE),
n.type_params[0].lineno,
n.type_params[0].col_offset,
blocker=False,
)

cdef = ClassDef(
n.name,
self.as_required_block(n.body),
Expand Down Expand Up @@ -1717,6 +1738,16 @@ def visit_MatchOr(self, n: MatchOr) -> OrPattern:
node = OrPattern([self.visit(pattern) for pattern in n.patterns])
return self.set_line(node, n)

def visit_TypeAlias(self, n: ast_TypeAlias) -> AssignmentStmt:
self.fail(
ErrorMessage("PEP 695 type aliases are not yet supported", 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)


class TypeConverter:
def __init__(
Expand Down
4 changes: 3 additions & 1 deletion mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ def num_skipped_suffix_lines(a1: list[str], a2: list[str]) -> int:


def testfile_pyversion(path: str) -> tuple[int, int]:
if path.endswith("python311.test"):
if path.endswith("python312.test"):
return 3, 12
elif path.endswith("python311.test"):
return 3, 11
elif path.endswith("python310.test"):
return 3, 10
Expand Down
2 changes: 2 additions & 0 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
typecheck_files.remove("check-python310.test")
if sys.version_info < (3, 11):
typecheck_files.remove("check-python311.test")
if sys.version_info < (3, 12):
typecheck_files.remove("check-python312.test")

# Special tests for platforms with case-insensitive filesystems.
if sys.platform not in ("darwin", "win32"):
Expand Down
35 changes: 35 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[case test695TypeAlias]
type MyInt = int # E: PEP 695 type aliases are not yet supported

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 \
# E: Name "T" is not defined

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]"

[case test695Class]
class MyGen[T]: # E: PEP 695 generics are not yet supported
def __init__(self, x: T) -> None: # E: Name "T" is not defined
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"

[case test695Function]
def f[T](x: T) -> T: # E: PEP 695 generics are not yet supported \
# E: Name "T" is not defined
return reveal_type(x) # N: Revealed type is "Any"

reveal_type(f(1)) # N: Revealed type is "Any"

async def g[T](x: T) -> T: # E: PEP 695 generics are not yet supported \
# E: Name "T" is not defined
return reveal_type(x) # N: Revealed type is "Any"

reveal_type(g(1)) # E: Value of type "Coroutine[Any, Any, Any]" must be used \
# N: Are you missing an await? \
# N: Revealed type is "typing.Coroutine[Any, Any, Any]"