Skip to content

Commit

Permalink
Check matching between properties and constructors
Browse files Browse the repository at this point in the history
So far, we only checked in the translation phase that the names and the
order of the properties and the constructor arguments match. In this
change, we also check that they match in type annotations, which is the
assumption we make in many code generators.
  • Loading branch information
mristin committed Jun 19, 2024
1 parent 29ab55d commit 3e566a4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
44 changes: 39 additions & 5 deletions aas_core_codegen/intermediate/_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
ConstantSetOfEnumerationLiterals,
Constant,
TranspilableVerification,
type_annotations_equal,
)
from aas_core_codegen.parse import tree as parse_tree

Expand Down Expand Up @@ -4038,10 +4039,14 @@ def _verify_all_non_optional_properties_are_initialized_in_the_constructor(
return errors


def _verify_orders_of_constructors_arguments_and_properties_match(
def _verify_constructor_arguments_and_properties_match(
symbol_table: SymbolTable,
) -> List[Error]:
"""Verify the order between the constructor arguments and the properties."""
"""
Verify that the constructor arguments and the properties match for all classes.
We check both the order and the types.
"""
errors = [] # type: List[Error]
for cls in symbol_table.classes:
args_without_default = [] # type: List[Identifier]
Expand Down Expand Up @@ -4098,6 +4103,37 @@ def _verify_orders_of_constructors_arguments_and_properties_match(
)
)

assert set(cls.properties_by_name.keys()) == set(
cls.constructor.arguments_by_name.keys()
)
for prop in cls.properties:
assert prop.name in cls.constructor.arguments_by_name, (
f"Expected th at we already checked that properties and "
f"constructor arguments match, but the corresponding "
f"constructor argument is missing for the property: {prop.name!r}"
)

arg = cls.constructor.arguments_by_name[prop.name]

if not type_annotations_equal(prop.type_annotation, arg.type_annotation):
errors.append(
Error(
(
cls.constructor.parsed.node
if cls.constructor.parsed is not None
else cls.parsed.node
),
f"The constructor argument {arg.name!r} "
f"and the property {prop.name!r} "
f"for the class {cls.name!r} "
f"mismatch in type. The argument is typed "
f"as {arg.type_annotation} while the property is "
f"typed as {prop.type_annotation}. "
f"We expect the constructors and the properties to match "
f"in type.",
)
)

return errors


Expand Down Expand Up @@ -4446,9 +4482,7 @@ def _verify(symbol_table: SymbolTable, ontology: _hierarchy.Ontology) -> List[Er
)

errors.extend(
_verify_orders_of_constructors_arguments_and_properties_match(
symbol_table=symbol_table
)
_verify_constructor_arguments_and_properties_match(symbol_table=symbol_table)
)

errors.extend(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Item:
class Something:
some_property: List[Item]

def __init__(self, some_property: Item) -> None:
def __init__(self, some_property: List[Item]) -> None:
self.some_property = some_property


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Item:
class Something:
some_property: List[Item]

def __init__(self, some_property: Item) -> None:
def __init__(self, some_property: List[Item]) -> None:
self.some_property = some_property


Expand Down

0 comments on commit 3e566a4

Please sign in to comment.