Selaa lähdekoodia

fix https://github.com/pocketpy/pocketpy/issues/336

blueloveTH 8 kuukautta sitten
vanhempi
commit
ccd00e83a5
2 muutettua tiedostoa jossa 9 lisäystä ja 2 poistoa
  1. 3 2
      src/compiler/compiler.c
  2. 6 0
      tests/40_class.py

+ 3 - 2
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);

+ 6 - 0
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