From ccd00e83a5581f0a8bd57543c960aeb58b3a8286 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 3 Jul 2025 18:11:35 +0800 Subject: [PATCH] fix https://github.com/pocketpy/pocketpy/issues/336 --- src/compiler/compiler.c | 5 +++-- tests/40_class.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index abfed652..c9fc95ea 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -2865,13 +2865,14 @@ static Error* compile_stmt(Compiler* self) { bool is_typed_name = false; // e.g. x: int // eat variable's type hint if it is a single name - if(Ctx__s_top(ctx())->vt->is_name) { + const ExprVt* top_vt = Ctx__s_top(ctx())->vt; + if(top_vt->is_name || top_vt->is_attrib) { if(match(TK_COLON)) { c11_sv type_hint; check(consume_type_hints_sv(self, &type_hint)); is_typed_name = true; - if(ctx()->is_compiling_class) { + if(ctx()->is_compiling_class && top_vt->is_name) { NameExpr* ne = (NameExpr*)Ctx__s_top(ctx()); int index = Ctx__add_const_string(ctx(), type_hint); Ctx__emit_(ctx(), OP_LOAD_CONST, index, BC_KEEPLINE); diff --git a/tests/40_class.py b/tests/40_class.py index 845b488b..f18a4261 100644 --- a/tests/40_class.py +++ b/tests/40_class.py @@ -126,7 +126,13 @@ class MyClass: b, c = 1, 2 d = b + c + def __init__(self, m, n) -> None: + self.m: int = m + self.n: float = n + assert MyClass.a == (1, 2, 3) assert MyClass.b == 1 assert MyClass.c == 2 assert MyClass.d == 3 + +assert MyClass(1, 2).m == 1 \ No newline at end of file