File tree Expand file tree Collapse file tree 4 files changed +22
-14
lines changed Expand file tree Collapse file tree 4 files changed +22
-14
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,7 @@ All notable changes to this project will be documented in this file.
94
94
- #2082 : Allow the use of a list comprehension to initialise an array.
95
95
- #2094 : Fix slicing of array allocated in an if block.
96
96
- #2085 : Fix calling class methods before they are defined.
97
+ - #2111 : Fix declaration of class attributes with name conflicts using type annotations.
97
98
98
99
### Changed
99
100
Original file line number Diff line number Diff line change @@ -3283,10 +3283,11 @@ def _visit_Assign(self, expr):
3283
3283
cls_def = semantic_lhs_var .lhs .cls_base
3284
3284
insert_scope = cls_def .scope
3285
3285
cls_def .add_new_attribute (semantic_lhs_var )
3286
+ lhs = lhs .name .name [- 1 ]
3286
3287
else :
3287
3288
insert_scope = self .scope
3289
+ lhs = lhs .name
3288
3290
3289
- lhs = lhs .name
3290
3291
if semantic_lhs_var .class_type is TypeAlias ():
3291
3292
if not isinstance (rhs , SyntacticTypeAnnotation ):
3292
3293
pyccel_stage .set_stage ('syntactic' )
@@ -3297,7 +3298,7 @@ def _visit_Assign(self, expr):
3297
3298
return EmptyNode ()
3298
3299
3299
3300
try :
3300
- insert_scope .insert_variable (semantic_lhs_var )
3301
+ insert_scope .insert_variable (semantic_lhs_var , lhs )
3301
3302
except RuntimeError as e :
3302
3303
errors .report (e , symbol = expr , severity = 'error' )
3303
3304
Original file line number Diff line number Diff line change 2
2
3
3
class A :
4
4
def __init__ (self : 'A' , x : int ):
5
- self .x = x
5
+ self .data : int = x
6
6
7
7
def update (self : 'A' , x : int ):
8
- self .x = x
8
+ self .data = x
9
9
10
10
def get_A ():
11
11
return A (4 )
@@ -15,15 +15,6 @@ def get_A_int():
15
15
16
16
def get_x_from_A (a : 'A' = None ):
17
17
if a is not None :
18
- return a .x
18
+ return a .data
19
19
else :
20
20
return 5
21
-
22
- if __name__ == '__main__' :
23
- print (get_A ().x )
24
- print (get_x_from_A ())
25
- print (get_x_from_A (get_A ()))
26
- x = get_A ()
27
- x .update (10 )
28
- print (get_x_from_A (x ))
29
- x , p = get_A_int ()
Original file line number Diff line number Diff line change @@ -210,6 +210,21 @@ def test_classes_6(language):
210
210
assert p_py .get_attributes (3 ) == p_l .get_attributes (3 )
211
211
assert p_py .get_attributes (4.5 ) == p_l .get_attributes (4.5 )
212
212
213
+ def test_classes_7 (language ):
214
+ import classes .classes_7 as mod
215
+ modnew = epyccel (mod , language = language )
216
+
217
+ p_py = mod .get_A ()
218
+ p_l = modnew .get_A ()
219
+
220
+ assert mod .get_x_from_A () == modnew .get_x_from_A ()
221
+ assert mod .get_x_from_A (p_py ) == modnew .get_x_from_A (p_l )
222
+
223
+ p_py .update (10 )
224
+ p_l .update (10 )
225
+
226
+ assert mod .get_x_from_A (p_py ) == modnew .get_x_from_A (p_l )
227
+
213
228
def test_classes_8 (language ):
214
229
import classes .classes_8 as mod
215
230
modnew = epyccel (mod , language = language )
You can’t perform that action at this time.
0 commit comments