support pep695

This commit is contained in:
blueloveTH 2024-10-03 18:31:36 +08:00
parent a43ac63a82
commit 14434b6e02
4 changed files with 49 additions and 1 deletions

View File

@ -2,5 +2,6 @@
"stubPath": "include/typings", "stubPath": "include/typings",
"reportMissingModuleSource": "none", "reportMissingModuleSource": "none",
"reportArgumentType": "none", "reportArgumentType": "none",
"pythonVersion": "3.10" "reportWildcardImportFromLibrary": "none",
"pythonVersion": "3.12"
} }

View File

@ -2224,12 +2224,26 @@ static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool enable_type_h
return NULL; return NULL;
} }
static Error* consume_pep695_py312(Compiler* self) {
// https://peps.python.org/pep-0695/
Error* err;
if(match(TK_LBRACKET)) {
consume(TK_ID);
if(match(TK_COLON)){
check(consume_type_hints(self));
}
consume(TK_RBRACKET);
}
return NULL;
}
static Error* compile_function(Compiler* self, int decorators) { static Error* compile_function(Compiler* self, int decorators) {
Error* err; Error* err;
consume(TK_ID); consume(TK_ID);
c11_sv decl_name_sv = Token__sv(prev()); c11_sv decl_name_sv = Token__sv(prev());
int decl_index; int decl_index;
FuncDecl_ decl = push_f_context(self, decl_name_sv, &decl_index); FuncDecl_ decl = push_f_context(self, decl_name_sv, &decl_index);
consume_pep695_py312(self);
consume(TK_LPAREN); consume(TK_LPAREN);
if(!match(TK_RPAREN)) { if(!match(TK_RPAREN)) {
check(_compile_f_args(self, decl, true)); check(_compile_f_args(self, decl, true));
@ -2281,6 +2295,7 @@ static Error* compile_class(Compiler* self, int decorators) {
consume(TK_ID); consume(TK_ID);
py_Name name = py_namev(Token__sv(prev())); py_Name name = py_namev(Token__sv(prev()));
bool has_base = false; bool has_base = false;
consume_pep695_py312(self);
if(match(TK_LPAREN)) { if(match(TK_LPAREN)) {
if(is_expression(self, false)) { if(is_expression(self, false)) {
check(EXPR(self)); check(EXPR(self));
@ -2405,7 +2420,9 @@ __EAT_DOTS_END:
return NULL; return NULL;
} }
bool has_bracket = match(TK_LPAREN);
do { do {
if(has_bracket) match_newlines();
Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE); Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
consume(TK_ID); consume(TK_ID);
c11_sv name = Token__sv(prev()); c11_sv name = Token__sv(prev());
@ -2416,6 +2433,10 @@ __EAT_DOTS_END:
} }
Ctx__emit_store_name(ctx(), name_scope(self), py_namev(name), prev()->line); Ctx__emit_store_name(ctx(), name_scope(self), py_namev(name), prev()->line);
} while(match(TK_COMMA)); } while(match(TK_COMMA));
if(has_bracket) {
match_newlines();
consume(TK_RPAREN);
}
Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE); Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
consume_end_stmt(); consume_end_stmt();
return NULL; return NULL;

View File

@ -34,3 +34,12 @@ f()
from math import * from math import *
assert pi > 3 assert pi > 3
from math import (pi, pow, sin, cos)
from math import (
pi,
pow,
sin,
cos
)

17
tests/96_pep695_py312.py Normal file
View File

@ -0,0 +1,17 @@
class Test[T]:
def __init__(self, value: T):
self.value = value
def get_value(self) -> T:
return self.value
def add[T: int|str|float](a: T, b: T) -> T:
return a + b # type: ignore
res = add(1, 2)
assert res == 3
test = Test(1)
assert test.get_value() == 1