BESSER icon indicating copy to clipboard operation
BESSER copied to clipboard

Type checks in structural.py

Open FChikh opened this issue 2 years ago • 0 comments

TL; DR: Python is not strict to types, should we check additionally the types of values assigned to classes' fields, in particularly DomainModel?

I was (still) exploring the features of BUML, and while creating a new DomainModel class, I completed wrong the associations parameter fro constructor: I put there set of ends, but not the set of Association type. Something stupid like this:

# Ends definition
movie_starring: Property = Property(
    name="starring", owner=None, property_type=movie_class, multiplicity=Multiplicity(0, "*"))
star_starred_in: Property = Property(
    name="starredIn", owner=None, property_type=star_class, multiplicity=Multiplicity(0, "*"))

movie_owned_by: Property = Property(
    name="ownedBy", owner=None, property_type=movie_class, multiplicity=Multiplicity(1, 1))
studio_owns: Property = Property(
    name="owns", owner=None, property_type=studio_class, multiplicity=Multiplicity(0, "*"))

# BinaryAssociation definition
relation_stars: BinaryAssociation = BinaryAssociation(
    name="stars", ends={movie_starring, star_starred_in})
relation_owns: BinaryAssociation = BinaryAssociation(
    name="owns", ends={movie_owned_by, studio_owns})

model_cinema: DomainModel = DomainModel(name="Cinema", types={
    movie_class, studio_class, star_class}, associations={movie_starring, star_starred_in, movie_owned_by, studio_owns}, generalizations=None, packages=None, constraints=None)

However, code ran well, I still could properly reach list of associations per class, get their ends etc. It was until the moment I called model_cinema.associations property and tried to call list of ends, like this:

for association in model.associations:
        if len(association.ends) == 2: 
...

And here the code crashed (AttributeError: 'Property' object has no attribute 'ends'), as set of Association was actually a set of Property due to my wrong DomainModel object definition.

For sure, that was caused by wrong declaration of new object, but it seemed to me, that the program should be halted at the moment of wrong definition, not in the runtime after.

FChikh avatar Oct 19 '23 09:10 FChikh