diff --git a/boa3/internal/analyser/moduleanalyser.py b/boa3/internal/analyser/moduleanalyser.py index adf6022bc..4eb895fe0 100644 --- a/boa3/internal/analyser/moduleanalyser.py +++ b/boa3/internal/analyser/moduleanalyser.py @@ -1115,7 +1115,16 @@ def visit_AnnAssign(self, ann_assign: ast.AnnAssign): self._check_annotation_type(ann_assign.annotation, ann_assign) - # TODO: check if the annotated type and the value type are the same #86a1ctmwy + if isinstance(ann_assign.value, ast.Constant): + annotation_type = self.get_type(ann_assign.value) + if not var_type.is_type_of(annotation_type): + self._log_error( + CompilerError.MismatchedTypes(line=ann_assign.value.lineno, + col=ann_assign.value.col_offset, + expected_type_id=var_type.identifier, + actual_type_id=annotation_type.identifier) + ) + return self.assign_value(var_id, var_type, source_node=ann_assign, assignment=ann_assign.value is not None, diff --git a/boa3_test/tests/compiler_tests/test_bytes.py b/boa3_test/tests/compiler_tests/test_bytes.py index eb83100a1..775f79068 100644 --- a/boa3_test/tests/compiler_tests/test_bytes.py +++ b/boa3_test/tests/compiler_tests/test_bytes.py @@ -492,20 +492,7 @@ async def test_byte_array_set_value_negative_index_run(self): self.assertEqual(b'\x01', result) def test_byte_array_literal_value(self): - data = b'\x01\x02\x03' - expected_output = ( - Opcode.INITSLOT # function signature - + b'\x01' - + b'\x00' - + Opcode.PUSHDATA1 # a = bytearray(b'\x01\x02\x03') - + Integer(len(data)).to_byte_array(min_length=1) - + data - + Opcode.STLOC0 - + Opcode.RET # return - ) - - output, _ = self.assertCompilerLogs(CompilerWarning.TypeCasting, 'BytearrayLiteral.py') - self.assertEqual(expected_output, output) + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'BytearrayLiteral.py') def test_byte_array_default_compile(self): expected_output = ( diff --git a/boa3_test/tests/compiler_tests/test_variable.py b/boa3_test/tests/compiler_tests/test_variable.py index 9f9c3e9b2..f9c9114bf 100644 --- a/boa3_test/tests/compiler_tests/test_variable.py +++ b/boa3_test/tests/compiler_tests/test_variable.py @@ -303,22 +303,7 @@ def test_return_undeclared_variable(self): self.assertCompilerLogs(CompilerError.UnresolvedReference, 'ReturnUndeclaredVariable.py') def test_assign_value_mismatched_type(self): - string_value = '1' - byte_input = String(string_value).to_bytes() - - expected_output = ( - Opcode.INITSLOT - + b'\x01' - + b'\x00' - + Opcode.PUSHDATA1 # a = '1' - + Integer(len(byte_input)).to_byte_array() - + byte_input - + Opcode.STLOC0 - + Opcode.RET - ) - - output, _ = self.assertCompilerLogs(CompilerWarning.TypeCasting, 'MismatchedTypeAssignValue.py') - self.assertEqual(expected_output, output) + self.assertCompilerLogs(CompilerError.MismatchedTypes, 'MismatchedTypeAssignValue.py') def test_assign_binary_operation_mismatched_type(self): expected_output = (